Append text to memo using Win API

流过昼夜 提交于 2019-12-08 09:31:46

问题


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


回答1:


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.




回答2:


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.




回答3:


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;



回答4:


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


来源:https://stackoverflow.com/questions/10052804/append-text-to-memo-using-win-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!