Append text to memo using Win API

2019-06-14 04:37发布

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

4条回答
啃猪蹄的小仙女
2楼-- · 2019-06-14 05:07

It is recommended that you do not use EM_SETSEL or EM_REPLACESEL. Because on newer operating systems like vista and windows 7, the UAC, will prevent you from sending these messages. I suggest you to. 1. Obtain the handle of the window element 2. Do a setfocus, this will position your cursor in the textarea 3. Then you should use SendInput, this will not have any problems with UAC

Hope it helps.

查看更多
We Are One
3楼-- · 2019-06-14 05:09

WM_SETTEXT will replace the entire content; either read the current content, append your new text & set the lot or ensure the caret is at the point you wish to append to and add the new text with EM_REPLACESEL.

查看更多
淡お忘
4楼-- · 2019-06-14 05:09

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;
查看更多
The star\"
5楼-- · 2019-06-14 05:12

Or better:

SendMessage(Form1.log.Handle, EM_SETSEL, 0, -1);
SendMessage(Form1.log.Handle, EM_SETSEL, (WPARAM)-1, -1);
SendMessage(Form1.log.Handle, EM_REPLACESEL, 0, (LPARAM)Msg); //add a text
//SendMessage(Form1.log.Handle, EM_REPLACESEL, 0, (LPARAM)L"\r\n"); //add a new line
查看更多
登录 后发表回答