弹出式菜单点击源于什么组件?

问题描述 投票:20回答:4

有一个弹出菜单连接到表单上的几个组件(按钮,但也有像TCharts这样的东西),我想知道是哪个组件首先被右键单击以启动弹出菜单。

click方法的Sender参数只是指向TMenuItem,也就是它的父级弹出菜单(或父级菜单项)。

如何获取始发组件?

delphi contextmenu
4个回答
37
投票

你是指PopupMenu1.PopupComponent吗?


10
投票

你可以通过以下方法在一个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; 

0
投票

作为最后的手段,你可以使用 Mouse.CursorPosTPopupMenu.OnPopup 在表单中找到这个组件。但可能还有一个更好的方法。


0
投票

PopUpMenu.PopupComponent 表示最后一次响应鼠标右键显示弹出菜单的组件。

© www.soinside.com 2019 - 2024. All rights reserved.