Search code examples
apiwinapisendmessagememo

Append text to memo using Win API


I'm trying to append text lines with this code

SendMessage(Form1.log.Handle, WM_SETTEXT, 0, Integer(PChar(textLog)));

// textLog are some lines eg 'Program started at xxx' etc

but it doesnt append, just set new text


Solution

  • Found complete solution

        procedure appendLog(const S: string);
    var
      SelStart, LineLen: Integer;
      Line: string;
    begin
    
      SelStart := SendMessage(Form1.log.Handle, EM_LINEINDEX, 0, 0);
      if SelStart >= 0 then Line := S + #13#10 else
        begin
          SelStart := SendMessage(Form1.log.Handle, EM_LINEINDEX, -1, 0);
          if SelStart < 0 then Exit;
          LineLen := SendMessage(Form1.log.Handle, EM_LINELENGTH, SelStart, 0);
          if LineLen = 0 then Exit;
          Inc(SelStart, LineLen);
          Line := #13#10 + s;
        end;
    
      SendMessage(Form1.log.Handle, EM_SETSEL, SelStart, SelStart);
      SendMessage(Form1.log.Handle, EM_REPLACESEL, 0, Longint(PChar(Line)));
    
    end;