有一个弹出菜单连接到表单上的几个组件(按钮,但也有像TCharts这样的东西),我想知道是哪个组件首先被右键单击以启动弹出菜单。
click方法的Sender参数只是指向TMenuItem,也就是它的父级弹出菜单(或父级菜单项)。
如何获取始发组件?
你是指PopupMenu1.PopupComponent吗?
你可以通过以下方法在一个PopupMenu的TMenuItem的点击事件中获得调用组件。
Caller := ((Sender as TMenuItem).GetParentMenu as TPopupMenu).PopupComponent;
一个PopupMenu的例子,它被分配给几个列表框并解决了导出到文件的功能。
procedure TForm1.mniExportFileClick(Sender: TObject);
var Caller: TObject;
begin
if SaveTextFileDialog1.Execute then
begin
Caller := ((Sender as TMenuItem).GetParentMenu as TPopupMenu).PopupComponent;
(Caller as TListBox).Items.
SaveToFile(SaveTextFileDialog1.FileName,
StandardEncodingFromName(
SaveTextFileDialog1.Encodings[SaveTextFileDialog1.EncodingIndex]));
end;
end;
作为最后的手段,你可以使用 Mouse.CursorPos
在 TPopupMenu.OnPopup
在表单中找到这个组件。但可能还有一个更好的方法。
PopUpMenu.PopupComponent 表示最后一次响应鼠标右键显示弹出菜单的组件。