当我右键单击 RichEdit 控件中的单词时,我希望光标位于该单词内,就像单击鼠标左键一样。
有可能实现吗?
我在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;
只需使用 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;