Delphi菜单选择

发布于 2024-12-07 22:48:02 字数 155 浏览 0 评论 0原文

我的问题是如何捕获以某种形式按下了哪个菜单项?举个例子:

我有一个带有按钮的表单。当我按下按钮时,应用程序的菜单将成为焦点,并且子窗体等待选择菜单项。选择一个后,子窗体会显示一条消息,其中包含我按下的菜单项的名称。

谁能告诉我该怎么做?

提前致谢!

My question is how to catch which menu item was pressed in some form? For an example :

I have a form with a button. When I pressed the button the menu of the application will be on focus and the child form wait to choose a menu item. After I choose one the child form show a message with the name of the menu item which I pressed.

Can anyone tell me how to do this?

Thanks in advance!

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

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

发布评论

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

评论(2

许久 2024-12-14 22:48:02

这样的事情是对您的问题的直接回答:

procedure TMyForm.MenuItemClick(Sender: TObject);
begin
  ShowMessage((Sender as TMenuItem).Caption);
end;

此事件处理程序应该连接到您希望以这种方式表现的每个菜单项。


您似乎想要对菜单项的执行进行一些集中日志记录或监视。如果您使用操作并将它们与菜单项相关联,那么您可以获得应用程序范围内的通知,表明操作已执行。将 TApplicationEvents 对象拖放到主窗体上并处理其 OnActionExecute 事件。像这样:

procedure TMyForm.ApplicationEvents1ActionExecute(Action: TBasicAction; var Handled: Boolean);
begin
  ShowMessage((Action as TAction).Caption);
end;

每当您的应用程序中执行任何事件时,都会触发该事件。

Something like this is a direct answer to your question:

procedure TMyForm.MenuItemClick(Sender: TObject);
begin
  ShowMessage((Sender as TMenuItem).Caption);
end;

This event handler should be connected to each menu item that you wish to behave this way.


It seems that you want some centralised logging or monitoring of the execution of menu items. If you use actions and associate these with your menu items then you can get an application wide notification that an action has been executed. Drop a TApplicationEvents object onto your main form and handle its OnActionExecute event. Like this:

procedure TMyForm.ApplicationEvents1ActionExecute(Action: TBasicAction; var Handled: Boolean);
begin
  ShowMessage((Action as TAction).Caption);
end;

This will fire whenever any event in your app is executed.

如梦亦如幻 2024-12-14 22:48:02
  • 1/ 默认情况下,您将所有 TMenuItem OnClick 事件处理程序设置为 Nil。
  • 2/ 当您单击表单按钮时,您为每个 TMenuItem 分配一个事件,该事件将仅被调用一次,并将记录“触发”。
  • 3/ 当该事件被调用时,您将所有 TMenuitem.OnClick 重置为 Nil
  • 4/ 为了使这更容易,您将所有 MenuItem 存储在 TList 中。

示例:

全局变量(TMyForm 中的私有声明):

MyTriggerItem: TMenuItem; // used as pointer
MyMenuItemList: TList; // used to store all TMenuItem which are 'listened to'

您的 TButton 处理程序:

Procedure TMyForm.ButtonClick(Sender: TObject);
Var
  i: Integer;
Begin
  For i:= 0 To Pred(MyMenuItemList.Count) Do TMenuItem(MyMenuItemList[i]).OnCLick := CommonMenuItemClick;    
End;        

您的 TMenuItem 事件处理程序:

Procedure TMyForm.CommonMenuItemClick(Sender: TObject);
Var
  i: Integer;
Begin
  MyTriggerItem := TMenuItem(Sender);
  For i:= 0 To Pred(MyMenuItemList.Count) Do TMenuItem(MyMenuItemList[i]).OnCLick := Nil;
End;
  • 1/ By default you set all the TMenuItem OnClick event handler to Nil.
  • 2/ When you click the form button you assign an event to each TMenuItem, this event will be only called once, and will record the 'trigger'.
  • 3/ When the event is called you reset all the TMenuitem.OnClick to Nil
  • 4/ To make this easyer, you store all your MenuItems in a TList.

example:

global variables (private declaration in TMyForm):

MyTriggerItem: TMenuItem; // used as pointer
MyMenuItemList: TList; // used to store all TMenuItem which are 'listened to'

your TButton handler:

Procedure TMyForm.ButtonClick(Sender: TObject);
Var
  i: Integer;
Begin
  For i:= 0 To Pred(MyMenuItemList.Count) Do TMenuItem(MyMenuItemList[i]).OnCLick := CommonMenuItemClick;    
End;        

your TMenuItem event handler:

Procedure TMyForm.CommonMenuItemClick(Sender: TObject);
Var
  i: Integer;
Begin
  MyTriggerItem := TMenuItem(Sender);
  For i:= 0 To Pred(MyMenuItemList.Count) Do TMenuItem(MyMenuItemList[i]).OnCLick := Nil;
End;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文