2
votes

In a Delphi 10.3.3 Windows VCL Application, in the OnHint event-handler of a TApplicationEvents component, I show the current hint in the status-bar:

procedure TForm1.ApplicationEvents1Hint(Sender: TObject);
begin
  statMain.SimpleText := Application.Hint;
end;

However, I would like to add some specific text containing specific run-time data depending on which control sends the hint.

Unfortunately, the Sender parameter does not give that information.

So how can I detect which control has sent the hint?

1
Ingenious! The most simple often are the best solutions!user1580348
@RemyLebeau: I am aware of OnShowHint and its HintInfo.HintControl. The reason I didn't mention it is because it seems only to fire if the control has ShowHint set to True (which I never have, since I want my hints in the status bar but not as yellow popups), and it is fired with the usual popup hint delay (I want the hint immediately). The OnHint event doesn't have these "shortcomings". Thus, I looked for an alternative solution. I realise now that I can still suppress the hint popup with OnShowHint, and get rid of the delay using Application.HintPause.Andreas Rejbrand
Maybe, using OnShowHint in addition to OnHint makes things unnecessarily complicated?user1580348

1 Answers

5
votes

The OnHint event does not provide access to any information about the control that is displaying the hint.

However, the OnShowHint event does, and you can fully customize the hint however you want in that event:

procedure TForm1.ApplicationEvents1ShowHint(var HintStr: string;
  var CanShow: boolean; var HintInfo: THintInfo);
begin
  if HintInfo.HintControl = DesiredControl then
  begin
    // customize HintStr, and/or HintInfo fields, as needed...
  end;
end;

procedure TForm1.ApplicationEvents1Hint(Sender: TObject);
begin
  statMain.SimpleText := Application.Hint;
end;

The HintInfo provides all kinds of information about the hint that you can customize:

HintControl
The name of the control for which hint processing is occurring.

HintWindowClass
The class of the hint-window control. The default is THintWindow, but you can specify any class derived from THintWindow. Use this field if you want to substitute a custom hint window for THintWindow.

HintPos
The default position in screen coordinates of the top-left corner of the hint window. Change where the window appears by changing this value.

HintMaxWidth
The maximum width of the hint window before word wrapping begins. By default, the value is the width of the screen (the Width property of the global Screen variable).

HintColor
The background color of the Hint window.

CursorRect
The rectangle the user's mouse pointer must be in for the hint window to appear. The default value for CursorRect is the client rectangle of the control. Change this value so that a single control can be divided into several hint regions. When the user moves the mouse pointer outside the rectangle, the hint window disappears.

CursorPos
The location of the mouse pointer within the control.

ReshowTimeout
How long the hint system should wait before asking about the hint status again. By default, this field is zero, indicating that the hint status will not change. Setting it to a non-zero value will cause the hint to act, after the requested milliseconds have elapsed, as if the user moved the mouse outside the hint rectangle and back in. This can be used to either put off hint processing for a period of time, or to allow the hint to be updated periodically.

HideTimeout
The number of milliseconds to show the hint. By default, it is set to the value of the Application variable's HintHidePause property.

HintStr
The string to be displayed in the hint window. This allows an OnHint event handler to modify the contents of a hint before it is displayed. By default, it contains the value returned by the GetShortHint function when passed the value of the Application variable's Hint property.

HintData
Additional data to be passed to the hint-window control. Use this field in conjunction with HintWindowClass.

Also, FYI, you don't need to use the TApplication(Event).OnHint event just to display the TApplication.Hint text in a TStatusBar. If you set the StatusBar's AutoHint property to true then the StatusBar can display TApplication.Hint updates automatically. You just need to make sure that you don't have an OnHint handler assigned, otherwise AutoHint will not work (OnShowHint is fine, though).