Delphi-创建一个自定义的TToolBar组件 [英] Delphi - Create a custom TToolBar component

查看:57
本文介绍了Delphi-创建一个自定义的TToolBar组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义工具栏控件(后代TToolBar),该控件应具有一些default-toolbarButtons.

I want to create a custom toolbar control (descendant TToolBar) which should have some default-toolbarButtons.

因此,我创建了一个简单的构造函数,该构造函数创建了1个默认按钮:

So I've created a simple constructor which creates 1 default button:

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;

问题是,拖动表单上的自定义控件后,工具栏按钮可见,但是在对象检查器中并未显示为工具栏的一部分.

The problem is, after draggign the custom control on a form, the toolbar-button is visible, but it's not shown in the object inspector as a part of the toolbar.

如果尝试通过工具栏的button属性分配按钮,但这不起作用.也许有人对如何做到这一点有意见?谢谢!

If tried to assign the button via the button property of the toolbar, but this doesn't work. Maybe somebody has an advice how this could be done? thank you!

推荐答案

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

If you make the toolbar the owner of the tool button, you need to have a published property to be able to set its properties in the object inspector. This will also make it possible to free it later. The local variable in your code sample suggests this is not the case.

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的子属性中看到按钮的属性,这与其他按钮不同.如果您希望按钮像普通工具按钮一样显示,请使其成为表单的所有者,而不是工具栏的所有者.


This, probably, will not give you what you want, you'll see the button's properties in a sub-property in the OI, not like other buttons. If you want your button to appear like ordinary tool buttons, make its owner the form, not the toolbar.

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

Then the button will be selectable on its own. This also means the button might be deleted at design-time (as well as at run time), hence you'd want to be notified when it's deleted and set its reference to nil.

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

Finally, you only want to create the button at design-time, since at run-time the button will be stream-created from the .dfm and then you'll have two buttons.

别忘了注册按钮类:

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

这篇关于Delphi-创建一个自定义的TToolBar组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆