如何在Delphi上使用Application.ActivateHint显示提示?

Dmi*_*try 2 delphi hint

我有以下代码试图显示提示:

procedure TMyGrid.OnGridMouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
var
  aPoint: TPoint;
begin
  inherited;
  //Application.Hint := 'Hint Text';
  //Application.ShowHint := True;
  Grid.Hint := 'Hint Text';
  Grid.ShowHint := True;
  aPoint.X := X;
  aPoint.Y := Y;
  Application.ActivateHint(aPoint);
end;
Run Code Online (Sandbox Code Playgroud)

但没有暗示出现.怎么了?

Rem*_*eau 9

正确的方法是让网格处理消息,以便在正常显示时自定义当前提示,而不是试图强制TApplication在网格控件的OnMouseMove事件中显示新的提示CM_HINTSHOW.

CM_HINTSHOW消息为您提供指向THintInfoLPARAM值中记录的指针. THintInfo有一个CursorPos成员告诉你当前鼠标在控件的客户区域内的位置,以及一个CursorRect允许你在控件的客户区内定义当前提示应该有效的矩形的THintInfo成员(还有其他成员可以进一步自定义提示,suh如文本,颜色,窗口类,显示/隐藏超时等).当鼠标移动到该矩形之外但仍在控件的客户区域内时,将CM_HINTSHOW再次生成该消息以获取用于更新当前提示的新信息.

如果需要在每次鼠标移动时更新提示,则可以定义CursorRect仅包含当前的自定义,CursorPos例如:

type
  TMyGrid = class(...)
  private
    procedure CMHintShow(var Message: TCMHintShow); message CM_HINTSHOW;
  end;

procedure TMyGrid.CMHintShow(var Message: TCMHintShow);
begin
  with Message.HintInfo^ do
  begin
    HintStr := Format('Hint Text (%d,%d)', [CursorPos.X, CursorPos.Y]);
    CursorRect := Rect(CursorPos.X, CursorPos.Y, CursorPos.X, CursorPos.Y);
  end;
end;
Run Code Online (Sandbox Code Playgroud)


jac*_*ate 5

ActivateHint 希望您的点在屏幕坐标中,而不在客户坐标中。

从文档:

ActivateHint将控件或菜单​​项定位在CursorPos指定的位置,其中CursorPos表示以像素为单位的屏幕坐标。找到控件后,ActivateHint在提示窗口中显示控件的提示。

因此,您必须像这样更改方法:

procedure TMyGrid.OnGridMouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
var
  aPoint: TPoint;
begin
  inherited;
  //Application.Hint := 'Hint Text';
  Grid.Hint := 'Hint Text';
  Grid.ShowHint := True;
  aPoint.X := X;
  aPoint.Y := Y;
  aPoint := ClientToScreen(aPoint);
  Application.ActivateHint(aPoint);
end;
Run Code Online (Sandbox Code Playgroud)

  • 我认为,正确的代码:`Grid.ClientToScreen`。在我的情况下它不会出现 - 例如,X:200,Y:15。 (2认同)