本教程原文链接:http://zetcode.com/gui/wxwidgets/firstprograms/

翻译:瓶哥

日期:2013年11月27日星期三

邮箱:414236069@qq.com

主页: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教程三:第一个窗体程序的更多相关文章

  1. [ZETCODE]wxWidgets教程五:布局管理

    本教程原文链接:http://zetcode.com/gui/wxwidgets/layoutmanagement/ 翻译:瓶哥 日期:2013年12月4日星期三 邮箱:414236069@qq.co ...

  2. [ZETCODE]wxWidgets教程八:组件专题1

    本教程原文链接:http://zetcode.com/gui/wxwidgets/widgets/ 翻译:瓶哥 日期:2013年12月12日星期四 邮箱:414236069@qq.com 主页:htt ...

  3. [ZETCODE]wxWidgets教程四:菜单栏和工具栏

    本教程原文链接:http://zetcode.com/gui/wxwidgets/menustoolbars/ 翻译:瓶哥 日期:2013年11月28日星期四 邮箱:414236069@qq.com ...

  4. [ZETCODE]wxWidgets教程二:辅助类

    本教程原文链接:http://zetcode.com/gui/wxwidgets/helperclasses/ 翻译:瓶哥 日期:2013年11月27日星期三 邮箱:414236069@qq.com ...

  5. [ZETCODE]wxWidgets教程六:事件处理

    本教程原文链接:http://zetcode.com/gui/wxwidgets/events/ 翻译:瓶哥 日期:2013年12月7号星期六 邮箱:414236069@qq.com 主页:http: ...

  6. [ZETCODE]wxWidgets教程七:对话框

    本教程原文链接:http://zetcode.com/gui/wxwidgets/dialogs/ 翻译:瓶哥 日期:2013年12月9日星期一 邮箱:414236069@qq.com 主页:http ...

  7. [ZETCODE]wxWidgets教程九:组件专题2

    本教程原文链接:http://zetcode.com/gui/wxwidgets/widgetsII/ 翻译:瓶哥 日期:2013年12月15日星期日 邮箱:414236069@qq.com 主页:h ...

  8. 微信小程序开发系列教程三:微信小程序的调试方法

    微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 这个教程的前两篇文章,介绍了如何用下图所示的微信开发者工具自动生成一个Hel ...

  9. PyQt5系列教程(三)用py2exe进行程序打包

    软硬件环境 Windows 10 Python 3.4.2 PyQt5 Py2exe 前言 在我们开发了完整的PyQt5项目后,一般都会将其打包成exe文件,方便其他人使用.今天我们就用Py2exe这 ...

随机推荐

  1. MongoDB笔记(三)启动命令mongod的参数

    上一节有关访问权限的笔记,是由启动命令mongod的参数auth引发的有关问题,这节就来看看mongod的其他参数 MongoDB启动命令mongod参数说明: 基本配置 --quiet # 安静输出 ...

  2. centos 安装php-fpm , nginx二级域名配置 ,但为什么必须要 域名提供商 哪里解析新的二级域名一下 才能用呢?

    yum -y install php-fpm php-mysql(当然还有其它扩展) /etc/init.d/php-fpm restart (重启php-fpm) /etc/php.ini (php ...

  3. 《sed的流艺术之一》-linux命令五分钟系列之二十一

    本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...

  4. linux运维工程师,必须掌握以下几个工具

    本人是linux运维工程师,对这方面有点心得,现在我说说要掌握哪方面的工具吧说到工具,在行外可以说是技能,在行内我们一般称为工具,就是运维必须要掌握的工具.我就大概列出这几方面,这样入门就基本没问题了 ...

  5. important的妙用

    !important: 为某些样式设置具有最高权值,高于id选择器 用法: !important要写在分号的前面 例如: <p class="first">!impor ...

  6. CentOS6.4 安装aria2多线程下载工具

    aria2是一个Linux下的多线程下载工具,支持HTTP/HTTPS.FTP.BitTorrent.Metalink协议. 平时在linux上下载http上的东西常用如wget.curl命令,但是他 ...

  7. [转]PHP Session原理分析及使用

    之前在一个叫魔法实验室的博客中看过一篇<php session原理彻底分析>的文章,作者从session的使用角度很好阐述了在代码运行过程中,每个环节的变化以及相关参数的设置及作用.本来想 ...

  8. JVM 找出最耗 cpu的线程 并打印线程栈

    监控JVM中最占cpu的线程 top -Hp pid JVM中最占cpu的线程ID -o THREAD,tid,time | awk 'BEGIN {count=0; } { if($2>0.3 ...

  9. 5个有用的.net profiling工具(转)

    我们有时需要对研发的软件程序进行性能测试,这时需要用到一些Profilers工具.下面列出5个有用的.net Profilers: 1. JetBrains dotTrace JetBrains do ...

  10. USB otg 学习笔记

    1 USB OTG的工作原理 OTG补充规范对USB2.0的最重要的扩展是其更具节能性的电源管理和允许设备以主机和外设两种形式工作.OTG有两种设备类型:两用OTG设备(Dualrole device ...