BACK to articles

OTA - How to create and add new menu item to Delphi IDE

by Miha Remec
New menu item within Delphi IDE

Note: This example is a simplified version of New Unit expert and taken from mrExperts, set of experts for Delphi. More information is available from mrExperts' homepage.

How to create and add new menu item to Delphi IDE

1. declare private variables
  FMainMenu: TMainMenu;
  FFileMenu: TMenuItem;
  FNewMenuItem: TMenuItem;
2. find original Delphi menu items
  // find Delphi's main menu
  FMainMenu := (BorlandIDEServices as INTAServices).MainMenu;
  // find File
  FFileMenu := FMainMenu.Items.Find('File');
3. create new menu item and associate OnClick event handler
  FNewMenuItem := TMenuItem.Create(FFileMenu);
  FNewMenuItem.Caption := 'mrExperts';
  FNewMenuItem.OnClick := OnMenuItemClick;
4. add new menu item into File menu, before the first separator
  // find first separator
  for i := 0 to FFileMenu.Count - 1 do begin
    if FFileMenu.Items[i].IsLine then begin
      FFileMenu.Insert(i, FNewMenuItem);
      Break;
    end;
  end;

How to destroy and remove new menu item from Delphi IDE

1. remove new menu item
  i := 0;
  while i < FFileMenu.Count do begin
    if AnsiSameCaption(FFileMenu.Items[i].Caption, 'mrExperts') then
      FFileMenu.Remove(FFileMenu.Items[i])
    else
      Inc(i);
  end;
2. free new menu item
  FNewMenuItem.Free;

Download the source code

Full source is available for download.


Document created: 2000-08-24
Last revised: 2000-11-19

BACK to articles