我有一个包含两个自定义选项的菜单:选项 A 和选项 B
我的目标是当单击菜单栏上的选项A时获得两个工具书选项,A1和A2
这是我的代码(主要取自 wxWidgets 存储库中的最小样本):
class MyApp : public wxApp
{
public:
bool OnInit() override;
};
wxIMPLEMENT_APP(MyApp);
class MyFrame : public wxFrame
{
public:
MyFrame();
private:
void OnHello(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void onOptionA(wxCommandEvent& event);
wxPanel* panel = new wxPanel(this);
wxToolbook* tabControl1 = new wxToolbook(panel, wxID_ANY, {10, 10}, {370, 250}, /*wxTBK_BUTTONBAR|*/wxTBK_HORZ_LAYOUT);
wxNotebookPage* tabA1 = new wxNotebookPage(tabControl1, wxID_ANY);
wxNotebookPage* tabA2 = new wxNotebookPage(tabControl1, wxID_ANY);
wxDECLARE_EVENT_TABLE();
};
enum
{
ID_Hello = 1,
ID_OptionA = 2
};
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_OptionA, MyFrame::onOptionA)
wxEND_EVENT_TABLE()
MyFrame::MyFrame()
: wxFrame(nullptr, wxID_ANY, "MyPrj")
{
if (wxPlatformInfo::Get().GetOperatingSystemFamilyName() == "Windows") tabControl1->SetImageList(new wxImageList(16, 16));
else if (wxPlatformInfo::Get().GetOperatingSystemFamilyName() == "Macintosh") tabControl1->SetImageList(new wxImageList(32,
32));
else tabControl1->SetImageList(new wxImageList(24, 24));
wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
"Help string shown in status bar for this menu item");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
wxMenu *menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append(menuFile, "&File");
menuBar->Append(menuHelp, "&Help");
wxMenu *menuOptionA = new wxMenu;
wxMenu *menuOptionB = new wxMenu;
menuBar->Append(menuOptionA, "&A");
menuBar->Append(menuOptionB, "&B");
SetMenuBar( menuBar );
CreateStatusBar();
SetStatusText("Welcome to myPrj!");
Bind(wxEVT_MENU, &MyFrame::OnHello, this, ID_Hello);
Bind(wxEVT_MENU, &MyFrame::OnAbout, this, wxID_ABOUT);
Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
Bind(wxEVT_MENU, [&](wxCommandEvent& event) {
tabControl1->AddPage(tabA1, "A1", true, 0);
tabControl1->AddPage(tabA2, "A2", false, 1);
}, ID_OptionA);
}
void MyFrame::OnExit(wxCommandEvent& event)
{
Close(true);
}
void MyFrame::onOptionA(wxCommandEvent& event)
{
tabControl1->AddPage(tabA1, "A1", true, 0);
tabControl1->AddPage(tabA2, "A2", false, 1);
}
void MyFrame::OnAbout(wxCommandEvent& event)
{
wxMessageBox("This is a wxWidgets Hello World example",
"About Hello World", wxOK | wxICON_INFORMATION);
}
void MyFrame::OnHello(wxCommandEvent& event)
{
wxLogMessage("Hello world from wxWidgets!");
}
与:
Bind(wxEVT_MENU, [&](wxCommandEvent& event) {
tabControl1->AddPage(tabA1, "A1", true, 0);
tabControl1->AddPage(tabA2, "A2", false, 1);
}, ID_OptionA);
单击菜单上的选项A时,我没有看到两个工具菜单选项A1和A2
如果我删除
ID_OptionA
:
Bind(wxEVT_MENU, [&](wxCommandEvent& event) {
tabControl1->AddPage(tabA1, "A1", true, 0);
tabControl1->AddPage(tabA2, "A2", false, 1);
});
当单击“文件”->“Hello”时,我奇怪地得到了两个工具书选项 A1 和 A2
我必须添加和/或修改什么才能使单击菜单上的“选项 A”时出现 A1 和 A2?
首先,正如评论中已经指出的,您需要使用 either
Bind()
xor 事件表,但不能在同一窗口中使用两者,因为这最多会造成混乱。
其次,处理菜单事件时确实需要指定ID。如果您在没有它的情况下调用
Bind()
,则您将在处理程序中处理所有菜单事件,这可能不是您想要的(尽管您可以执行此操作,然后检查处理程序内的 event.GetId()
来决定如果您真的需要做什么通缉)。如果您使用 ID 调用 Bind()
,如您所示,它确实有效,并且如果您没有其他任何东西连接到它(并且首先执行),您的事件处理程序将被调用。