2

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

在这个类似的问题中,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.
4

1 回答 1

2

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

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,您可以自定义提示字体。

最后一点:由于当前的实现(D 10.2.3 Tokyo)THintWindow.Paint,您无法更改提示文本的字体颜色,因为该值是从Screen.HintFont.Color每次绘制提示窗口时获取的。在这种情况下,您必须自己覆盖Paint并绘制完整的提示窗口。

于 2018-10-08T20:36:29.180 回答