您的位置:首页 > 其它

一个基本的wxWidgets程序

2016-04-08 10:30 295 查看
转自Cross Platform Gui Programming

配置:win10+msvc2013+wxWidgets3.1.0

#include<wx/wx.h>

class MyApp : public wxApp
{
public:
virtual bool OnInit();
};

class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
private:
wxDECLARE_EVENT_TABLE();
};

//声明可以使用全局的指针MyApp& wxGetApp();
DECLARE_APP(MyApp);

//事件映射表;
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(wxID_ABOUT,MyFrame::OnAbout)    //菜单项关联到事件处理函数;
EVT_MENU(wxID_EXIT,MyFrame::OnQuit)
wxEND_EVENT_TABLE()

//主程序入口;
wxIMPLEMENT_APP(MyApp);

//初始化定义;
bool MyApp::OnInit()
{
//MyFrame *frame = new MyFrame("Hello World", wxPoint(50, 50), wxSize(450, 340));
MyFrame *frame = new MyFrame("Hello World", wxDefaultPosition, wxDefaultSize);
frame->Show(true);
return true;
}

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, wxID_ANY, title, pos, size)
{
//创建菜单项;
wxMenu *fileMenu = new wxMenu;
fileMenu->Append(wxID_ABOUT);
wxMenu* helpMenu = new wxMenu;
helpMenu->Append(wxID_EXIT);

//创建菜单条;
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append(fileMenu, "&File");
menuBar->Append(helpMenu, "&Help");
SetMenuBar(menuBar);

//创建状态栏;
CreateStatusBar();
SetStatusText("Welcome to wxWidgets!");
}

void MyFrame::OnQuit(wxCommandEvent& event)
{
Close();
}

void MyFrame::OnAbout(wxCommandEvent& event)
{
wxString msg;
msg.Printf(wxT("hello and welcome to %s"), wxVERSION_STRING);
wxMessageBox(msg, wxT("about Minimal"), wxOK | wxICON_INFORMATION, this);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: