TEdit 根据鼠标光标获取角色 ID

发布于 2025-01-10 09:43:20 字数 948 浏览 0 评论 0原文

我之前发布了一个基于儿童表单的问题。如果没有子窗体,插入符号就是您单击 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿萝 2025-01-17 09:43:20

EM_CHARFROMPOSTEdit 配合得很好。但是,如果您阅读其文档,您将可以看到 RichEdit 控件与 Edit 控件的输入和返回值非常不同:

参数

wParam

未使用此参数。

lParam

控件工作区中点的坐标。坐标以屏幕单位为单位,并且相对于控件工作区的左上角。

丰富的编辑控件:指向包含水平和垂直坐标的POINTL结构的指针。

编辑控件: LOWORD 包含水平坐标。 HIWORD 包含垂直坐标。

返回值

丰富的编辑控件:返回值指定最接近指定点的字符的从零开始的字符索引。如果指定点超出控件中的最后一个字符,则返回值指示编辑控件中的最后一个字符。

编辑控件: LOWORD 指定最接近指定点的字符的从零开始的索引。该索引相对于控件的开头,而不是行的开头。如果指定点超出编辑控件中的最后一个字符,则返回值指示控件中的最后一个字符。 HIWORD 指定包含该字符的行的从零开始的索引。对于单行编辑控件,该值为零。如果指定点超出行中最后一个可见字符,则索引指示行分隔符。

您显示的代码适用于 RichEdit 控件,而不是 Edit 控件。

试试这个:

procedure TfrmClientManager.edt1MouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
var
  iCharIndex: Integer;
begin
  // Get Character Index from word under the cursor
  iCharIndex := LOWORD(TEdit(Sender).Perform(EM_CHARFROMPOS, 0, MAKELPARAM(X, Y)));
  Caption := IntToStr(iCharIndex);
end;

EM_CHARFROMPOS works just fine with TEdit. 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:

Parameters

wParam

This parameter is not used.

lParam

The coordinates of a point in the control's client area. The coordinates are in screen units and are relative to the upper-left corner of the control's client area.

Rich edit controls: A pointer to a POINTL structure that contains the horizontal and vertical coordinates.

Edit controls: The LOWORD contains the horizontal coordinate. The HIWORD contains the vertical coordinate.

Return value

Rich edit controls: The return value specifies the zero-based character index of the character nearest the specified point. The return value indicates the last character in the edit control if the specified point is beyond the last character in the control.

Edit controls: The LOWORD specifies the zero-based index of the character nearest the specified point. This index is relative to the beginning of the control, not the beginning of the line. If the specified point is beyond the last character in the edit control, the return value indicates the last character in the control. The HIWORD specifies the zero-based index of the line that contains the character. For single-line edit controls, this value is zero. The index indicates the line delimiter if the specified point is beyond the last visible character in a line.

The code you have shown is suited for a RichEdit control, not an Edit control.

Try this instead:

procedure TfrmClientManager.edt1MouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
var
  iCharIndex: Integer;
begin
  // Get Character Index from word under the cursor
  iCharIndex := LOWORD(TEdit(Sender).Perform(EM_CHARFROMPOS, 0, MAKELPARAM(X, Y)));
  Caption := IntToStr(iCharIndex);
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文