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.

解决方案 »

  1.   

    TrackPopupMenu  有返回值的,你可以看下,那样写我测试是没问题的
    Popup其实也是调用TrackPopupMenu  这个API
      

  2.   

    我知道你原因了,
    因为你没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;
      

  3.   

    嗯,我是看过Popup的代码,可是想不明白为什么就不能响应事件。
      

  4.   


    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;你把代码改成这样就可以了 
      

  5.   

    就是点菜单项的事件啊,让它执行PopupMenuItemsClick,但是很没任何反应