wxWidgets hello world 教程展示了一种奇怪的行为。只要我使用以下行将面板添加到应用程序到 MyFrame 构造函数:
wxPanel *panel = new wxPanel(this);
[Return] 和 [Keypad Enter] 键导致程序退出(检测到清洁关闭事件)。
为什么是这样?没有 wxPanel,按键什么都不做。
这是带有添加行的代码:
// hworld.cpp
// Version using dynamic event routing
#include <wx/wx.h>
class MyApp : public wxApp
{
virtual bool OnInit();
};
IMPLEMENT_APP(MyApp)
class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
};
enum
{
ID_Quit=1,
ID_About
};
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame( _("Hello World"), wxPoint(50, 50),
wxSize(450, 350));
frame->Connect( ID_Quit, wxEVT_COMMAND_MENU_SELECTED,
(wxObjectEventFunction) &MyFrame::OnQuit );
frame->Connect( ID_About, wxEVT_COMMAND_MENU_SELECTED,
(wxObjectEventFunction) &MyFrame::OnAbout );
frame->Show(true);
SetTopWindow(frame);
return true;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame( NULL, -1, title, pos, size )
{
wxMenuBar *menuBar = new wxMenuBar;
wxMenu *menuFile = new wxMenu;
menuFile->Append( ID_About, _("&About...") );
menuFile->AppendSeparator();
menuFile->Append( ID_Quit, _("E&xit") );
menuBar->Append(menuFile, _("&File") );
// Added line causing failure.
wxPanel *panel = new wxPanel(this);
SetMenuBar(menuBar);
CreateStatusBar();
SetStatusText( _("Welcome to wxWidgets!") );
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(true);
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox( _("wxWidgets Hello World example."),
_("About Hello World"),
wxOK|wxICON_INFORMATION, this );
}