3

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.
Community
  • 1
  • 1
Marus Gradinaru
  • 2,824
  • 1
  • 26
  • 55
  • 1
    [Here's an example](http://stackoverflow.com/q/20748641/62576) of using a custom hint window class that may help. – Ken White Jun 22 '15 at 12:57
  • I am doing the same way, but Delphi won't let me assign the newly created HintWindow. – Marus Gradinaru Jun 22 '15 at 13:12
  • `THintWindowClass` is metaclass defined as `THintWindowClass = class of THintWindow;` You cannot assign `THintWindow` object instance to it, but `THintWindow` class or `THintWindow` descendant class. `Message.HintInfo.HintWindowClass := THintWindow;` – Dalija Prasnikar Jun 22 '15 at 13:13
  • 1
    You're doing it wrong. See the example I posted again, **specifically** the code that assigns to `HintWindowClass`. `HintWindowClass` wants a metaclass, and you're trying to assign a specific instance of a hint window. They're not the same thing, which is why it's HintWindow**Class**. Also, `HintWindowClass` is a global setting, which means it affects all hint windows within the entire application. You can't change it for just one control class. – Ken White Jun 22 '15 at 13:14
  • @Marus Do you know what a metaclass is? It's an advanced topic. Not surprising if you don't know. No point at all in continuing until you know what a metaclass is. – David Heffernan Jun 22 '15 at 13:24
  • I don't know what a metaclass is. But someone still can post an answer with an working code... I would appreciate very much. – Marus Gradinaru Jun 22 '15 at 13:25
  • I've done it myself :) Now, what should I do: post an answer to my own question, edit the question or delete the question ? – Marus Gradinaru Jun 22 '15 at 14:01
  • How are you going to stop asking the same question over and again if you don't want to know what a metaclass is? Here's my free advice. Solving specific problems is great, but learning new concepts makes it so much easier for you to solve problems in the future. Do yourself a favour and find out what a metaclass is. – David Heffernan Jun 22 '15 at 14:04
  • I rolled back your edit. If you want to post a solution, do so in the form of an answer in the space provided. Editing the problem code to show the solution means that future readers don't see the problem you're asking for help solving. This is a question and answer site, and you've posted the question. – Ken White Jun 22 '15 at 14:23
  • I solved the assignation error but another problem has occurred so I need to edit the question... – Marus Gradinaru Jun 22 '15 at 14:36
  • Please don't ask a whole new question in an edit. That needs a new question. As for setting font size at runtime, yes of course you can do that. Just set the property at runtime. That's it. – David Heffernan Jun 22 '15 at 15:29

1 Answers1

3

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.

ventiseis
  • 3,029
  • 11
  • 32
  • 49