Code: Select all
//创建一个基本的wxWidgets窗口
#include"wx/wx.h"
#include <wx/dialog.h>
#include<wx/textctrl.h>
class MyApp : public wxApp
{
public:
//入口函数
virtual bool OnInit();
};
//继承wxFrame
class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& label);
//事件处理函数
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void OnSize(wxSizeEvent& event);
void OnButtonOK(wxCommandEvent& event);
void OnTEXTCtrl(wxCommandEvent& event);
void funcName(wxCommandEvent& event);
//增加一段空白
void Add(int width, int height, int strech = 0, int flags = 0, int border = 0);
//增加一段固定大小的空白
void AddSpacer(int sizer);
//增加一个可缩放的空白
void AddStretchSpacer(int stretch = 1);
private:
DECLARE_EVENT_TABLE()
};
//创建MyApp
DECLARE_APP(MyApp)
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
//创建一个继承自wxFrame的窗口对象
MyFrame * frame = new MyFrame(wxT("Minimal wxWidgets App"));
frame->Show(true);
return true;
}
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
EVT_SIZE(MyFrame::OnSize)
EVT_BUTTON(wxID_OK, MyFrame::OnButtonOK)
EVT_TEXT(wxID_ANY, MyFrame::OnTEXTCtrl)
END_EVENT_TABLE()
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);
}
void MyFrame::OnQuit(wxCommandEvent& event)
{
Close();
}
void MyFrame::OnSize(wxSizeEvent& event)
{
}
void MyFrame::OnButtonOK(wxCommandEvent& event)
{
Close();
}
void MyFrame::OnTEXTCtrl(wxCommandEvent& event)
{
wxLogMessage("Hello");
}
MyFrame::MyFrame(const wxString& label = wxEmptyString)
: wxFrame(NULL, wxID_ANY, label)
{
//添加按钮控件
wxBoxSizer*topSizer = new wxBoxSizer(wxVERTICAL);
topSizer->Add(
//创建一个多行文本框
new wxTextCtrl(this, wxID_ANY, " hello",
wxDefaultPosition,wxSize(150,90), wxTE_MULTILINE),
1,
wxEXPAND|
wxALL,
10);
wxBoxSizer*buttonSizer = new wxBoxSizer(wxHORIZONTAL);
buttonSizer->Add(
new wxButton(this, wxID_OK, wxT("button")),
0,
wxALL,
10);
topSizer->Add(
buttonSizer,
0,
wxALIGN_CENTER);
SetSizer(topSizer);
topSizer->Fit(this);
topSizer->SetSizeHints(this);
//SetIcon(wxIcon(mondrian xpm));
//添加菜单控件
wxMenu *fileMenu = new wxMenu;
wxMenu *helpMenu = new wxMenu;
helpMenu->Append(wxID_ABOUT, wxT("&About...\tF1"),
wxT("Show about dialog"));
fileMenu->Append(wxID_EXIT, wxT("E&xit\tAlt−X"),
wxT("Quit this program"));
wxMenuBar *menuBar = new wxMenuBar();
menuBar->Append(fileMenu, wxT("&File"));
menuBar->Append(helpMenu, wxT("&Help"));
SetMenuBar(menuBar);
CreateStatusBar(2);
SetStatusText(wxT("Welcome to wxWidgets!"));
}