delphi-如何确定TMenuItem所属的tpopmpMenu

delphi-如何确定TMenuItem所属的tpopmpMenu,delphi,menuitem,right-click,popupmenu,Delphi,Menuitem,Right Click,Popupmenu,应该很简单,但我看不出来 您可以找到右键单击以显示弹出菜单的组件,其中包含: PopupMenu1.PopupComponent 但是,如何找到包含TMenuItem的弹出菜单,而TMenuItem又是在该菜单上单击的 要将问题简化为一个示例,请执行以下操作: 我有一系列标签,每个标签都有不同的标题,我有一个弹出菜单,它被分配给每个标签的PopupMenu属性 当有人右键单击其中一个标签并打开弹出菜单,然后单击菜单项EM1时,我想编码: procedure TForm1.MenuItem1Cl

应该很简单,但我看不出来

您可以找到右键单击以显示弹出菜单的组件,其中包含:

PopupMenu1.PopupComponent
但是,如何找到包含TMenuItem的弹出菜单,而TMenuItem又是在该菜单上单击的

要将问题简化为一个示例,请执行以下操作:

我有一系列标签,每个标签都有不同的标题,我有一个弹出菜单,它被分配给每个标签的PopupMenu属性

当有人右键单击其中一个标签并打开弹出菜单,然后单击菜单项EM1时,我想编码:

procedure TForm1.MenuItem1Click(Sender: TObject);

begin
MsgBox (Format ('The label right-clicked has the caption %', [xxxx.Caption ])) ;
end ;
xxxx应该是什么

实施的答案

感谢两位受访者。我的结局是:

procedure TForm1.MenuItem1Click(Sender: TObject);

var
    AParentMenu : TMenu ;
    AComponent  : TComponent ;
    ALabel      : TLabel ;

begin
AParentMenu := TMenuItem (Sender).GetParentMenu ;
AComponent  := TPopupMenu (AParentMenu).PopupComponent ;
ALabel      := TLabel (AComponent) ;
MsgBox (Format ('The label right-clicked has the caption %', [ALabel.Caption ])) ;
end ;

它还询问了涉及哪个TMenuItem,因此给了我一段代码,我可以将其放入其他OnClick处理程序中,修改更少。

我对您的问题有点困惑,但由于您排除了所有其他问题,我只能想象您正在寻找
TMenuItem.GetParentMenu

,我知道会是这样简单的。。。我在查看TMenuItem的属性,从未想过要查看方法。非常感谢。
procedure TForm1.MenuItem1Click(Sender: TObject);
var pop:TPopupMenu;
    lbl:TLabel;
begin
  // Firstly get parent TPopupMenu (needs casting from TMenu) 
  pop:= TPopupMenu(MenuItem1.GetParentMenu()); 
  // pop.PopupComponent is the "source" control, just cast it to Tlabel
  lbl:= TLabel(pop.PopupComponent);            

  ShowMessage(Format('The label right-clicked has the caption %s',[lbl.Caption]));
end;