Search code examples
windowsdelphiwindow-handles

Setting carret-position in an External Application's text area?


Thanks to Rob Kennedy's answer to my question on how to set the Skype Chat window text.

However, whenever I set the text using

SendMessage(RichEditWnd,WM_SETTEXT,0,Integer(PChar(Edit1.Text)));

Then when I click on the Chat Edit control in Skype, the carret is placed at the beginning and some clicking is required to get it "right" again.

Is there a Windows Message for setting the carret position? Or atleast something that I can use to set the Carret Position to the end of the text? :)


Solution

  • Yes, there is: EM_EXSETSEL.

    wParam should be 0, and lParam should be a pointer to a TCharRange structure containing the first and last characters in the selection. You want these to be equal (that is, zero characters selected).

    For instance,

    var
      cr: TCharRange;
    begin
      cr.cpMin := 2;
      cr.cpMax := 2;
      SendMessage(RichEdit1.Handle, EM_EXSETSEL, 0, integer(@cr));
    

    will set the caret just before the third character in the Rich Edit control.