(deleted old - ugly answer completely)
It's pretty easy actually - once Remy tells what to do (see comments).
Here's the menu structure for the below sample
File1 Edit1
FileItem11 EditItem11
FileItem21 EditItem21
and two popup menu items. The code:
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
File1: TMenuItem;
FileItem11: TMenuItem;
FileItem21: TMenuItem;
Edit1: TMenuItem;
EditItem11: TMenuItem;
EditItem21: TMenuItem;
PopupMenu1: TPopupMenu;
PopupItem11: TMenuItem;
PopupItem21: TMenuItem;
procedure PopupItem11Click(Sender: TObject);
procedure PopupItem21Click(Sender: TObject);
private
FSelectedItem: TMenuItem;
FTracking: Boolean;
procedure MenuRButtonUp(var Msg: TMessage); message WM_MENURBUTTONUP;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.MenuRButtonUp(var Msg: TMessage);
var
Cmd: UINT;
begin
if not FTracking then
FSelectedItem :=
MainMenu1.FindItem(GetMenuItemID(Msg.LParam, Msg.WParam), fkCommand);
if (not FTracking) and (FSelectedItem <> nil) then begin
FTracking := True;
LongBool(Cmd) := TrackPopupMenuEx(PopupMenu1.Handle,
TPM_RECURSE or TPM_BOTTOMALIGN or TPM_RETURNCMD,
Mouse.CursorPos.X, Mouse.CursorPos.Y, Handle, nil);
FTracking := False;
if Cmd <> 0 then
PopupMenu1.DispatchCommand(Cmd);
end;
inherited;
end;
procedure TForm1.PopupItem11Click(Sender: TObject);
begin
Caption := 'Popup Item 1 clicked on ' + FSelectedItem.Caption;
end;
procedure TForm1.PopupItem21Click(Sender: TObject);
begin
// whatever..
Caption := 'Popup Item 2 clicked on ' + FSelectedItem.Caption;
end;
Sertac Akyuz
2010-09-24 14:58:26