基本的示例和解释

This commit is contained in:
taynpg 2024-01-25 14:14:28 +08:00
parent 5cfc4e3bd8
commit 9afb2bfe4d
5 changed files with 193 additions and 16 deletions

View File

@ -16,9 +16,9 @@ find_package(wxWidgets REQUIRED COMPONENTS core base)
include(${wxWidgets_USE_FILE}) include(${wxWidgets_USE_FILE})
add_executable(wxWidgetStudy main.cpp) add_executable(wxWidgetStudy main.cpp)
add_executable(wxEvent event_01_exe.cpp src/event_01.cpp)
target_link_libraries(wxWidgetStudy PRIVATE ${wxWidgets_LIBRARIES}) target_link_libraries(wxWidgetStudy PRIVATE ${wxWidgets_LIBRARIES})
target_link_libraries(wxEvent PRIVATE ${wxWidgets_LIBRARIES})
set_target_properties(wxWidgetStudy set_target_properties(wxWidgetStudy PROPERTIES WIN32_EXECUTABLE TRUE)
PROPERTIES set_target_properties(wxEvent PROPERTIES WIN32_EXECUTABLE TRUE)
WIN32_EXECUTABLE TRUE
)

4
event_01_exe.cpp Normal file
View File

@ -0,0 +1,4 @@
#include "src/event_01.h"
IMPLEMENT_APP(MyApp)
DECLARE_APP(MyApp)

120
main.cpp
View File

@ -1,20 +1,57 @@
// Name: minimal.cpp /*
// Purpose: Minimal wxWidgets sample
// Author: Julian Smart 1. main函数或者winmain函数或者其它类似的函数被调用(
wxWidgets提供的,).wxWidgets MyApp的
.
2. wxWidgets调用MyApp::OnInit函数, MyFrame的实例.
3. MyFrame的构造函数通过它的基类wxFrame的构造函数创建一个窗口
.
4. MyApp::OnInit函数显示主窗口并且返回真.
5. wxWidgets开始事件循环.
退退
*/
#include "wx/wx.h" #include "wx/wx.h"
/*
wxWidgets程序都需要定义一个wxApp类的子类
wxApp的子类至少需要定义一个OnInit函数wxWidgets准备好运行
Win32程序中的main函数或者WinMain函数类似
*/
// 定义应用程序类 // 定义应用程序类
class MyApp : public wxApp { class MyApp : public wxApp {
public: public:
// 这个函数将会在程序启动的时候被调用 /*
OnInit函数中
.wxWidgets将开始事件循环用来处理用户输入并且在必要的情
OnInit函数返回假 wxWidgets将会释放它内部已经分配的资源
*/
virtual bool OnInit(); virtual bool OnInit();
/*
OnExit 便退
OnInit
*/
}; };
// 定义主窗口类
/*
Frame窗口是一个可以容纳别的窗口的顶级窗口
*/
class MyFrame : public wxFrame { class MyFrame : public wxFrame {
public: public:
// 主窗口类的构造函数 // 主窗口类的构造函数
MyFrame(const wxString& title); MyFrame(const wxString& title);
// 事件处理函数
/*
MyFrame类中不是虚函数
*/
void OnQuit(wxCommandEvent& event); void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event); void OnAbout(wxCommandEvent& event);
@ -23,22 +60,54 @@ private:
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
}; };
// 有了这一行就可以使用 MyApp& wxGetApp()了 /*
DECLARE_APP(MyApp) DECLARE_APPwxWidgets就不知道怎样创建一个新的应用程序对象
// 告诉wxWidgets主应用程序是哪个类 使
*/
IMPLEMENT_APP(MyApp) IMPLEMENT_APP(MyApp)
/*
wxWidgets创建这个MyApp类的实例的时候wxTheApp.
使wxApp到MyApp的类型强制转换
wxGetApp()MyApp实例的引用
便
使DECLARE_APP,wxTheApp变量调用wxApp的方法.
MyApp的头文件
*/
DECLARE_APP(MyApp)
// 初始化程序 // 初始化程序
bool MyApp::OnInit() bool MyApp::OnInit()
{ {
// 创建主窗口
/*
wxT Unicode模式_T宏的作用是完全一样的使
"_()" wxWidgets将其中的字符串翻译成其它语言的版本
*/
MyFrame* frame = new MyFrame(wxT("Minimal wxWidgets App")); MyFrame* frame = new MyFrame(wxT("Minimal wxWidgets App"));
// 显示主窗口 // 显示主窗口
frame->Show(true); frame->Show(true);
// 开始事件处理循环 // 开始事件处理循环
return true; return true;
} }
// MyFrame类的事件表
/*
.cpp文件wxWidgets来自用户或者其它地方的事件
EVT_MENU宏只是很多中事件宏的其中之一wxWidgets哪种事件应该被关联到哪个成员函数
wxID_EXIT wxID_ABOUT wxID_ABOUT和wxID_EXIT是wxWidgets
*/
BEGIN_EVENT_TABLE(MyFrame, wxFrame) BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(wxID_ABOUT, MyFrame::OnAbout) EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
EVT_MENU(wxID_EXIT, MyFrame::OnQuit) EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
@ -48,14 +117,41 @@ void MyFrame::OnAbout(wxCommandEvent& event)
{ {
wxString msg; wxString msg;
msg.Printf(wxT("Hello and welcome to %s"), wxVERSION_STRING); msg.Printf(wxT("Hello and welcome to %s"), wxVERSION_STRING);
// 四个参数分别代表消息内容,标题,窗口类型以及父窗口。
wxMessageBox(msg, wxT("About Minimal"), wxOK | wxICON_INFORMATION, this); wxMessageBox(msg, wxT("About Minimal"), wxOK | wxICON_INFORMATION, this);
} }
void MyFrame::OnQuit(wxCommandEvent& event) void MyFrame::OnQuit(wxCommandEvent& event)
{ {
// 释放主窗口 /*
wxFrame类的 Close frame窗口 wxEVT_CLOSE_WINDOW
wxWindow::Destroy frame
OnQuit OnQuit
wxWidgets会通过 Close OnQuit中的那样 frame wxEVT_CLOSE_WINDOW
frame
*/
Close(); Close();
} }
/*
wxFrame使(NULL),
(wxID_ANY标识让 wxWidgets自己选择一个)
wxFrame::Create函数来创建一个frame窗口的实例
XPM格式来表示XPM文件其实是一个ASCII编码的完全符合C++
C++
SetIcon那一行代码使用 mondrian_xpm
mondrian变量是在mondrian.xpm文件里定义的 frame窗口关联在一起
Append函数的三个参数的意义分别为
"&"线"\t"
*/
MyFrame::MyFrame(const wxString& title) : wxFrame(NULL, wxID_ANY, title) MyFrame::MyFrame(const wxString& title) : wxFrame(NULL, wxID_ANY, title)
{ {
// 设置窗口图标 // 设置窗口图标

46
src/event_01.cpp Normal file
View File

@ -0,0 +1,46 @@
#include "event_01.h"
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
END_EVENT_TABLE()
bool MyApp::OnInit()
{
MyFrame* frame = new MyFrame(wxT("Minimal wxWidgets App"));
frame->Show(true);
return true;
}
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();
}
MyFrame::MyFrame(const wxString& title) : wxFrame(NULL, wxID_ANY, title)
{
// 设置窗口图标
// 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!"));
}

31
src/event_01.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef EVENT_CHAPTER_ONE
#define EVENT_CHAPTER_ONE
#include "wx/wx.h"
/*
GUI程序都是单线程的
GUI编程架构用不同的方法将它内部的事件处理机制展现给程序开发者wxWidgets来说
*/
class MyApp : public wxApp {
public:
virtual bool OnInit();
};
class MyFrame : public wxFrame {
public:
MyFrame(const wxString& title);
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
private:
DECLARE_EVENT_TABLE()
};
#endif