2

我有这段代码,我可以使用它设置控件提示的字体大小,但我希望能够稍后在运行时以某种方式对其进行调整。我怎样才能做到这一点 ?

unit Unit1;

interface

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

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

  TMyButton = class(TButton)
  protected
    procedure CMHintShow(var Message: TCMHintShow); message CM_HINTSHOW;
  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;

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

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

end.
4

3 回答 3

1

由于当时只有一个提示窗口实例,并且该实例将在调用后创建CMHintShow,您可以使用类变量进行额外的提示自定义。类变量是类的所有实例共享的类成员,可以通过类类型或类实例直接访问。

type
  TMyHintWindow = class(THintWindow)
  protected
    class constructor ClassCreate;
  public
    class var FontSize: integer;
    constructor Create(AOwner: TComponent); override;
  end;

class constructor TMyHintWindow.ClassCreate;
begin
  FontSize := 25;
end;

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

然后你可以FontSize改变CMHintShow方法

procedure TMyButton.CMHintShow(var Message: TCMHintShow);
begin
  inherited;
  TMyHintWindow.FontSize := 12;
  Message.HintInfo.HintWindowClass := TMyHintWindow;
  Message.HintInfo.HintStr := 'My custom hint';
end;
于 2015-06-22T18:50:48.810 回答
1

TLama给出的指示开始,我终于解决了这个问题。关键是要设置Canvas.Font.SizeTMyHintWindow.CalcHintRect

这是代码:

unit Unit1;

interface

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

type
  TMyHintData = record
    FontSize: Integer;
  end;

  TMyHintWindow = class(THintWindow)
  public
    function CalcHintRect(MaxWidth: Integer; const AHint: string; AData: TCustomData): TRect; override;
  end;

  TMyButton = class(TButton)
  private
    procedure CMHintShow(var AMessage: TCMHintShow); message CM_HINTSHOW;
  public
    FMyHintData: TMyHintData;
    constructor Create(AOwner: TComponent); override;
  end;

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
 TMyButton(Sender).FMyHintData.FontSize:=44;
end;

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;
 MyButton.OnClick:=Button1Click;
end;

function TMyHintWindow.CalcHintRect(MaxWidth: Integer; const AHint: string; AData: TCustomData): TRect;
begin
 Canvas.Font.Size:=TMyHintData(AData^).FontSize;
 Result:=inherited;
end;

constructor TMyButton.Create(AOwner: TComponent);
begin
 inherited;
 FMyHintData.FontSize:=25;
end;

procedure TMyButton.CMHintShow(var AMessage: TCMHintShow);
begin
 inherited;
 AMessage.HintInfo.HintData:=@FMyHintData;
 AMessage.HintInfo.HintWindowClass:=TMyHintWindow;
 AMessage.HintInfo.HintStr:='My custom hint';
end;

end.
于 2015-06-22T18:55:14.077 回答
0
procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnShowHint:=AppOnShowHint;
end;

procedure TForm1.AppOnShowHint(var HintStr: string; var CanShow: Boolean; var HintInfo: THintInfo);
begin
  {Use HintInfo (type:THintInfo) to specify some property of hint-window}
  {For example: set hint-window width to the width of longest word in the hint-text}
  HintInfo.HintMaxWidth:=1;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  {Set HintFont at runtime}
  Screen.HintFont.Size:=strtoint(Edit1.Text);
  {It's necessary to recreate the Application.FHintWindow private variable, so:}
  Application.ShowHint:=False;
  Application.ShowHint:=True;
end;
于 2019-02-07T06:47:17.933 回答