TComponent から派生させたコンポーネントを作成しています。
ParentFontプロパティをTrueにしている場合に
MyFormの親フォーム(メインフォーム)のフォントが変更されたときに
MyFormのフォントも変更したいのですが、どうすればよいのでしょうか?
以下、フォントに関する部分以外を省略して
関係のある部分だけを抜き出してみました。
TMyComponent = class(TComponent)
private
MyForm: TForm;
FParentFont: Boolean;
protected
procedure SetFont(Value: TFont);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Font: TFont read FFont write SetFont;
property ParentFont: Boolean read FParentFont write FParentFont;
end;
constructor TMyComponent.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
MyForm := TForm.CreateNew(Application.MainForm);
MyForm.Parent := Application.MainForm;
// フォームのフォント
FFont := TFont.Create;
end;
destructor TOriginalDialogForms.Destroy;
begin
FFont.Free;
MyForm.Free;
inherited Destroy;
end;
procedure TMyComponent.SetFont(Value: TFont);
begin
if Value <> FFont then begin
FFont.assign(Value);
end;
end;
TFormを拡張して(たとえばTMyFormとか作って)
そこにフォント変更時のメッセージ捕まえる
ようにしておくのは既に試されましたか?(私ならそうするかな、みたいな感じです。)
だめでしたか?
↓こんな感じで
{ WMFONTCHANGE }
Procedure TMyForm.WMFONTCHANGE(Var Msg: TWMFONTCHANGE);
Begin
inherited;
{ここにやりたい処理を書く、例えば↓}
ChildForm1.Font.Name := MyForm1.Font.Name;
End;
実は今、途中まで作ろうとしたのですが
(脳が)疲れて作るの辞めてしまったので( ;´Д`)いやぁぁぁぁぁー!
動作するか確認はとれてません(´Д⊂ モウダメ。
試してないけど、これだけじゃダメ?
MyForm.ParentFont := True;
親フォームのフォント変更時に CM_PARENTFONTCHANGED メッセージの
WParam が 0 で送られてくるのが原因なので、独自に処理をする必要があるようです。
以下はサブクラス化を用いた場合のコードです。
type
TMyComponent = class(TComponent)
private
FMyForm: TForm;
FDefMyFormProc: TWndMethod;
procedure MyFormProc(var Message: TMessage);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;
implementation
{ TMyComponent }
constructor TMyComponent.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FMyForm:= TForm.CreateNew(Application.MainForm);
FMyForm.Parent:= Application.MainForm;
FDefMyFormProc := FMyForm.WindowProc;
FMyForm.WindowProc:= MyFormProc;
end;
destructor TMyComponent.Destroy;
begin
FMyForm.WindowProc:= FDefMyFormProc;
FMyForm.Release;
inherited Destroy;
end;
procedure TMyComponent.MyFormProc(var Message: TMessage);
begin
with Message do
if (Msg = CM_PARENTFONTCHANGED) and (WParam = 0) then
FMyForm.Font.Assign(Application.MainForm.Font) else
FDefMyFormProc(Message);
end;
訂正です。
× if (Msg = CM_PARENTFONTCHANGED) and (WParam = 0) then
○ if (Msg = CM_PARENTFONTCHANGED) and FMyForm.ParentFont and (WParam = 0) then
ツイート | ![]() |