How To…Customize Designer Menu and Toolbars

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:

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;