如何通过鼠标右键单击TRichEdit来设置插入位置?

时间:2018-05-21 13:08:54

标签: delphi richedit

当我右键单击RichEdit控件中的单词时,我希望光标按照鼠标左键单击的方式放置在该单词内。

有可能实现吗?

2 个答案:

答案 0 :(得分:1)

我在Stackoverflow上找到了另一个解决方案。以下是RRUZ的https://stackoverflow.com/a/6197549/3986609稍微修改过的代码。

procedure TForm1.RichEdit1MouseDown(Sender: TObject; Button: TMouseButton;   Shift: TShiftState; X, Y: Integer);
var
    APoint  : TPoint;
    Index   : Integer;
begin
    if Button = mbRight then
    begin
        APoint := Point(X, Y);
        Index :=  SendMessage(TRichEdit(Sender).Handle,EM_CHARFROMPOS, 0, Integer(@APoint));
        if Index<0 then Exit;
        TRichEdit(Sender).SelStart:=Index;
    end;
end;

答案 1 :(得分:0)

只需使用ContextPopup事件并模拟鼠标左键单击

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;