Re: Delphi Popup Menus

Posted by Dimi on August 31, 2005

In Reply to: Delphi Popup Menus posted by Andrew on August 28, 2005

: Hello...is it possible to create items in a Delphi popup menu while the program is running?Also those items created will contain a ShellExecute.

: Thank you in advance
: Andrew

Easy part: adding item to PopupMenu

procedure TForm1.AddToPopupMenu(PUMenu: TPopupMenu; Capt: string);
var
  NewItem: TMenuItem;
begin
  NewItem := TMenuItem.Create(Self);
  NewItem.Caption := Capt;
  NewItem.OnClick := MenuItemClick;
  PUMenu.Items.Add(NewItem);
end;

Example: you have PopupMenu1 and you want to add item 'Option 1'

AddToPopupMenu(PopupMenu1, 'Option 1');

Difficult part: procedure MenuItemClick. It must exist already when you execute AddToPopupMenu! It must do a different thing for each MenuItem, so for example look at Caption of the MenuItem, or maybe look at number of MenuItem

procedure TForm1.MenuItemClick(Sender: TObject);
begin
  if (Sender as TMenuItem).Caption = 'Option 1' then
    // ... do something
  else if (Sender as TMenuItem).Caption = 'Option 2' then
    // ... do something
  else // and so on...
end; 
 

Related Articles and Replies