How to change HINT font size of my custom control ?

2.3k views Asked by At

After long searches on the Internet, with the information I found, I created this code to change the hint font size of my control. But when I try to assign Message.HintInfo.HintWindowClass:=HintWin; it give me error: E2010 Incompatible types: 'THintWindowClass' and 'THintWindow'. If I try to typecast THintWindowClass(HitWin) I get access violation. What should I do ?

In this similar question, Remy Lebeau says: "To change the layout of the hint, you can derive a new class from THintWindow and assign that class type to the THintInfo.HintWindowClass field.".... But I do not understand what he meant.

unit Unit1;

interface

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

type
  TMyButton = class(TButton)
  protected
    HintWin: THintWindow;
    procedure CMHintShow(var Message: TCMHintShow); message CM_HINTSHOW;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    MyButton: TMyButton;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
 MyButton:=TMyButton.Create(Form1);
 MyButton.Parent:=Form1;
 MyButton.Caption:='Test';
 MyButton.Left:=100;
 MyButton.Top:=100;
 MyButton.ShowHint:=true;
end;

constructor TMyButton.Create(AOwner: TComponent);
begin
 inherited;
 HintWin:=THintWindow.Create(self);
 HintWin.Canvas.Font.Size:=24;
end;

destructor TMyButton.Destroy;
begin
 HintWin.Free;
 inherited;
end;

procedure TMyButton.CMHintShow(var Message: TCMHintShow);
begin
 inherited;
 Message.HintInfo.HintWindowClass:=HintWin;
 Message.HintInfo.HintStr:='My custom hint';
end;

end.
1

There are 1 answers

0
ventiseis On

As others mentioned in the comments, you can use a custom hint window like this:

Message.HintInfo.HintWindowClass := TMyHintWindow;

Using the following declaration, you have a bigger hint font only for your button:

TMyHintWindow = class(THintWindow)
public
  constructor Create(AOwner: TComponent); override;
end;

//...

constructor TMyHintWindow.Create(AOwner: TComponent);
begin
  inherited;
  Canvas.Font.Size := 20;
end;

Explanation: in THinWindow.Create, the font is initialized with the value of Screen.HintFont - so after the call to inherited, you can customize your hint font.

A final note: because of the current implementation (D 10.2.3 Tokyo) of THintWindow.Paint, you can not change the font color of the hint text, because the value is taken from Screen.HintFont.Color every time the hint window is painted. In such cases, you'll have to override Paint and draw the complete hint window yourself.