【问题标题】:TFont Property to control font of subclassed controlsTFont 属性控制子类控件的字体
【发布时间】:2015-07-27 20:55:31
【问题描述】:

我创建了一个源自 TPanel 的组件。在组件的构造函数中,我创建了几个 TButton 组件。我创建并显示了一个 TFont 类型的 ButtonFont 属性。该属性控制组件上所有按钮的字体。示例:

TMyPanel = Class(TPanel)
private
    FButtonFont      : TFont;
    FExampleButton   : TButton;
    procedure SetButtonFont(Value: TFont);    
public
    property ButtonFont: TFont read FButtonFont write SetButtonFont;
    constructor Create (AOwner: TComponent); override;
end;        

constructor TMyPanel.Create (AOwner: TComponent);
begin
  FButtonFont := TFont.Create;
  FExampleButton := TButton.Create(self);
  FExampleButton.Parent := self;
  .......
  inherited;
end;

procedure TMyPanel.SetButtonFont(Value: TFont);

begin
  FButtonFont.Assign(Value);
  FExampleButton.Font := Value;
end;

以下将导致所有子类按钮的按钮字体发生变化:

MyLabel.Font.Size := 22; 
MyPanel.ButtonFont := label1.font;

我可以看到正在调用 SetButtonFont 方法。

我怎样才能得到这样的东西来导致所有子类按钮改变它们的字体大小:

MyPanel.ButtonFont.Size := 22;

【问题讨论】:

  • 继承的应该是构造函数的第一行

标签: delphi


【解决方案1】:

为字体的OnChange 事件分配一个处理程序并更新该处理程序中所有子控件的字体:

constructor TMyPanel.Create(AOwner: TComponent);
begin
  inherited;
  FButtonFont := TFont.Create;
  FButtonFont.OnChange := ButtonFontChanged; // <-- here
  FExampleButton := TButton.Create(Self);
  FExampleButton.Parent := Self;
  ...
end;

destructor TMyPanel.Destroy;
begin
  ...
  FButtonFont.Free;
  inherited;
end;


procedure TMyPanel.ButtonFontChanged(Sender: TObject);
begin
  FExampleButton.Font := FButtonFont;
  ...
end;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多