【问题标题】:Delphi - Create a custom TToolBar componentDelphi - 创建自定义 TToolBar 组件
【发布时间】:2011-07-10 09:56:39
【问题描述】:

我想创建一个自定义工具栏控件(后代 TToolBar),它应该有一些默认工具栏按钮。

所以我创建了一个简单的构造函数,它创建了 1 个默认按钮:

constructor ZMyToolbart.Create(AOwner: TComponent);
var
  ToolButton : TToolButton;
begin
  inherited;
  Parent := Owner as TWinControl;
  ToolButton := TToolButton.Create(Self);
  ToolButton.Parent := Self;
  ToolButton.Caption := 'Hallo';
end;

问题是,在窗体上拖动自定义控件后,工具栏按钮是可见的,但它没有作为工具栏的一部分显示在对象检查器中。

如果尝试通过工具栏的按钮属性分配按钮,但这不起作用。 也许有人有一个建议如何做到这一点?谢谢!

【问题讨论】:

  • 工具栏是使用起来比较复杂的控件之一。如果我是你,我会倾向于把它放在一个框架中并以这种方式重复使用。
  • 您确定要在此处使用工具栏吗?使用具有位图纹理和一些速度按钮的面板会简单得多,也许可以将其设为框架,因此您可以轻松地重复使用它。我会写一个组件,但是我已经这样做了很多年了。如果你是新手,也许你应该考虑一个框架。

标签: delphi


【解决方案1】:

如果您将工具栏设置为工具按钮的所有者,则需要有一个已发布的属性才能在对象检查器中设置其属性。这也使得以后可以释放它。您的代码示例中的局部变量表明情况并非如此。

type
  ZMyToolbart = class(TToolbar)
  private
    FHalloButton: TToolButton;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property HalloButton: TToolButton read FHalloButton write FHalloButton;
  end;

constructor ZMyToolbart.Create(AOwner: TComponent);
begin
  inherited;
  Parent := Owner as TWinControl;
  FHalloButton := TToolButton.Create(Self);
  FHalloButton.Parent := Self;
  FHalloButton.Caption := 'Hallo';
end;

destructor ZMyToolbart.Destroy;
begin
  FHalloButton.Free;
  inherited;
end;


这可能不会给你想要的东西,你会在 OI 的子属性中看到按钮的属性,而不是像其他按钮一样。如果您希望您的按钮看起来像普通的工具按钮,请将其所有者设为表单,而不是工具栏。

然后按钮将可以自行选择。这也意味着按钮可能在设计时(以及在运行时)被删除,因此您希望在它被删除时收到通知并将其引用设置为 nil。

最后,您只想在设计时创建按钮,因为在运行时按钮将从 .dfm 流式创建,然后您将拥有两个按钮。

别忘了注册按钮类:

type
  ZMyToolbart = class(TToolbar)
  private
    FHalloButton: TToolButton;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  protected
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  end;

[...]
constructor ZMyToolbart.Create(AOwner: TComponent);
begin
  inherited;
  Parent := Owner as TWinControl;
  if Assigned(FHalloButton) then
    Exit;

  if csDesigning in ComponentState then begin
    FHalloButton := TToolButton.Create(Parent);
    FHalloButton.Parent := Self;
    FHalloButton.FreeNotification(Self);
    FHalloButton.Caption := 'Hallo';
  end;
end;

destructor ZMyToolbart.Destroy;
begin
  FHalloButton.Free;
  inherited;
end;

procedure ZMyToolbart.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if (AComponent = FHalloButton) and (Operation = opRemove) then
    FHalloButton := nil;
end;

initialization
  RegisterClass(TToolButton);

【讨论】:

    【解决方案2】:

    似乎 ToolButton 的所有者应该是表单本身而不是工具栏。 将代码更改为以下代码时,工具按钮会显示在对象检查器的工具栏下方:

    constructor ZMyToolbart.Create(AOwner: TComponent);
    var
      ToolButton : TToolButton;
    begin
      inherited;
      Parent := Owner as TWinControl;
      ToolButton := TToolButton.Create(Self.Parent);
      ToolButton.Parent := Self;
      ToolButton.Caption := 'Hallo';
    end;
    

    【讨论】:

      猜你喜欢
      • 2013-12-08
      • 1970-01-01
      • 2011-03-06
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      相关资源
      最近更新 更多