Search code examples
delphirichedit

How to set caret position by right mouse button click for TRichEdit?


When I right-click a word in a RichEdit control, I want the cursor to be positioned inside that word the way it happens with left mouse button click.

Is it possible to achieve?


Solution

  • Just use the ContextPopup event and simulate a left mouse click

    type    
        TForm1 = class(TForm)
        edtRich: TRichEdit;
        procedure edtRichContextPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean);
      end;
    
    implementation
    
    procedure TForm1.edtRichContextPopup(Sender: TObject; MousePos: TPoint; 
        var Handled: Boolean);
    begin
      mouse_event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTDOWN,
                  MousePos.x, MousePos.y, 0, 0);
      mouse_event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTUP,
                  MousePos.x, MousePos.y, 0, 0);
    end;