Re: Delphi Popup Menus

Posted by Dimi on August 31, 2005

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

: 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);
  NewItem: TMenuItem;
  NewItem := TMenuItem.Create(Self);
  NewItem.Caption := Capt;
  NewItem.OnClick := MenuItemClick;

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);
  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...