Mirror

Display hints on the title bar of a TForm (Views: 100)


Problem/Question/Abstract:

How to display hints on the title bar of a TForm

Answer:

To accomplish this you need to create a handler for the OnHint event for TApplication. Whenever a hint is going to fire, Delphi calls the TApplication.OnHint event for processing, if nothing is defined then the default processing occurs (i.e. the yellow tool-tip window).

To override the event, you need to define a TNotifyEvent method in your form and assign it to Application.OnHint. Following is sample code that demonstrates this. To use, create a new project and drop three buttons on the form. Then set the Form's ShowHint property to True. Finally enter the following code and run the application, when you move the mouse over the buttons, their hint will appear as the form's caption.

unit Sample1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    procedure DoHint(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

procedure TForm1.DoHint(Sender: TObject);
begin

  {A hint can contain to pieces, a short and long hint separated by a pipe '|' (e.g. "Open File|Displays a file browser to select file to open". The short hint is "Open File" and the long hint is "Displays a file browser to select file to open".)

  To display the short portion, use the global method
  GetShortHint(const Hint: string): string;

  To display the long portion, use the global method
  GetLongHint(const Hint: string): string;}

  Caption := GetLongHint(Application.Hint);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnHint := DoHint;
  {Assigns the form's current caption as the form's hint}
  Hint := Caption;
  {Assign Hints to the buttons}
  Button1.Hint := 'Button One|This is the hint for button 1';
  Button2.Hint := 'Button Two|This is the hint for button 2';
  Button3.Hint := 'Button Three|This is the hint for button 3';
end;

end.

<< Back to main page