C++ 动态 WX 菜单绑定 [英] C++ Dynamic WX Menu Bindings

查看:29
本文介绍了C++ 动态 WX 菜单绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 wxMenu,并且想要一种处理事件的方法.我真的只需要处理单击菜单项时发生的事件的东西.具体来说,给定一个任意函数指针(形式为 void(*)(void)),我想让它在给定事件发生时调用该函数指针.

I am working with wxMenu, and want a a way to handle events. I really only need something that handles events that happen when you click on a menu item. Specifically, given an arbitrary function pointer (of the form void(*)(void)), I want to make it so that that function pointer is called when the given event happens.

菜单不会提前知道,所以我理解的事件表已经出来了.

The menus will not be known in advance, so event tables as I understand them are out.

我找到了绑定和连接,但似乎都不起作用(另见 this 用于连接).

I found Bind and Connect, but neither seems to work (also see this for Connect).

添加代码:

class Menu : public wxMenu {
    public:
        void handle_event(wxCommandEvent& event) {
            volatile int i = 6; //to prevent this method being optimized out
            //breakpoint here that never gets hit
            //selecting a callback to call (based on event.GetID()) could go here
        }
};

//Later, in a function (menu is instance of Menu)
menu->Connect(
    wxEVT_COMMAND_MENU_SELECTED,
    (wxObjectEventFunction)(&Menu::handle_event),
    NULL,menu
);
//Also tried:
menu->Bind(wxEVT_COMMAND_MENU_SELECTED,&(Menu::handle_event),menu);

推荐答案

第一条评论:您的命名约定非常混乱:Menu 是 wxMenu 的子类,并且有一个名为 menu 的实例.如果我必须维护这个代码,我会发疯.

First comment: Your naming convention is terribly confusing: Menu is a subclass of wxMenu and has an instance called menu. If I had to maintain this code, I would go mad.

第二条评论:您到底想在这里实现哪些正常"方式无法实现的目标?

Second comment: What exactly are you trying to achieve here that you can't do with the 'normal' way of doing this?

通常的方法是这样的:

向标准 wxMenu 类的实例添加选项

Add options to an instance of the standard wxMenu class

myMenu.Append(IDM_Option1,"Option 1");
...
myMenu.Append(IDM_OPtionN,"OPtion N");

绑定事件

   Bind( wxEVT_COMMAND_MENU_SELECTED, &myWindow::OnMenuEvent, this, 
            IDM_Option1, IDM_OptionN );

处理事件

  myWindow::OnMenuEvent( wxCommandEvent& event )
  {
      switch ( event.GetId() ) {
           case IDM_Option1
           ...

这可以处理大多数情况,而无需您对 wxMenu 进行子类化.

This handles most every situation without requiring you to subclass wxMenu.

这篇关于C++ 动态 WX 菜单绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆