Toolbar Control

Hierarchy: TObject - TPersistent - TComponent - TControl - TWinControl - TToolWindow - TToolBar

This provides access to the Win32 common control, allowing construction of a toolbar with its own buttons and other facilities. In the following example a toolbar has been added, which appears along the top of the form. The main area of the form is taken up by a RichEdit control, whose Alignment is set to alClient so it fills the window below the toolbar. 

To add a toolbar double click the icon in the Win32 palette. An action list has been added to the project so that actions can be assigned to the buttons on the toolbar. An image list has also been added from the Win32 palette so that images can be added to the toolbar buttons. 

Key properties for a Toolbar:

Buttonwidth: sets width of buttons on a toolbar.

Flat: if set to true renders the toolbar and its buttons transparent, something which may be desirable when the Toolbar is used in a Coolbar or a Control Bar. This also turns on the alternative images available through the HotImages property

HotImages: makes a second list of images available for use when the mouse moves over a button. 

List: changes the way text and images align on buttons: when set to False images are centred and text is underneath; when set to True images are aligned to the left with text on the right.

ShowCaptions: shows captions on toolbar when set to True.

To add a toolbutton to a toolbar:

In this example three Edit buttons have been added, Copy, Cut and Paste, three file buttons for New, Open and Save, a Print button, a Font Dialog button plus Bold and Italic buttons, and an Exit button. 

The Edit buttons are linked to standard actions through the Action Manager.

The file buttons are linked to simple file operations:

New:

procedure TForm1.ToolButton6Click(Sender: TObject);
begin
 RichEdit1.Text := '';
 FileName := '';
 Caption := Application.Title + ' - [Untitled]';
end;

Open:

procedure TForm1.ToolButton13Click(Sender: TObject);
begin
 if OpenDialog1.Execute then
  begin
   Filename := OpenDialog1.FileName;
   RichEdit1.Lines.LoadFromFile (FileName);
   Caption := Application.Title + ' - ' + FileName;
   RichEdit1.ReadOnly := ofReadOnly in
   OpenDialog1.Options;
  end;
end;

Save:

procedure TForm1.ToolButton7Click(Sender: TObject);
begin
 if SaveDialog1.Execute then
  begin
   Filename := SaveDialog1.FileName;
   RichEdit1.Lines.SaveToFile (FileName);
  end;
end;

A list of fonts has been added in a combo box:

procedure TForm1.ComboBox1Change(Sender: TObject);
var fontnumber:integer;
begin
 fontnumber:=combobox1.ItemIndex;
 richedit1.Font.Name:=combobox1.Items[fontnumber];
end;

A ColorBox (Additional Palette) could also be added in the same way.

The Fonts button opens the Font Dialog button and sets properties of RichEdit.SelAttributes::

procedure TForm1.ToolButton12Click(Sender: TObject);
begin
 if fontdialog1.Execute then
  with RichEdit1.SelAttributes do
   begin
    Style:=fontdialog1.Font.Style;
    Color:=fontdialog1.Font.Color;
    Name:=fontdialog1.Font.Name;
    Size:=fontdialog1.Font.Size;
   end;
end;

The Bold and Italic buttons are simple:

procedure TForm1.ToolButton14Click(Sender: TObject);
begin
 RichEdit1.SelAttributes.Style:=[fsBold];
end;

procedure TForm1.ToolButton15Click(Sender: TObject);
begin
 RichEdit1.SelAttributes.Style:=[fsItalic];
end;

The Print button is also simple:

procedure TForm1.ToolButton9Click(Sender: TObject);
begin
 RichEdit1.Print (FileName);
end;

Refinements to this code would include a Status Bar, switching on toolbar hints and checking to see if a file had been modified and saved before a new file was created or opened. To switch on toolbar hints select the toolbar and set the ShowHints property to True. Then, for each button or control, you can define the text of the hint in the Hint property. 

Toolbars can also be seen as 'containers' for 'CoolBars' and 'ControlBars', where a number of toolbars can be placed to produce Windows-style applications. (Microsoft use the Coolbar in Internet Explorer but applications such as Excel and Word have toolbars that are further customised.)

One property of interest to builders of CoolBars and ControlBars is transparency - set this to 'true' to make the area around the buttons transparent so that any bitmap underneath is visible.

Another property for fine-tuning a toolbar is the Edge Property, whereby each of four possible edges can be set to visible. The default is for only the top border to be visible.

Back to Tutorial

Back to Palette List