Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
How to customize Toolbars at runtime Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Delphi 4.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Jonas Bilinkevicius

Professional applications like Word or Excel let the user design the toolbars and 
let create new toolbars at runtime. This is't usually done with a little customize 
dialog and drag & drop functions.


Professional applications like Word or Excel let the user design the toolbars and 
let create new toolbars at runtime. This ist usually done with a little customize 
dialog and drag & drop functions. This sample shows you, how to get this 
functionality simple into your program.

First of all, you must make some decissions to your program design. All functions, 
that can be put on a toolbar must defined in an Actionlist, because we need a list 
of functions that can be asigned to the toolbuttons. To minimize this work an 
Actionlist is the right thing, because wie can walk through the functions, have an 
automatic integration of the images for the toolbuttons via the imagelist, 
centralized shortcuts and have unique names for the functions to save and restore 

The next thing is, that you should save and restore the customized toolbars to an 
inifile or the registry. This is done by the TFiletoolbar component included with 
this sample. Note, that you must assign the Actionlist to TFiletoolbars to get the 
save/restore work.

The customize dialog is built with formstyle fsStayOnTop, so the window is on top 
of our application. The form is displayed with the Show methode, because we must 
interact in the mainform for doing Drag & Drop.

For further details look at the TFiletoolbar documentation and to the source of the 
demo project. In the source you find detailed comments to anything that ist 
neccesary for get it working in your application.

Download the whole sample project including TFiletoolbar component for saving and 
restoring Toolbars.

TToolbar97 component pack by Jordan Russel, Version 1.75 or higher available from (Freeware for non commercial use) is needed.

Example of Drag&Drop functions from Sample project:
2   //---------------------------------------------------------------
3   // Drag & Drop of Toolbar Buttons
4   // These functions are plugged to the OnDragOver, OnDragDrop and
5   // OnDragEnd property of any toolbar and any button on it.
6   // By user defined buttons and toolbars this is done automatic,
7   // by predefined toolbars you must plug in the functions in
8   // Objectinspector
9   //---------------------------------------------------------------
11  procedure TForm1.ToolbarDragOver(Sender, Source: TObject; X,
12    Y: Integer; State: TDragState; var Accept: Boolean);
13  // This function must be plugged to the OnDragOver property of
14  // any toolbars and toolbuttons
15  //
16  // Check if Dropping is allowed
17  begin
18    if Source is TListView then
19      // Docking allowed only from CustomizeDialog Command Listview
20      Accept := (Source as TListView).Name = CustomizeDialog.Commands.Name;
21  end;
23  procedure TForm1.ToolbarDragDrop(Sender, Source: TObject; X, Y: Integer);
24  // This function must be plugged to the OnDragDrop property of
25  // any toolbars and toolbuttons
26  //
27  // Handler for TToolbutton97 objects for dropping from CustomizeDialog
28  var
29    t: TListView;
30    act: TAction;
31    ti: TListItem;
32    tb, tb2: TToolbarButton97;
33    ftb: TFileToolbar;
34    oi: Integer;
35  begin
36    if Source is TListView then
37    begin // Is it from the Listview?
38      t := Source as TListView;
39      if Assigned(t) then
40      begin
41        // Get the selected item, it holds the desired action
42        ti := t.Selected;
43        if Assigned(ti) then
44        begin
45          // The action is stored in the data property of the item
46          act := ti.Data;
47          if Assigned(act) then
48          begin
49            // create a toolbutton on the fly
50            tb := TToolbarButton97.Create(self);
51            if Assigned(tb) then
52            begin
53              tb.Images := ImageList1; // Assign the Imagelist
54              tb.DisplayMode := dmGlyphOnly; // display only the image
55              // mode is turned to dmManual when leaving the
56              // CustomizeDialog but to handle Drag&Drop, it must be
57              // dmAutomatic by now
58              tb.DragMode := dmAutomatic;
59              tb.Action := act; // the desired Action
60              // Assign the handlers
61              tb.OnDragDrop := ToolbarDragDrop;
62              tb.OnDragOver := ToolbarDragOver;
63              tb.OnEndDrag := ToolbarEndDrag;
64              // Hints like parent
65              tb.ParentShowHint := True;
66              // Look for the place to add the button on the toolbar
67              ftb := nil;
68              oi := x div tb.Width;
69              // Convert Position from pixel to number of buttons
70              if Sender is TFileToolbar then
71              begin
72                // dropped direct to the toolbar?
73                ftb := Sender as TFileToolbar;
74              end
75              else if Sender is TToolbarButton97 then
76              begin
77                // placed on an other button?
78                tb2 := Sender as TToolbarButton97;
79                if Assigned(tb2) then
80                begin
81                  // Get parent and Orderindex of the button
82                  ftb := tb2.Parent as TFileToolbar;
83                  oi := ftb.OrderIndex[tb2];
84                end;
85              end;
86              if Assigned(ftb) then
87              begin // We have a parent...
88                // generate a unique name for the button
89                tb.Name := 'tbB' + CustomizeDialog.DateStamp;
90                // Insert the button on the toolbar
91                ftb.InsertControl(tb);
92                ftb.OrderIndex[tb] := oi; // and set the Orderindex
93              end;
94            end;
95          end;
96        end;
97      end;
98    end;
99  end;
101 procedure TForm1.ToolbarEndDrag(Sender, Target: TObject; X, Y: Integer);
102 // This function must be plugged to the OnEndDrag property of any
103 // toolbutton. The toolbars must not have this, because you cant
104 // throw them out of the program...
105 //
106 // Handler for TToolbarbutton97 objects to throw 'em out
107 // of the Toolbar
108 var
109   tb: TToolbarButton97;
110   ftb: TFileToolbar;
111 begin
112   if not Assigned(Target) then
113   begin
114     // No target > so throw the button away
115     tb := Sender as TToolbarButton97;
116     if Assigned(tb) then
117     begin
118       ftb := tb.Parent as TFileToolbar;
119       // Delete the button
120       if Assigned(ftb) then
121         ftb.RemoveControl(tb);
122     end;
123   end;
124 end;

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC