2、wxWidgets介绍–菜单栏、状态栏、图标简介

2、wxWidgets介绍–菜单栏、状态栏、图标简介wxWidgetswxWidgets是一个用来编写C++程序的GUI(图形用户界面)工具包。它是一个开源的、成熟的、跨平台的工具包。wxWidgets应用程序能在所有主流的操作系统上运行,Windo

大家好,又见面了,我是你们的朋友全栈君。

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)

执行结果:

<span role="heading" aria-level="2">2、wxWidgets介绍--菜单栏、状态栏、图标简介

特点:

  1. 没有main()函数,实际上是包含在wxWidgets框架中看不到。
  2. 一个应用程序类App,一个框架类Frame
  3. FrameAppOnInit()函数中实例化。
    注意:这里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 };

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/155292.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 数据库设计概念结构设计_数据库设计典型实例

    数据库设计概念结构设计_数据库设计典型实例文章目录数据库设计概念设计结构概念结构设计ER模型的基本元素实体/实体集属性区别实体和属性联系二元联系的关系1:11:nm:n一元联系1:11:nm:n三元联系采用ER模型的概念设计设计局部ER模型例题设计全局ER模型全局ER模型的优化数据库设计数据库设计:构造最优的数据模型,建立数据库及其应用系统的过程数据库设计的好坏非常重要概念设计结构概念设计的目标是产生反映用户需求的数据库概念结构,即概念模型概念模型具有硬件独立、软件独立的特点处于一个桥梁作用概念设计的主要步骤

    2022年10月12日
    4
  • 基于 Laravel-Admin 在十分钟内搭建起功能齐全的后台模板

    基于 Laravel-Admin 在十分钟内搭建起功能齐全的后台模板

    2021年10月21日
    36
  • 现代OpenGL教程 01 – 入门指南

    文章转载自:http://huangwei.pro/2015-05/modern-opengl1/以下是我学习opengl得到的启示最多的一篇文章,我强烈地建议大家去读一下这位大神的文章!译序早前学OpenGL的时候还是1.x版本,用的都是glVertex,glNormal等固定管线API。后来工作需要接触DirectX9,shader也只是可选项而已,跟固定管线一起混用着

    2022年4月6日
    45
  • python计算最大公约数和最小公倍数_python怎么求最大公约数和最小公倍数

    python计算最大公约数和最小公倍数_python怎么求最大公约数和最小公倍数详细内容python怎么求最大公约数和最小公倍数一、求最大公约数用辗转相除法求最大公约数的算法如下:两个正整数a和b(a>b),它们的最大公约数等于a除以b的余数c和b之间的最大公约数。比如10和25,25除以10商2余5,那么10和25的最大公约数,等同于10和5的最大公约数。具体代码如下:defgongyue(a,b):”””欧几里得算法—-辗转相除法:parama:第一个数…

    2022年5月16日
    51
  • idea导入maven项目右侧没有maven_maven多模块和单模块的

    idea导入maven项目右侧没有maven_maven多模块和单模块的今天重装了idea,发现右边的maven,数据库模块不见了,在网上找了一些方法(如:https://blog.csdn.net/zhouyingge1104/article/details/50068919),也没解决,最后自己瞎捣鼓出来了,记录下。1.maven模块不见了,如图:2,找到右下角这个图标,如图:3,鼠标右击,出现很多菜单模块,如图:4.点击Mavenp…

    2022年10月3日
    5
  • oracle 入门_钢琴零基础入门教程

    oracle 入门_钢琴零基础入门教程本章内容:ØOracle介绍Ø安装步骤Ø基本使用Ø用户管理

    2022年8月31日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号