Question
“How can I customize the menu and toolbars?”
Solution
The following example shows how to acess the menus and toolbars to add/remove items, hide toolbars, and assign custom event-handlers.
Download: DesignerCustomizeMenusAndToolbars.zip
Sample Delphi code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
uses ppTBX, ppToolbarTBX, ppDesignLayoutManager, ppDesignLayoutMenu, ppDesignToolManagerTBX, ppDesignToolbarsTBX, ppDesignToolActions, ActnList; {****************************************************************************** Example: hide File | Print menu item ******************************************************************************} procedure TForm1.btnHideFilePrintClick(Sender: TObject); var lMenuBar: TppMenuBar; { lFileMenu: TppTBCustomItem;} lDesignerMenu: TppDesignerMenu; begin // each notebook page can have a menu bar, this returns the menu bar for the // active notebook page lMenuBar := ppDesigner1.Form.MainMenu; // one approach is to get the menu item and then index into its items { lFileMenu :=lMenuBar.Items[0]; } // an easier way is to type cast to TppDesignerMenu, which has properties // corresponding to each menu item if (lMenuBar is TppDesignerMenu) then begin lDesignerMenu := TppDesignerMenu(lMenuBar); lDesignerMenu.FileMenu.Print.Visible := False; end; end; {****************************************************************************** Example: add File | Hello menu item ******************************************************************************} procedure TForm1.btnAddFileHelloItemClick(Sender: TObject); var lMenuBar: TppMenuBar; { lFileMenu: TppTBCustomItem; } lDesignerMenu: TppDesignerMenu; lItem: TppTBXItem; liIndex: Integer; begin // each notebook page can have a menu bar, this returns the menu bar for the // active notebook page lMenuBar := ppDesigner1.Form.MainMenu; // one way is to get the menu item { lFileMenu :=lMenuBar.Items[0];} // an easier way is to type cast to TppDesignerMenu, which has properties // corresponding to each menu item if (lMenuBar is TppDesignerMenu) then begin lDesignerMenu := TppDesignerMenu(lMenuBar); // get index of File | Print item liIndex := lDesignerMenu.FileMenu.IndexOf(lDesignerMenu.FileMenu.Print); Inc(liIndex); lItem := TppTBXItem.Create(nil); // create a menu item lItem.Caption := '*** Hello ***'; lItem.OnClick := ehFileHello_Click; lDesignerMenu.FileMenu.Insert(liIndex, lItem); // insert menu item // lDesignerMenu.FileMenu.AddSeparator; // add separator { lItem := lDesignerMenu.FileMenu.AddChildItem;} // add menu item // lItem.Caption := 'Hello'; end; end; procedure TForm1.ehFileHello_Click(Sender: TObject); begin ShowMessage('Hello menu item clicked'); end; {****************************************************************************** Example: hide Toolbar Print button ******************************************************************************} procedure TForm1.btnHideToolbarPrintButtonClick(Sender: TObject); var lLayoutManager: TppDesignLayoutManager; lToolActionManager: TppDesignToolActionManager; lAction: TContainedAction; begin lLayoutManager := ppDesigner1.Form.LayoutManager; // the Layout (i.e. Design workspace) toolbars use the Delphi Action architecture // (except for the component palette toolbars). // the ActionManager, contains properties such as StandardActions, AlignActions, etc. // each of the properties is an ActionList that is implemented by one of the toolbars lToolActionManager := lLayoutManager.ToolActionManager; // the Standard Actions list contains the Print Action lAction := lToolActionManager.StandardActions.GetActionForName('Print'); // set action visible to false TCustomAction(lAction).Visible := False; end; {****************************************************************************** Example: assign PrintPreview tool button event-handler ******************************************************************************} procedure TForm1.btnToolbarButtonEventHandlerClick(Sender: TObject); var lLayoutManager: TppDesignLayoutManager; lToolManager: TppDesignToolManager; lToolbar: TppToolbar; lStdToolbar: TppStandardToolbar; begin lLayoutManager := ppDesigner1.Form.LayoutManager; // get the toolmanager lToolManager := lLayoutManager.ToolManager; // use the ToolBars[] array to access the standard toolbar lToolbar := lToolManager.Toolbars.ItemsByName['Standard']; if (lToolbar = nil) then ShowMessage('Standard toolbar not found') else begin //typecast lStdToolbar := TppStandardToolbar(lToolbar); // assign button event-handler lStdToolbar.PrintPreviewButton.OnClick := ehPrintPreviewButton_Click; // Note: you can also access the Items[] array // lStdToolbar.Items[4].OnClick := ehPrintPreviewButton_Click; end; end; procedure TForm1.ehPrintPreviewButton_Click(Sender: TObject); begin ShowMessage('PreviewButton clicked'); end; {****************************************************************************** Example: Remove the Standard toolbar ******************************************************************************} procedure TForm1.Button2Click(Sender: TObject); var lLayoutManager: TppDesignLayoutManager; lToolManager: TppDesignToolManager; lToolbar: TppToolbar; begin lLayoutManager := ppDesigner1.Form.LayoutManager; // get the toolmanager lToolManager := lLayoutManager.ToolManager; // use the ToolBars[] array to access the standard toolbar lToolbar := lToolManager.Toolbars.ItemsByName['Standard']; // free the toolbar - the tool menu and popupmenu will be updated automatically lToolbar.Free; end; |