大家好,又见面了,我是你们的朋友全栈君。
wxWidgets
wxWidgets是一个用来编写C++程序的GUI(图形用户界面)工具包。它是一个开源的、成熟的、跨平台的工具包。wxWidgets应用程序能在所有主流的操作系统上运行,Windows,Unix,Mac。这个项目由Julian Smart在1992年启动。wxWidgets提供各种各样的C++类来处理数据流、数据库、多线程、在线帮助、应用程序设置。wxWidgets由大量的窗口小部件组成。
接下来我们一步步的完成一个Hello World的窗口程序:
首先,搭建一个非常简单的框架:
1 #include <wx/wx.h> 2 3 class MyFrame : public wxFrame 4 { 5 public: 6 MyFrame(const wxString& title): wxFrame(NULL, wxID_ANY, title) {} 7 8 }; 9 10 class MyApp : public wxApp 11 { 12 public: 13 virtual bool OnInit() 14 { 15 MyFrame * pframe = new MyFrame(wxT("Hello World")); 16 pframe->Show(true); 17 return true; 18 } 19 20 }; 21 IMPLEMENT_APP(MyApp)
执行结果:
特点:
- 没有
main()
函数,实际上是包含在wxWidgets框架中看不到。 - 一个应用程序类
App
,一个框架类Frame
。 Frame
在App
的OnInit()
函数中实例化。注意:这里Frame没有销毁,可能会内存泄露(通常这么处理也不会有太大问题,因为窗口关闭时,OS会收回所有资源)。
然后给窗口添加状态栏
1 #include <wx/wx.h> 2 3 //框架类wxFrame 4 class MyFrame : public wxFrame 5 { 6 public: 7 MyFrame(const wxString& title): wxFrame(NULL, wxID_ANY, title) 8 { 9 //添加状态栏 10 CreateStatusBar(); 11 //将状态栏分为两栏 12 //CreateStatusBar(2); 13 SetStatusText(wxT("Welcome to wxWidgets!")); 14 } 15 16 }; 17 18 //应用程序类APP 19 class MyApp : public wxApp 20 { 21 public: 22 //Frame的实例化 23 virtual bool OnInit() 24 { 25 MyFrame * pframe = new MyFrame(wxT("Hello World")); 26 pframe->Show(true); 27 return true; 28 } 29 30 }; 31 //声明应用程序 32 IMPLEMENT_APP(MyApp)
添加菜单
1 #include <wx/wx.h> 2 3 //框架类wxFrame 4 class MyFrame : public wxFrame 5 { 6 public: 7 MyFrame(const wxString& title): wxFrame(NULL, wxID_ANY, title) 8 { 9 //定义菜单 10 wxMenu *menuFile = new wxMenu; 11 menuFile->Append(wxID_EXIT, wxT("Exit ... \tAlt+X"), wxT("Quit this program")); 12 13 wxMenu *menuHelp = new wxMenu; 14 menuHelp->Append(wxID_ABORT, wxT("&About ... \tF1"), wxT("Show about frame")); 15 16 //定义菜单栏 17 wxMenuBar *menuBar = new wxMenuBar; 18 19 //向菜单栏添加菜单 20 menuBar->Append(menuFile, wxT("&File")); 21 menuBar->Append(menuHelp, wxT("&Help")); 22 23 //将菜单栏添加到wxFrame中 24 SetMenuBar(menuBar); 25 26 //添加状态栏 27 CreateStatusBar(); 28 //将状态栏分为两栏 29 //CreateStatusBar(2); 30 SetStatusText(wxT("Welcome to wxWidgets!")); 31 } 32 33 }; 34 35 //应用程序类APP 36 class MyApp : public wxApp 37 { 38 public: 39 //Frame的实例化 40 virtual bool OnInit() 41 { 42 MyFrame * pframe = new MyFrame(wxT("Hello World")); 43 pframe->Show(true); 44 return true; 45 } 46 47 }; 48 //声明应用程序 49 IMPLEMENT_APP(MyApp)
添加菜单事件、添加应用显示图标
1 #include <wx/wx.h> 2 #include "icon.xpm" 3 4 //框架类wxFrame 5 class MyFrame : public wxFrame 6 { 7 public: 8 MyFrame(const wxString& title): wxFrame(NULL, wxID_ANY, title) 9 { 10 //定义菜单 11 wxMenu *menuFile = new wxMenu; 12 menuFile->Append(wxID_EXIT, wxT("Exit ... \tAlt+X"), wxT("Quit this program")); 13 14 wxMenu *menuHelp = new wxMenu; 15 menuHelp->Append(wxID_ABOUT, wxT("&About ... \tF1"), wxT("Show about frame")); 16 17 //定义菜单栏 18 wxMenuBar *menuBar = new wxMenuBar; 19 20 //向菜单栏添加菜单 21 menuBar->Append(menuFile, wxT("&File")); 22 menuBar->Append(menuHelp, wxT("&Help")); 23 24 //将菜单栏添加到wxFrame中 25 SetMenuBar(menuBar); 26 27 //添加状态栏 28 CreateStatusBar(); 29 //将状态栏分为两栏 30 //CreateStatusBar(2); 31 SetStatusText(wxT("Welcome to wxWidgets!")); 32 33 //设置应用显示图标 34 SetIcon(wxIcon(icon_xpm)); 35 36 } 37 38 //定义事件处理函数 39 void OnQuit(wxCommandEvent& event); 40 void OnAbout(wxCommandEvent& event); 41 private: 42 //声明事件表 43 DECLARE_EVENT_TABLE() 44 }; 45 46 //应用程序类APP 47 class MyApp : public wxApp 48 { 49 public: 50 //Frame的实例化 51 virtual bool OnInit() 52 { 53 MyFrame * pframe = new MyFrame(wxT("Hello World")); 54 pframe->Show(true); 55 return true; 56 } 57 58 }; 59 //声明应用程序 60 IMPLEMENT_APP(MyApp) 61 62 //定义事件表,完成事件和处理函数的映射 63 BEGIN_EVENT_TABLE(MyFrame, wxFrame) 64 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout) 65 EVT_MENU(wxID_EXIT, MyFrame::OnQuit) 66 END_EVENT_TABLE() 67 68 //事件处理函数的实现 69 void MyFrame::OnAbout(wxCommandEvent& event) 70 { 71 wxString msg; 72 msg.Printf(wxT("About hello wxWidgets"), wxVERSION_STRING); 73 74 wxMessageBox(msg, wxT("About wxWidgets"), wxOK | wxICON_INFORMATION, this); 75 } 76 77 void MyFrame::OnQuit(wxCommandEvent& event) 78 { 79 Close(); 80 }
icon.xpm
1 /* XPM */ 2 static const char * icon_xpm[] = { 3 /* columns rows colors chars-per-pixel */ 4 "32 32 6 1", 5 " c black", 6 ". c navy", 7 "X c red", 8 "o c yellow", 9 "O c gray100", 10 "+ c None", 11 /* pixels */ 12 "++++++++++++++++++++++++++++++++", 13 "++++++++++++++++++++++++++++++++", 14 "++++++++++++++++++++++++++++++++", 15 "++++++++++++++++++++++++++++++++", 16 "++++++++++++++++++++++++++++++++", 17 "++++++++ ++++++++++", 18 "++++++++ ............ ++++++++++", 19 "++++++++ ............ ++++++++++", 20 "++++++++ .OO......... ++++++++++", 21 "++++++++ .OO......... ++++++++++", 22 "++++++++ .OO......... ++++++++++", 23 "++++++++ .OO...... ", 24 "++++++++ .OO...... oooooooooooo ", 25 " .OO...... oooooooooooo ", 26 " XXXXXXX .OO...... oOOooooooooo ", 27 " XXXXXXX .OO...... oOOooooooooo ", 28 " XOOXXXX ......... oOOooooooooo ", 29 " XOOXXXX ......... oOOooooooooo ", 30 " XOOXXXX oOOooooooooo ", 31 " XOOXXXXXXXXX ++++ oOOooooooooo ", 32 " XOOXXXXXXXXX ++++ oOOooooooooo ", 33 " XOOXXXXXXXXX ++++ oOOooooooooo ", 34 " XOOXXXXXXXXX ++++ oooooooooooo ", 35 " XOOXXXXXXXXX ++++ oooooooooooo ", 36 " XXXXXXXXXXXX ++++ ", 37 " XXXXXXXXXXXX ++++++++++++++++++", 38 " ++++++++++++++++++", 39 "++++++++++++++++++++++++++++++++", 40 "++++++++++++++++++++++++++++++++", 41 "++++++++++++++++++++++++++++++++", 42 "++++++++++++++++++++++++++++++++", 43 "++++++++++++++++++++++++++++++++" 44 };
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/155292.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...