wxWidgets框架例子
1
#include "wx/wx.h"2

3
// 定义应用程序类4
class MyApp : public wxApp5


{6
public:7
virtual bool OnInit();8
};9

10
// 定义主窗口类11
class MyFrame : public wxFrame12


{13
public:14
// 主窗口类的构造函数15
MyFrame(const wxString& title);16

17
// 事件处理函数18
void OnQuit(wxCommandEvent& event);19
void OnAbout(wxCommandEvent& event);20

21
private:22
// 声明事件表23
DECLARE_EVENT_TABLE()24
};25

26
// 有了这一行就可以使用 MyApp& wxGetApp()了27
DECLARE_APP(MyApp)28

29
// 告诉wxWidgets主应用程序是哪个类30
IMPLEMENT_APP(MyApp)31

32
// 初始化程序33
bool MyApp::OnInit()34


{35
// 创建主窗口36
MyFrame *frame = new MyFrame(wxT("Minimal wxWidgets App"));37

38
// 显示主窗口39
frame->Show(true);40

41
// 开始事件处理循环42
return true;43
}44

45
// MyFrame类的事件表46
BEGIN_EVENT_TABLE(MyFrame, wxFrame)47
EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)48
EVT_MENU(wxID_EXIT, MyFrame::OnQuit)49
END_EVENT_TABLE()50

51
void MyFrame::OnAbout(wxCommandEvent& event)52


{53
wxString msg;54
msg.Printf(wxT("Hello and welcome to %s"),55
wxVERSION_STRING);56

57
wxMessageBox(msg, wxT("About Minimal"),58
wxOK | wxICON_INFORMATION, this);59
}60

61
void MyFrame::OnQuit(wxCommandEvent& event)62


{63
// 释放主窗口64
Close();65
}66

67
//#include "mondrian.xpm"68
//69
#include "sample.xpm"70

71
MyFrame::MyFrame(const wxString& title)72
: wxFrame(NULL, wxID_ANY, title)73


{74
// 设置窗口图标75
//SetIcon(wxIcon(mondrian_xpm));76

77
// 创建菜单条78
wxMenu *fileMenu = new wxMenu;79

80
// 添加“关于”菜单项81
wxMenu *helpMenu = new wxMenu;82
helpMenu->Append(wxID_ABOUT, wxT("&About
\tF1"),83
wxT("Show about dialog"));84

85
fileMenu->Append(wxID_EXIT, wxT("E&xit\tAlt-X"),86
wxT("Quit this program"));87

88
// 将菜单项添加到菜单条中89
wxMenuBar *menuBar = new wxMenuBar();90
menuBar->Append(fileMenu, wxT("&File"));91
menuBar->Append(helpMenu, wxT("&Help"));92

93
//
然后将菜单条放置在主窗口上94
SetMenuBar(menuBar);95

96
// 创建一个状态条来让一切更有趣些。97
CreateStatusBar(2);98
SetStatusText(wxT("欢迎使用wxWidgets!"));99
}
浙公网安备 33010602011771号