Hints or Tool Tips in Delphi Application

Hints, we also call Tool Tips are used to show some information related to an action for Users. By default it displays in a separate yellow color window when we move mouse on buttons, menus etc. But we can create our own customized Hint also. So in this blog I will give a brief idea about use of Hints in our Delphi application. We will also check how to customize or create a new Hint as per our requirement.

Display Hint for individual Controls
In Delphi every run time visible component/control and form has 2 main properties Hint and ShowHint which are used to display Hints. Hint property is Sting type used to set the Hint text. ShowHint is Boolean type used to whether display hint or not.

Here I created a form and put some controls on that. And I set Hint and Showhint property for Button at design time.


Now at run time we can see the hint when we move mouse on button as follow

We can set the same properties for other controls in code also as follow.
procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo1.Hint := 'My test Memo hint';
  Memo1.ShowHint := True;
end;
and at run time we can see .

Multi line Hint.
Creating multi line Hint is not so difficult. We can use #13 or Chr(13) for multi line concatenate.
procedure TForm1.btnMultilinehintClick(Sender: TObject);
begin
  CheckBox1.Hint := 'First Line 1'+#13+'Second Line 2'+chr(13)+'Hints';
  CheckBox1.ShowHint := True;
end;

Disable /Enable Application Hint
Delphi provides a way to Disable or Enable hint display for whole application.
// To disable hint. Hints won't display in whole application //
procedure TForm1.btnDisableHintClick(Sender: TObject);
begin
  Application.ShowHint := False;
end;
// To enable hint. Hints can be displayed in whole application //
procedure TForm1.btnEnableHintClick(Sender: TObject);
begin
  Application.ShowHint := True;
end;
Change Hint show and hide timings. 
We can set Hint display duration by following way
procedure TForm1.btnChangeHintTimeClick(Sender: TObject);
begin
  Application.HintPause := 250;    // 250 mSec before hint is shown
  Application.HintHidePause:=3000; // hint disappears after 3 secs
end;

Change Application Hint Font and Color
We can change Hint font and color in following way.
// Declare a procedure for Application.OnShowHint event //
  private
    { Private declarations }
    procedure MyShowHint(var HintStr: string; var CanShow: Boolean; var HintInfo: THintInfo);

implement
// following code will change the hint font and color to bold and yellow background //
procedure TForm1.MyShowHint(var HintStr: string; var CanShow: Boolean;
  var HintInfo: THintInfo);
var
  i: integer;
begin
  for I := 0 to Application.ComponentCount-1 do
  begin
    if Application.Components[0] is THintWindow then
    begin
      with THintWindow(Application.Components[0]) do
      begin
        Canvas.Font.Name := 'Monotype Corsiva';
        Canvas.Font.Size := 15;
        Canvas.Font.Style := [fsBold];
        Canvas.Brush.Color := clYellow;
      end;
    end;
  end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnShowHint := MyShowHint;
end;

With above code at run time


Creating own Custom Hint for Delphi application
If you don’t quite like the way how Delphi’s default hints look like, you can use THintWindow component to create your own customized hint window.
Here’s an example:
// Declare a new custom class from THintWindow class //
interface
type
  TMyHint = class(THintWindow)
    public
      Constructor Create(Aowner: TComponent); Override;
  end;

implement
// complete constructor as required //
constructor TMyHint.Create(Aowner: TComponent);
begin
  inherited;
  Self.Canvas.Font.Name := 'Courier New';
  Self.Canvas.Font.Size := 20;
  Self.Canvas.Font.Style := [fsUnderline];
  Self.Canvas.Brush.Color := clSkyBlue;
end;
// assign HintWindowClass to new declared class //
procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.ShowHint := false;
  HintWindowClass := TMyHint;
  Application.ShowHint := True;
end;
at run time

THintwindow
THintWindow implements the small pop-up window that appears over a control at run time, when the control has its ShowHint property set to True. Use objects of the THintWindow class to display a Help Hint pop-up window directly from an application. Override THintWindow to customize the window that appears automatically for controls with their ShowHint property set to True. After overriding THintWindow to create a new derived type, assign the new type to the global HintWindowClass variable at application start-up time, so that the new hint window type is used for Help Hints.

HintWindowClass
Specifies the class for the window used to display Help Hints. When the application displays a Help Hint, it creates an instance of HintWindowClass to represent the window used for displaying the Hint. Applications can customize this window by creating a descendant of THintWindow and assigning it to the HintWindowClass variable at application start-up time.

Comments

  1. Olá, construí o displayHint no meu projeto.
    Acontece que está apresentando "access violation"

    No formMain funciona normalmente, mas se construo em outro formulário apresenta este erro.

    ReplyDelete
    Replies
    1. Can you please share the block of code that raises error and code how you are creating?

      Delete

Post a Comment

Popular posts from this blog

ShellExecute in Delphi

How to send Email in Delphi?

Variants in Delphi. Use of Variant Array. How to check a Variant is unassigned or empty or clear ?