TEdit 根据鼠标光标获取角色 ID
我之前发布了一个基于儿童表单的问题。如果没有子窗体,插入符号就是您单击 TEdit 的位置。 使用以下代码,我可以将焦点放在文本末尾的 TEdit 和插入符号上:
edt2.SetFocus;
edt2.SelStart := Length(edt2.Text);
这是我想要做的 99% 的事情。 我找到了可以获取鼠标光标位置的代码,它在 TRichEdit 上完美运行,但在 TEdit(或 TMemo)上不起作用。我为 TEdit 上的 MouseDown 编写了它。作为测试,我将其复制到 MouseMove。在这两种情况下,我得到的唯一结果是 -1
procedure TfrmClientManager.edt1MouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
var
iCharIndex: Integer;
Pt: TPoint;
begin
with TEdit(Sender) do
begin
Pt := Point(X, Y);
// Get Character Index from word under the cursor
iCharIndex := Perform(Messages.EM_CHARFROMPOS, 0, Integer(@Pt));
Caption := IntToStr(iCharIndex);
end;
编辑:
修复了 SetFocus 的输入错误。
编辑:
根据 Remy Lebeau,以下内容 100% 有效
edt2.SetFocus;
edt2.SelStart := LOWORD(TEdit(Sender).Perform(EM_CHARFROMPOS, 0, MAKELPARAM(X, Y)));
I posted a previous question based on a child form. Without the child form, the caret is where you click on the TEdit.
Using the following code I can get focus on the TEdit and the caret at the end of the text:
edt2.SetFocus;
edt2.SelStart := Length(edt2.Text);
This is 99% of what I want to do.
I found code that can get the position of the mouse cursor and it works perfectly on a TRichEdit, however it does not work on a TEdit (or TMemo). I wrote it for the MouseDown on the TEdit. As a test I copied it to the MouseMove. In both instances the only result I get is -1
procedure TfrmClientManager.edt1MouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
var
iCharIndex: Integer;
Pt: TPoint;
begin
with TEdit(Sender) do
begin
Pt := Point(X, Y);
// Get Character Index from word under the cursor
iCharIndex := Perform(Messages.EM_CHARFROMPOS, 0, Integer(@Pt));
Caption := IntToStr(iCharIndex);
end;
EDIT:
Fixed typing mistake with the SetFocus.
EDIT:
As per Remy Lebeau the following below works 100%
edt2.SetFocus;
edt2.SelStart := LOWORD(TEdit(Sender).Perform(EM_CHARFROMPOS, 0, MAKELPARAM(X, Y)));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
EM_CHARFROMPOS
与TEdit
配合得很好。但是,如果您阅读其文档,您将可以看到 RichEdit 控件与 Edit 控件的输入和返回值非常不同:您显示的代码适用于 RichEdit 控件,而不是 Edit 控件。
试试这个:
EM_CHARFROMPOS
works just fine withTEdit
. But, if you read its documentation, you will see that its input and return value are very different for a RichEdit control vs an Edit control:The code you have shown is suited for a RichEdit control, not an Edit control.
Try this instead: