我有一个带有两个自定义选项的菜单:选项A和选项B
我的目标是在单击菜单栏上的选项A时获得两个工具书选项,A1和A2
这是我的代码(主要取自wxWidgets repo中的最小样本):
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);
});
型
我奇怪地得到两个工具书选项A1和A2时,点击文件->你好
我需要添加和/或修改什么,以便在单击菜单上的“选项A”时显示A1和A2?
1条答案
按热度按时间g6ll5ycj1#
首先,正如在注解中已经指出的,您需要使用 either
Bind()
xor 事件表,但不能在同一个窗口中使用这两个表,因为这充其量是令人困惑的。其次,在处理菜单事件时确实需要指定ID。(尽管您可以这样做,然后在处理程序中检查
event.GetId()
,以决定如果您真的需要做什么)。它确实工作,如果你没有其他东西连接到它(并且首先执行),你的事件处理程序将被调用。