开发者

Delphi - Create a custom TToolBar component

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

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.

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!


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;


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.

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.

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.

And don't forget to register the button class:

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


it seems that the owner of the ToolButton should be the form itself instead of the toolbar. When changing the code to the following the ToolButton is displayed under the ToolBar in the object inspector:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜