2015-06-22 4 views
1

У меня есть этот код, с помощью которого я могу установить размер шрифта подсказки управления, но я хочу как-то настроить его позже во время выполнения. Как я могу это сделать ?Как настроить свойства подсказки элемента управления во время выполнения?

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. 
+0

Вы выполнить 'Font.Size: = SomeValue' спустя некоторое время. Предположительно это должен быть 'Font.Size', а не' Canvas.Font.Size'. –

+0

'Font.Size' этого компонента? –

+0

Ваше окно подсказки –

ответ

1

Поскольку существует только один экземпляр окна намекает на то время, и этот экземпляр будет создан после вызова CMHintShow, вы можете использовать переменный класс для дополнительной подсказки настройки. Class variable - это член класса, который является общим для всех экземпляров класса и может быть доступен непосредственно через класс или экземпляр класса.

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; 
1

Начиная от показаний, данных TLama я, наконец, решить эту проблему. Ключ должен был установить Canvas.Font.Size в TMyHintWindow.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:[email protected]; 
AMessage.HintInfo.HintWindowClass:=TMyHintWindow; 
AMessage.HintInfo.HintStr:='My custom hint'; 
end; 

end. 
Смежные вопросы