Search code examples
delphihint

How to send an hint message by code?


How can I send an hint message to the application? I've tried with a little test:

  TForm1 = class(TForm)
    ApplicationEvents1: TApplicationEvents;
    Memo1: TMemo;
    procedure ApplicationEvents1Hint(Sender: TObject);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

procedure TForm1.ApplicationEvents1Hint(Sender: TObject);
begin
  Memo1.Lines.Add(Application.Hint);
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  Application.Hint := 'Hello';
end;

Observing Memo1's lines, it seems that an empty hint message is sent everytime I set 'Hello'.

enter image description here

In a real scenario, the empty hint message will hide my hint message and I don't understand what I'm doing wrong, is this the wrong approach?


Solution

  • I suspect what you are really trying to do is tweak the currently displayed hint while the mouse is moving over a control. To do that, you can use the TApplication.OnShowHint or TApplicationEvents.OnShowHint event, or subclass the target control to handle the CM_HINTSHOW message. Any of those will provide access to a THintInfo record that you can customize, eg:

    procedure TForm1.ApplicationEvents1ShowHint(var HintStr: string;
      var CanShow: Boolean; var HintInfo: THintInfo)
    begin
      // HintInfo.HintControl is the control that is about to display a hint
      if HintInfo.HintControl = Memo1 then
      begin
        // HintInfo.CursorPos is the current mouse position within the HintControl
        HintStr := Format('Hello, cursor = %d,%d', [HintInfo.CursorPos.X, HintInfo.CursorPos.Y]);
    
        // the hint will remain active until it times out (see
        // TApplication.HintHidePause and THintInfo.HideTimeout) or
        // the mouse moves outside of the HintInfo.CursorRect.  In
        // the latter case, a new hint will be displayed. This allows
        // you to change the hint for different sections of the
        // HintControl. The CursorRect is set to the HintControl's
        // whole client area by default.
    
        // In this example, setting the new CursorRect to a 1x1 square
        // around the current CursorPos will display a new hint string
        // on each mouse movement...
        HintInfo.CursorRect := Rect(HintInfo.CursorPos.X, HintInfo.CursorPos.Y, HintInfo.CursorPos.X, HintInfo.CursorPos.Y);
      end;
    end;
    

    Note that customizing the displayed hint in this manner will not trigger the TApplication.OnHint/TApplicationEvents.OnHint event on each hint change, only when a new hint popup is displayed. OnShowHint/CM_HINTSHOW allows you to perform live updates of an existing hint popup.

    hint with live updates