说明:
本文是作者的一点总结,对所学的回顾与记录,如有不妥之处,欢迎大家指正,并且讨论交流。
一个典型的GUI程序所做的事情包括:显示控件窗口,在窗口上绘图或图像,响应来自鼠标,键盘或者其他设备的输入,和其他进程通信,调用其他应用程序等
每个应用程序都有入口点,也就是程序开始执行的地方,wxWidgets通过继承wxApp来实现,必须重载其bool OnInit()函数,返回值为true。
/**********************************
*文件名:MainApp.h
*实现文件:MainApp.cpp
*创建时间:2019年12月30日
*作者:李欢
*描述:程序入口
*其他:......
***********************************/
#include <wx/app.h>
class MainApp:public wxApp
{
public:
virtual bool OnInit();
virtual int OnExit();
DECLARE_APP(MianApp)
};
OnInit()函数中应该至少有一个窗口实例,初始化应用程序的一些数据,
返回为真开始时间循环,返回为假释放资源退出。
/************************************
* 文件名:MainApp.cpp
* 声明文件:MainApp.h
* 其他:
************************************/
#include “MainApp.h”
IMPLEMENT_APP(MainApp)
bool MainApp::OnInit()
{
MainFrame *frame =new MainFrame(NULL);
frame->Show(); //显示
frame->Centre(); //居中
return true;
}
int MainApp::OnExit()
{
return wxApp::OnExit();
}
设计一个窗口类,一般为顶层窗口,wxFrame与wxDialog;添加各种控件,GUI界面以及事件处理函数.此为一个简单的实现。
//文件名;MainFrame.h
#include <wx/frame.h>
class MainFrame : public wxFrame
{
public:
MainFrame(wxWindow *parent);
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
DECLARE_EVENT_TABLE()
};
#include “MainFrame.h”
#include <wx/wx.h>
BEGIN_EVENT_TABLE(MainFrame, wxFrame)
EVT_MENU(wxID_ABOUT, MainFrame::OnAbout)
EVT_MENU(wxID_EXIT, MainFrame::OnQuit)
END_EVENT_TABLE()
MainFrame::MainFrame(wxWindow *parent)
: wxFrame(parent, wxID_ANY, "test")
{
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!”));
}
void MainFrame::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 MainFrame::OnQuit(wxCommandEvent& event)
{
Close();
}
尽信书不如无书,多看多实践。