弹出菜单点击源自哪个组件

发布于 2024-08-26 04:33:24 字数 141 浏览 4 评论 0原文

将弹出菜单附加到表单上的多个组件(按钮,还有 TCharts 等),我想知道首先右键单击哪个组件来启动弹出菜单。

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

如何获取原始组件?

Having a popup menu attached to several components on a form (buttons, but also things like TCharts), I would like to know which component was right clicked to start the popup menu in the first place.

The Sender parameter of the click method just points to the TMenuItem, its parent to the popup menu (or the parenting menu item).

How do I get the originating component?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

溺ぐ爱和你が 2024-09-02 04:33:24

您的意思是 PopupMenu1.PopupComponent 吗?

Did you mean PopupMenu1.PopupComponent ?

云巢 2024-09-02 04:33:24

在 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; 

You can get the caller component within the click event of the TMenuItem of a PopupMenu by

Caller := ((Sender as TMenuItem).GetParentMenu as TPopupMenu).PopupComponent;

An Example of a PopupMenu which is assigned to several list boxes and solves the export to file functionality:

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; 
一笑百媚生 2024-09-02 04:33:24

作为最后的手段,您可以使用 TPopupMenu.OnPopup 中的 Mouse.CursorPos 在表单上查找此组件。但可能有更好/更简单的方法。

As a last resort you can use Mouse.CursorPos in TPopupMenu.OnPopup to find this component on a form. But there is probably a better/easier way.

顾忌 2024-09-02 04:33:24

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

PopUpMenu.PopupComponent indicates the component that last displayed the popup menu in response to the right mouse click

莫多说 2024-09-02 04:33:24

我有一堆面板,我想让用户右键单击这些面板中的任何一个并执行“删除文件”操作。
因此,我有一个与所有这些面板关联的弹出菜单。
这就是我如何找出哪个面板被右键单击:(

注意:我添加了很多注释来清楚地解释它是如何工作的。但是如果您不喜欢它,您可以将代码压缩为 2 行(请参阅第二个过程) )。

因此,如果您为该弹出菜单分配了操作:

procedure Tfrm.actDelExecute(Sender: TObject);
VAR
  PopMenu: TPopupMenu;
  MenuItem: TMenuItem;
  PopupComponent: TComponent;
begin
 { Find the menuitem associated to this action }
 MenuItem:= TAction(Sender).ActionComponent as TMenuItem;  { This will crash and burn if we call this from a pop-up menu, not from an action! But we always use actions, so.... }

 { Was this action called by keyboard shortcut? Note: in theory there should be no keyboard shortcuts for this action if the action can be applyed to multiple panels. We can call this action ONLY by selecting (right click) a panel! }
 if MenuItem = NIL then
  begin
   MsgError('This action should not be called by keyboard shortcuts!');
   EXIT;
  end;

 { Find to which pop-up menu this menuitem belongs to }
 PopMenu:= (MenuItem.GetParentMenu as TPopupMenu);

 { Find on which component the user right clicks }
 PopupComponent := PopMenu.PopupComponent;

 { Finally, access that component }
 (PopupComponent as TMonFrame).Delete(FALSE);
end;

如果您只有一个简单的弹出菜单(未分配操作):

procedure Tfrm.actDelHddExecute(Sender: TObject);
VAR PopupComponent: TComponent;
begin
 PopupComponent := ((Sender as TMenuItem).GetParentMenu as TPopupMenu).PopupComponent;
 (PopupComponent as TMonFrame).Delete(TRUE);
end;

您可以放置​​所有代码在返回 TPanel 的单个函数中并像这样调用它:

procedure Tfrm.actDelWallExecute(Sender: TObject);
begin
 if GetPanelFromPopUp(Sender) <> NIL
 then GetPanelFromPopUp(Sender).Delete(FALSE);
end;

I have a bunch of panels and I want to let the user right click any of those panels and perform a "delete file" action.
So, I have a single pop-up menu associated with all those panels.
This is how I find out which panel was right clicked:

(Note: I put lots of comments to clearly explain how it works. But if you don't like it, you can compactify the code to 2 lines (see the second procedure)).

So, if you have actions assigned to that pop-up menu:

procedure Tfrm.actDelExecute(Sender: TObject);
VAR
  PopMenu: TPopupMenu;
  MenuItem: TMenuItem;
  PopupComponent: TComponent;
begin
 { Find the menuitem associated to this action }
 MenuItem:= TAction(Sender).ActionComponent as TMenuItem;  { This will crash and burn if we call this from a pop-up menu, not from an action! But we always use actions, so.... }

 { Was this action called by keyboard shortcut? Note: in theory there should be no keyboard shortcuts for this action if the action can be applyed to multiple panels. We can call this action ONLY by selecting (right click) a panel! }
 if MenuItem = NIL then
  begin
   MsgError('This action should not be called by keyboard shortcuts!');
   EXIT;
  end;

 { Find to which pop-up menu this menuitem belongs to }
 PopMenu:= (MenuItem.GetParentMenu as TPopupMenu);

 { Find on which component the user right clicks }
 PopupComponent := PopMenu.PopupComponent;

 { Finally, access that component }
 (PopupComponent as TMonFrame).Delete(FALSE);
end;

If you only have a simple pop-up menu (no actions assigned):

procedure Tfrm.actDelHddExecute(Sender: TObject);
VAR PopupComponent: TComponent;
begin
 PopupComponent := ((Sender as TMenuItem).GetParentMenu as TPopupMenu).PopupComponent;
 (PopupComponent as TMonFrame).Delete(TRUE);
end;

You can put all that code in a single function that returns a TPanel and call it like this:

procedure Tfrm.actDelWallExecute(Sender: TObject);
begin
 if GetPanelFromPopUp(Sender) <> NIL
 then GetPanelFromPopUp(Sender).Delete(FALSE);
end;
抱着落日 2024-09-02 04:33:24

对于 Firemonkey 框架,相同函数的工作方式如下,以 PopupMenu 为例,该菜单分配给多个列表框并解决导出到文件功能:

procedure TForm1.mniExportFileClick(Sender: TObject);
var Caller: TPopupMenu;
begin
  Caller := (((Sender as TMenuItem).parent) as TContent).parent as TPopupMenu;
  (Caller.PopupComponent as TListBox).Items.SaveToFile(...);
end; 
  

For the Firemonkey framework the same function works as follows for an example of a PopupMenu which is assigned to several list boxes and solves the export to file functionality:

procedure TForm1.mniExportFileClick(Sender: TObject);
var Caller: TPopupMenu;
begin
  Caller := (((Sender as TMenuItem).parent) as TContent).parent as TPopupMenu;
  (Caller.PopupComponent as TListBox).Items.SaveToFile(...);
end; 
  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文