应该很简单但我看不见。
您可以找到正确单击以显示弹出菜单的组件:
PopupMenu1.PopupComponent但是,如何找到包含TMenuItem的弹出菜单,然后单击该菜单?
若要将问题简化为示例:
我有一系列标签,每个标签都有不同的标题,还有一个弹出式菜单,分配给每个标签的PopupMenu属性。
当有人右键单击其中一个标签并弹出弹出式菜单,然后单击MenuItem1时,我要编写代码:
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处理程序中。
发布于 2011-05-28 10:10:18
我对你的问题有点困惑,但既然你排除了其他的一切,我只能想象你在寻找TMenuItem.GetParentMenu。
发布于 2011-05-28 10:57:20
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;https://stackoverflow.com/questions/6160888
复制相似问题