Trying to figure out the Delphi Open Tools API can be a daunting task. Here is a bit of code to get a menu item into the Delphi IDE's main menu. Please note that I didn't test this code and you will want to reorganize it a bit.
unit ComStdReg;
interface
uses Menus, ActnList;
type
TMyMenus = class
private
FMenuItem: TMenuItem;
FMenuAction: TAction;
procedure DoExecute(Sender: TObject);
procedure DoUpdate(Sender: TObject);
public
constructor Create;
destructor Destroy; override;
end;
implementation
uses ToolsAPI;
constructor TMyMenus.Create;
var
MenuService: INTAServices;
begin
MenuService := BorlandIDEServices as INTAServices;
if MenuService <> nil then
begin
MenuItem := TMenuItem.Create(nil);
MenuAction := TAction.Create(nil);
MenuItem.Action := MenuAction;
MenuItem.Name := 'MyMenuItem';
MenuActiond.Caption := 'My Menu Item';
MenuAction.OnExecute := DoExecute;
MenuAction.OnUpdate := DoUpdate;
MenuAction.Enabled := False;
// Only set this if you want to allow F1 when the mouse if over the menu item.
MenuAction.HelpContext := 0;
// Tell the menu service which item to place your menu item after. This will
// place your menu item after Component | Installed Packages.
MenuService.AddActionMenu('ComponentPaletteItem', MenuAction,
MenuAction);
MenuService.UpdateMenuAccelerators(MenuService.MainMenu);
end;
end;
destructor TComMenus.Destroy;
begin
FMenutItem.Free;
FMenuAction.Free;
inherited;
end;
procedure TComMenus.DoExecute(Sender: TObject);
begin
// DoExecute is called when the the menu item is clicked.
end;
procedure TMyMenus.DoUpdate(Sender: TObject);
begin
// DoUpdate is called when the menu is displayed.
// Here you will want to set FMenuAction.Enabled to True or False or
// FMenuAction.Visible to True or False. I personally don't like it when menu
// items hide so don't change visible.
end;
7 comments:
Hi Chris,
where do I get those 'menu item' names from like 'ComponentPaletteItem'?
cu,
Michael
Michael,
Take a look at the MainMenu property off INTAServices.
Hi Chris,
Using Turbo Delphi Pro I certainly have got this MainMenu property of INTAServices, but none of the describing comments mentions the names of the menu items.
So where did you find "ComponentPaletteItem'?
cu,
Michael
Hi Michael,
From there use the Names and Items property as if you were spelunking through a TMainMenu as you normally would if you wanted to know the names of everything.
Hint, one recursive function will get you the name of ever menu item.
Hi Chris,
thanks for your reply! I saw this answer coming ;-)
I hoped however there is a more convenient way e.g. looking up in a document or so.
cu,
Michael
Hi, Is there a way, by which i can get the position of an item in a menu. I need this because I need to change menu items' positions dynamically at runtime (up and down the menu according to how often an item is ised). Thanks
Hi Anonymous,
Don't do that! There is a lot of research where UI elements should not move because users use them based on location not what they say. It's like muscle memory. Remember that whole Customized Menus thing that Microsoft came up with in Office? It didn't work, everyone turned it off.
Post a Comment