type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure PopupMenuItemsClick(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
PopupMenu1: TPopupMenu;
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
procedure TForm1.AddMenuItemClick(Sender: TObject);
var
index: Integer;
NewItem: TMenuItem;
begin
for index := 0 to 3 do
begin
NewItem := TMenuItem.Create(PopupMenu1);
PopupMenu1.Items.Add(NewItem);
NewItem.Caption := 'Menu Item ' + IntToStr(index);
NewItem.Tag := index;
NewItem.OnClick := PopupMenuItemsClick;
end;
end;procedure TForm1.PopupMenuClick(Sender: TObject);
var
PT: TPoint;
begin
pt.X := button2.Left;
pt.Y := button2.Top + button2.Height;
PT := ClientToScreen(PT);
TrackPopupMenu (PopupMenu1.Handle,
TPM_LEFTALIGN + TPM_LEFTBUTTON + TPM_RETURNCMD,
pt.x, pt.y, 0, Handle, nil);
// PopupMenu1.Popup(PT.X, PT.Y);// 使用这行代码点击菜单项就有反应。
end;procedure TForm1.FormCreate(Sender: TObject);
begin
PopupMenu1 := TPopupMenu.Create(self);
end;procedure TForm1.PopupMenuItemsClick(Sender: TObject);
begin
with Sender as TMenuItem do
begin
case Tag of
0: ShowMessage('first item clicked');
1: ShowMessage('second item clicked');
2: ShowMessage('third item clicked');
3: ShowMessage('fourth item clicked');
end;
end;
end; end.
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure PopupMenuItemsClick(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
PopupMenu1: TPopupMenu;
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
procedure TForm1.AddMenuItemClick(Sender: TObject);
var
index: Integer;
NewItem: TMenuItem;
begin
for index := 0 to 3 do
begin
NewItem := TMenuItem.Create(PopupMenu1);
PopupMenu1.Items.Add(NewItem);
NewItem.Caption := 'Menu Item ' + IntToStr(index);
NewItem.Tag := index;
NewItem.OnClick := PopupMenuItemsClick;
end;
end;procedure TForm1.PopupMenuClick(Sender: TObject);
var
PT: TPoint;
begin
pt.X := button2.Left;
pt.Y := button2.Top + button2.Height;
PT := ClientToScreen(PT);
TrackPopupMenu (PopupMenu1.Handle,
TPM_LEFTALIGN + TPM_LEFTBUTTON + TPM_RETURNCMD,
pt.x, pt.y, 0, Handle, nil);
// PopupMenu1.Popup(PT.X, PT.Y);// 使用这行代码点击菜单项就有反应。
end;procedure TForm1.FormCreate(Sender: TObject);
begin
PopupMenu1 := TPopupMenu.Create(self);
end;procedure TForm1.PopupMenuItemsClick(Sender: TObject);
begin
with Sender as TMenuItem do
begin
case Tag of
0: ShowMessage('first item clicked');
1: ShowMessage('second item clicked');
2: ShowMessage('third item clicked');
3: ShowMessage('fourth item clicked');
end;
end;
end; end.
Popup其实也是调用TrackPopupMenu 这个API
因为你没VCL控件和菜单做关联,直接用API去弹出不行的
但是VCL的Popup方法是可以的,他在里边做了处理
procedure TPopupMenu.Popup(X, Y: Integer);
const
Flags: array[Boolean, TPopupAlignment] of Word =
((TPM_LEFTALIGN, TPM_RIGHTALIGN, TPM_CENTERALIGN),
(TPM_RIGHTALIGN, TPM_LEFTALIGN, TPM_CENTERALIGN));
Buttons: array[TTrackButton] of Word = (TPM_RIGHTBUTTON, TPM_LEFTBUTTON);
var
AFlags: Integer;
begin
FPopupPoint := Point(X, Y);
SetBiDiModeFromPopupControl;
DoPopup(Self);
FItems.InternalRethinkHotkeys(False);
FItems.InternalRethinkLines(False);
FItems.RebuildHandle;
AdjustBiDiBehavior;
AFlags := Flags[UseRightToLeftAlignment, FAlignment] or Buttons[FTrackButton] or
(Byte(FMenuAnimation) shl 10);
TrackPopupMenu(FItems.Handle, AFlags, X, Y, 0 { reserved }, PopupList.Window, nil);
end;
var
PT: TPoint;
AFlags: Integer;
begin pt.X := button1.Left;
pt.Y := button1.Top + button1.Height;
PT := ClientToScreen(PT); AFlags := TPM_LEFTALIGN or TPM_RIGHTBUTTON ;
TrackPopupMenu (PopupMenu1.Handle,
AFlags,
pt.x, pt.y, 0, Handle, nil);
end;你把代码改成这样就可以了