如何通过单击鼠标右键为 TRichEdit 设置插入符位置?
How to set caret position by right mouse button click for TRichEdit?
当我在 RichEdit 控件中右键单击一个词时,我希望光标定位在该词内,就像单击鼠标左键时发生的那样。
是否可以实现?
只需使用 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;
我在 Whosebug 上找到了另一个解决方案。以下是 RRUZ 的代码稍作修改。
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;
当我在 RichEdit 控件中右键单击一个词时,我希望光标定位在该词内,就像单击鼠标左键时发生的那样。
是否可以实现?
只需使用 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;
我在 Whosebug 上找到了另一个解决方案。以下是 RRUZ 的代码稍作修改。
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;