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

その時点でヒント ウィンドウ インスタンスは 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 に答える
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 に答える