Search code examples
delphihint

How to show hint using Application.ActivateHint on Delphi?


I have the following code trying to show a 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;

But there is no hint appears. What's wrong?


Solution

  • ActivateHint wants your point in screen coordinates, not in client coordinates.

    From doc:

    ActivateHint locates the control or menu item at the position specified by CursorPos, where CursorPos represents a screen coordinates in pixels. After locating the control, ActivateHint displays the control's Hint in a hint window.

    So, you have to change your method like this:

    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;