TToolButtonCreate (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example creates a ToolBar and adds ToolButtons to it. Include ComCtlrs in the uses clause.

Code

#include <comctrls.hpp>
#include <memory>

void AddButtons(TToolBar *toolBar, TStringList *captions)
{
  for (int i = 0; i < captions->Count; i++)
  {
	TToolButton *button = new TToolButton(toolBar);  // The owner (toolBar) will clean this up.
	button->Parent = toolBar;
	button->Caption = captions->Strings[i];
	if (button->Caption == "|")
	  button->Style = tbsSeparator;
    else
	  button->Style = tbsButton;
  }
}

TToolBar *myToolBar;

__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  myToolBar = new TToolBar(this); // The owner (this) will clean this up.
  myToolBar->Parent = this;
//  TStringList *captions = new TStringList();
  std::auto_ptr<TStringList> captions(new TStringList());

  captions->Add("New");
  captions->Add("Save");
  captions->Add("|");
  captions->Add("Cut");
  captions->Add("Copy");
  captions->Add("Paste");
  AddButtons(myToolBar, captions.get());
//  Delete captions.
  myToolBar->ShowCaptions = True;
  myToolBar->Height = 40;
}

Uses