如何更改自定义控件的HINT字体大小?

时间:2015-06-22 12:33:59

标签: delphi delphi-2009 hints

在互联网上长时间搜索后,根据我找到的信息,我创建了这段代码来更改控件的提示字体大小。但是当我尝试分配Message.HintInfo.HintWindowClass:=HintWin;时,它会给我错误:E2010不兼容的类型:'THintWindowClass'和'THintWindow'。如果我尝试对THintWindowClass(HitWin)进行类型转换,则会导致访问冲突。我该怎么办?

在这个similar question中,Remy Lebeau说:“要更改提示的布局,可以从THintWindow派生一个新类,并将该类类型分配给THintInfo.HintWindowClass字段。”......但是我不明白他的意思。

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 个答案:

答案 0 :(得分:1)

正如评论中提到的其他人一样,您可以使用这样的自定义提示窗口:

Message.HintInfo.HintWindowClass := TMyHintWindow;

使用以下声明,您只能为按钮使用更大的提示字体:

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

//...

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

说明:在THinWindow.Create中,字体用值Screen.HintFont初始化-因此,在调用inherited之后,您可以自定义提示字体。

最后的注释:由于THintWindow.Paint的当前实现(D 10.2.3 Tokyo),您不能更改提示文本的字体颜色,因为该值取自Screen.HintFont.Color提示窗口被绘制的时间。在这种情况下,您将必须覆盖Paint并自己绘制完整的提示窗口。

相关问题