[ZETCODE]wxWidgets教程三:第一个窗体程序
本教程原文链接:http://zetcode.com/gui/wxwidgets/firstprograms/
翻译:瓶哥
日期:2013年11月27日星期三
主页:http://www.cnblogs.com/pingge/
若有翻译错误或者歧义请联系我!
在这一章,我们将会概括介绍如何创建wxWidgets程序。我们将会创建第一个简单的例子,展示如何显示一个图标。接下来我们将会用一个简单的例子说明如何响应事件。最后,我们将会看到这些小部件是如何与wxWidgets程序进行交互的。
一个简单的程序
首先我们创建一个非常简单的wxWidgets程序。
simple.h
#include <wx/wx.h> class Simple : public wxFrame
{
public:
Simple(const wxString & title);
};
simple.cpp
#include "simple.h" Simple::Simple(const wxString & title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(, ))
{
Centre();
}
main.h
#include <wx/wx.h> class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
main.cpp
#include "main.h"
#include "simple.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit()
{
Simple * simple = new Simple(_T("Simple"));
simple->Show(true); return true;
}
这个小例子在屏幕上显示了一个小窗口,并且这个窗口是居中显示的。
Centre();
这个方法使这个窗口居中显示在屏幕上(水平和垂直)。
IMPLEMENT_APP(MyApp)
The code thar implements the application is hidden behind the macro.这是一段代码的复制粘贴,我们通常不用去关心。

应用程序图标
在这个例子中,我们给我们的程序提供一个图标,这是一个在窗口的左上角显示一个小图标的标准方法。这个图标是作为一个xpm格式文件被包含进这个程序。
icon.h
#include <wx/wx.h> class Icon : public wxFrame
{
public:
Icon(const wxString & title);
};
icon.cpp
#include "icon.h"
#include "icon.xpm" Icon::Icon(const wxString & title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(, ))
{
SetIcon(wxIcon(icon_xpm));
Centre();
}
main.h
#include <wx/wx.h> class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
main.cpp
#include "main.h"
#include "icon.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit()
{
Icon * icon = new Icon(_T("Icon"));
icon->Show(true); return true;
}
icon.xpm
/* XPM */
static const char * icon_xpm[] = {
/* columns rows colors chars-per-pixel */
"32 32 6 1",
" c black",
". c navy",
"X c red",
"o c yellow",
"O c gray100",
"+ c None",
/* pixels */
"++++++++++++++++++++++++++++++++",
"++++++++++++++++++++++++++++++++",
"++++++++++++++++++++++++++++++++",
"++++++++++++++++++++++++++++++++",
"++++++++++++++++++++++++++++++++",
"++++++++ ++++++++++",
"++++++++ ............ ++++++++++",
"++++++++ ............ ++++++++++",
"++++++++ .OO......... ++++++++++",
"++++++++ .OO......... ++++++++++",
"++++++++ .OO......... ++++++++++",
"++++++++ .OO...... ",
"++++++++ .OO...... oooooooooooo ",
" .OO...... oooooooooooo ",
" XXXXXXX .OO...... oOOooooooooo ",
" XXXXXXX .OO...... oOOooooooooo ",
" XOOXXXX ......... oOOooooooooo ",
" XOOXXXX ......... oOOooooooooo ",
" XOOXXXX oOOooooooooo ",
" XOOXXXXXXXXX ++++ oOOooooooooo ",
" XOOXXXXXXXXX ++++ oOOooooooooo ",
" XOOXXXXXXXXX ++++ oOOooooooooo ",
" XOOXXXXXXXXX ++++ oooooooooooo ",
" XOOXXXXXXXXX ++++ oooooooooooo ",
" XXXXXXXXXXXX ++++ ",
" XXXXXXXXXXXX ++++++++++++++++++",
" ++++++++++++++++++",
"++++++++++++++++++++++++++++++++",
"++++++++++++++++++++++++++++++++",
"++++++++++++++++++++++++++++++++",
"++++++++++++++++++++++++++++++++",
"++++++++++++++++++++++++++++++++"
};
在我们的程序中我们显示了一个小的wxWidgetsLogo图标。
SetIcon(wxIcon(icon_xpm));
显示图标是上面这行代码的做的工作。XPM(X PixMap)是一种以ASCII码表现的图像格式。

一个简单的按钮
在接下来的这个例子中,我们在框架上创建了一个按钮。我们将会看到,如何建立一个简单的事件处理程序。
button.h
#include <wx/wx.h> class Button : public wxFrame
{
public:
Button(const wxString & title); void OnQuit(wxCommandEvent & event);
};
button.cpp
#include "button.h" Button::Button(const wxString & title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(, ))
{
wxPanel * panel = new wxPanel(this, wxID_ANY); wxButton * button = new wxButton(panel, wxID_EXIT, _T("Quit"), wxPoint(, )); Connect(wxID_EXIT, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Button::OnQuit)); button->SetFocus(); // This sets the window to receive keyboard input Centre();
} void Button::OnQuit(wxCommandEvent & WXUNUSED(event))
{
Close(true);
}
main.h
#include <wx/wx.h> class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
main.cpp
#include "main.h"
#include "button.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit()
{
Button * button = new Button(_T("Button"));
button->Show(true); return true;
}
wxPanel * panel = new wxPanel(this, wxID_ANY);
首先我们创建一个wxPanel组件,这将被放置到一个wxFrame组件中。
wxButton * button = new wxButton(panel, wxID_EXIT, _T("Quit"), wxPoint(20, 20));
我们建立了一个wxButton组件,它被放置在panel上,我们使用wxWidgets预定义的id:wxID_EXIT绑定这个按钮,按钮上面的标签文字是"Quit",按钮被放置的坐标为(20, 20),坐标系的起点是左上角。
Connect(wxID_EXIT, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Button::OnQuit));
如果我们点击这个按钮,一个wxEVT_COMMAND_BUTTON_CLICKED事件就会被触发,我们把这个事件和OnQuit()方法捆绑在一起,所以当我们点击按钮时,OnQuit()方法就会被调用。
Button->SetFocus();
我们把键盘的焦点设置到这个按钮上,所以如果我们按下回车键就相当于按钮被按下了。
Close(true);
在OnQuit()方法中,我们调用了Close()方法,这将会终结我们的应用程序。

组件之间的通信
知道各个组件之间如何互相通信很重要,请看下面这个例子。
panels.h
#include <wx/wx.h>
#include <wx/panel.h> class LeftPanel : public wxPanel
{
public:
LeftPanel(wxPanel * parent); void OnPlus(wxCommandEvent & event);
void OnMinus(wxCommandEvent & event); wxButton * m_plus;
wxButton * m_minus;
wxPanel * m_parent;
int count;
}; class RightPanel : public wxPanel
{
public:
RightPanel(wxPanel * parent); void OnSetText(wxCommandEvent & event); wxStaticText * m_text;
}; const int ID_PLUS = ;
const int ID_MINUS = ;
panels.cpp
#include <wx/stattext.h>
#include "communicate.h" LeftPanel::LeftPanel(wxPanel * parent)
: wxPanel(parent, wxID_ANY, wxPoint(-, -), wxSize(-, -), wxBORDER_SUNKEN)
{
count = ;
m_parent = parent;
m_plus = new wxButton(this, ID_PLUS, _T("+"), wxPoint(, )); m_minus = new wxButton(this, ID_MINUS, _T("-"), wxPoint(, )); Connect(ID_PLUS, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(LeftPanel::OnPlus)); Connect(ID_MINUS, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(LeftPanel::OnMinus));
} void LeftPanel::OnPlus(wxCommandEvent & WXUNUSED(event))
{
count++; Communicate * comm = (Communicate *)(m_parent->GetParent()); comm->m_rp->m_text->SetLabel(wxString::Format(_T("%d"), count));
} void LeftPanel::OnMinus(wxCommandEvent & WXUNUSED(event))
{
count--; Communicate * comm = (Communicate *)(m_parent->GetParent()); comm->m_rp->m_text->SetLabel(wxString::Format(_T("%d"), count));
} RightPanel::RightPanel(wxPanel * parent)
: wxPanel(parent, wxID_ANY, wxDefaultPosition, wxSize(, ), wxBORDER_SUNKEN)
{
m_text = new wxStaticText(this, -, _T(""), wxPoint(, ));
}
communicate.h
#include "panels.h"
#include <wx/wxprec.h> class Communicate : public wxFrame
{
public:
Communicate(const wxString & title); LeftPanel * m_lp;
RightPanel * m_rp;
wxPanel * m_parent;
};
communicate.cpp
#include "communicate.h" Communicate::Communicate(const wxString & title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(, ))
{
m_parent = new wxPanel(this, wxID_ANY); wxBoxSizer * hbox = new wxBoxSizer(wxHORIZONTAL); m_lp = new LeftPanel(m_parent);
m_rp = new RightPanel(m_parent); hbox->Add(m_lp, , wxEXPAND | wxALL, );
hbox->Add(m_rp, , wxEXPAND | wxALL, ); m_parent->SetSizer(hbox); this->Centre();
}
main.h
#include <wx/wx.h> class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
main.cpp
#include "main.h"
#include "communicate.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit()
{
Communicate * communicate = new Communicate(_T("Communicate"));
communicate->Show(true); return true;
}
在我们的例子中,我们有两个panel,一个左边一个右边,左边的有两个按钮,右边的有一个静态文本控件。按钮改变显示在右panel中的数字。问题是,我们是如何获得静态文本控件的指针的?
m_parent = parent;
在LeftPanel里我们存储了父组件的地址,这是一个wxPanel组件。
Communicate * comm = (Communicate *)m_parent->GetParent();
Comm->m_rp->m_text->SetLabel(wxString::Format(_T("%d"), count));
这两行是这个例子中最重要的两行,它展示了如何操纵那个放在右panel中的静态文本控件。首先我们取得了左panel和右panel的父窗口指针,这个父窗口组件有一个指向右panel的指针,并且右panel有一个指向静态文本控件的指针。

在这部分的wxWidgets教程中,我们创建了一些简单的程序。
[ZETCODE]wxWidgets教程三:第一个窗体程序的更多相关文章
- [ZETCODE]wxWidgets教程五:布局管理
本教程原文链接:http://zetcode.com/gui/wxwidgets/layoutmanagement/ 翻译:瓶哥 日期:2013年12月4日星期三 邮箱:414236069@qq.co ...
- [ZETCODE]wxWidgets教程八:组件专题1
本教程原文链接:http://zetcode.com/gui/wxwidgets/widgets/ 翻译:瓶哥 日期:2013年12月12日星期四 邮箱:414236069@qq.com 主页:htt ...
- [ZETCODE]wxWidgets教程四:菜单栏和工具栏
本教程原文链接:http://zetcode.com/gui/wxwidgets/menustoolbars/ 翻译:瓶哥 日期:2013年11月28日星期四 邮箱:414236069@qq.com ...
- [ZETCODE]wxWidgets教程二:辅助类
本教程原文链接:http://zetcode.com/gui/wxwidgets/helperclasses/ 翻译:瓶哥 日期:2013年11月27日星期三 邮箱:414236069@qq.com ...
- [ZETCODE]wxWidgets教程六:事件处理
本教程原文链接:http://zetcode.com/gui/wxwidgets/events/ 翻译:瓶哥 日期:2013年12月7号星期六 邮箱:414236069@qq.com 主页:http: ...
- [ZETCODE]wxWidgets教程七:对话框
本教程原文链接:http://zetcode.com/gui/wxwidgets/dialogs/ 翻译:瓶哥 日期:2013年12月9日星期一 邮箱:414236069@qq.com 主页:http ...
- [ZETCODE]wxWidgets教程九:组件专题2
本教程原文链接:http://zetcode.com/gui/wxwidgets/widgetsII/ 翻译:瓶哥 日期:2013年12月15日星期日 邮箱:414236069@qq.com 主页:h ...
- 微信小程序开发系列教程三:微信小程序的调试方法
微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 这个教程的前两篇文章,介绍了如何用下图所示的微信开发者工具自动生成一个Hel ...
- PyQt5系列教程(三)用py2exe进行程序打包
软硬件环境 Windows 10 Python 3.4.2 PyQt5 Py2exe 前言 在我们开发了完整的PyQt5项目后,一般都会将其打包成exe文件,方便其他人使用.今天我们就用Py2exe这 ...
随机推荐
- 项目规范和建立-从frozenui学习
1.分支branches开发新功能,主干trunk是稳定发布的.因为frozenui下载完,看到branches比trunk多了一个版本 2.版本号定义,主版本.次版本.修订号 大版本号: 主版本号: ...
- 更加直观地了解hasLayout和BFC
网络上有很多关于hasLayout和BFC相关的文章,但是大部分都显得有些晦涩难懂.所以想用一些比较直观的例子来说明hasLayout和BFC给平时的布局带来的影响. 基础知识 在讲hasLayout ...
- yii2源码学习笔记(十三)
模型类DynamicModel主要用于实现模型内的数据验证yii2\base\DynamicModel.php <?php /** * @link http://www.yiiframework ...
- CentOS下Redis 2.2.14安装配置详解(转载)
一. 下载redis最新版本2.2.14 cd /usr/local/src wget –c http://redis.googlecode.com/files/redis-2.2.14.tar.gz ...
- 有两个数组a,b,大小都为n;通过交换a,b中的元素,使sum(a)-sum(b)最小。
今天在浏览网页的时候,发现了一个叫做 华为面试题(8分钟写出代码) 的链接,不确定真实性,纯属好奇,就点进去看看 这个可能是很老的题目吧,因为我看到这题目时,底下有好多评论了.提到XX排序,内存占用 ...
- C# 在SQLite数据库中存储图像 z
C# 在SQLite数据库中存储图像 更多 0 C# SQLite 建表语句 CREATE TABLE [ImageStore]([ImageStore_Id] INTEGER NOT NULL ...
- myeclipse启动项目时报:An internal error occurred during: "Launching TestSpring on Tomcat 7.x". java.lang.NullPointerException 解决方法
如果出现了上述的错误按照如下的3个步骤解决: 1.首先关闭MyEclipse工作空间. 2.然后删除工作空间下的 “/.metadata/.plugins/org.eclipse.core.runti ...
- (转) 各种好用的插件 Xcode
时间就是金钱.编码效率的提升意味着更多的收入.可是当我们的开发技巧已经到达一定高度时,如何让开发效率更上一层楼呢?答案就是使用开发工具!在这篇文章中,我会向你介绍一些帮助我提升编码速度和工作效率的工具 ...
- DM8168 编译filesystem步骤
在板子跑起来之前,需要先编译好8168的文件系统.前提是已经设置好板子的类型等参数,详见<DM8168环境搭建> 1.进入<DVR_RDK_BASE>/dvr_rdk目录 ma ...
- Eclipse里初次使用Maven注意问题
在Eclipse里初次使用Maven,右键project->add dependency, 发现search不管用,这时候需要按照以下步骤操作: Goto "Preferences - ...