本文為原創(chuàng),如需轉(zhuǎn)載,請(qǐng)注明作者和出處,謝謝!
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="ProgId" content="Word.Document"> <meta name="Generator" content="Microsoft Word 11"> <meta name="Originator" content="Microsoft Word 11"> <link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"> <!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋體"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} </style> <![endif]-->
本文闡述了
wxWidgets
的由來(lái),以及從
wxWidgets
的特點(diǎn)出發(fā),說(shuō)明了選擇
wxWidgets
給我們帶來(lái)的好處,并且通過(guò)一個(gè)典型的例子討論了如何在
C++
中使用
wxWidgets
開(kāi)發(fā)跨平臺(tái)的軟件。
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋體"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {} @list l0:level1 { margin-left:39.0pt; text-indent:-18.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->
什么是
wxWidgets
?
wxWidgets
是一個(gè)跨平臺(tái)的軟件開(kāi)發(fā)包。它誕生于
1992
年,最初的名子是
wxWindows
,但由于
Microsoft
的抗議,在
2004
年改名為
wxWidgets
。它最初是被設(shè)計(jì)成跨平臺(tái)的
GUI
軟件開(kāi)發(fā)包,但后來(lái)隨著越來(lái)越多的人參與進(jìn)來(lái),為
wxWidgets
加入了許多非
GUI
的功能,如多線程
(MultiThread)
、網(wǎng)絡(luò)
(Network)
等。并且從最初的只支持
C++
語(yǔ)言,逐漸發(fā)展成為支持?jǐn)?shù)種語(yǔ)言(如
Python
、
Perl
、
C#
、
Basic
等)。因此,現(xiàn)在的
wxWidgets
已經(jīng)不再是單純的跨平臺(tái)的
GUI
軟件開(kāi)發(fā)包,而是一個(gè)可以支持多種操作系統(tǒng)平臺(tái)的能夠在多種語(yǔ)言中使用的通用跨平臺(tái)軟件開(kāi)發(fā)包。
由于
wxWidgets
最開(kāi)始是為
C++
而設(shè)計(jì)的,因此,本文主要討論了
wxWidgets
在
C++
中的使用。
為什么選擇
wxWidgets
?
目前支持
C++
的軟件開(kāi)發(fā)包非常多,比較有名的除了
wxWidgets
外,還有一些其它的軟件開(kāi)發(fā)包,如
MFC
、
QT
、
ACE
等。即然有這么多開(kāi)發(fā)包,那么我們?yōu)槭裁匆褂?
wxWidgets
呢?在給出答案之前,讓我們首先來(lái)看一看上述的三種軟件開(kāi)發(fā)包的特性。
1.
MFC
MFC
是
Microsoft
提供的軟件開(kāi)發(fā)包。
MFC
雖然十分強(qiáng)大,但它只能運(yùn)行在
Windows
下運(yùn)行。而且它是收費(fèi)的。
2. QT
QT
是由
Trolltech
公司開(kāi)發(fā)的一套跨平臺(tái)軟件開(kāi)發(fā)包。它和
wxWidgets
類(lèi)似,但是
QT
只在
linux
下免費(fèi),而在
Windows
或
Unix
下使用
QT
要向
Trolltech
公司支付版權(quán)費(fèi)。
3. ACE
ACE
雖然是免費(fèi)開(kāi)源的,但是它沒(méi)有提供
GUI
功能。
從以上三個(gè)軟件開(kāi)發(fā)包可以看出,它們雖然有各自的優(yōu)勢(shì),但是它們或多或少地都會(huì)使開(kāi)發(fā)受到限制。而使用wxWidgets將不會(huì)有以上所述的問(wèn)題。wxWidgets和MFC、QT、
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋體"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->ACE的特性對(duì)比如表1所示。
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="ProgId" content="Word.Document"> <meta name="Generator" content="Microsoft Word 11"> <meta name="Originator" content="Microsoft Word 11"> <link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"> <!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:Wingdings; panose-1:5 0 0 0 0 0 0 0 0 0;} @font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:黑體; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋體"; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@黑體"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {} @list l0:level1 { margin-left:42.0pt; text-indent:-21.0pt; font-family:Wingdings;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->
表1: wxWidgets和MFC、QT、ACE的特性對(duì)比表
注:其中免費(fèi)中的“是
/
否”代表
QT
在
linux
平臺(tái)上的
Free Edition
是免費(fèi)的,而在
windows
和
unix
下使用
QT
是收費(fèi)的。而開(kāi)源中的“是
/
否”代表
QT
有一個(gè)基于
GPL
的開(kāi)源版本,但要進(jìn)行商業(yè)開(kāi)發(fā),需要使用它的商業(yè)版本。
使用
wxWidgets
編寫(xiě)程序
學(xué)習(xí)一種編程語(yǔ)言的最好方法就是用它去編寫(xiě)程序,學(xué)習(xí)
wxWidgets
也不例外。由于
wxWidgets
的主要功能是實(shí)現(xiàn)跨平臺(tái)的
GUI
,因此,本文主要從
GUI
入手,討論
wxWidgets
在
C++
中如何編寫(xiě)跨平臺(tái)的應(yīng)用程序
1. 應(yīng)用程序類(lèi)的建立
使用
wxWidgets
建立系統(tǒng)需要一個(gè)類(lèi)來(lái)描述整個(gè)應(yīng)用程序。這個(gè)類(lèi)必須從
wxApp
類(lèi)繼承。
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
class
MyApp:
public
wxApp
//
應(yīng)用程序類(lèi)
{
public
:
virtual
bool
OnInit();
//
在應(yīng)用程序啟動(dòng)時(shí)調(diào)用,如果返回false,退出應(yīng)用程序
};
這個(gè)類(lèi)只覆蓋了
wxApp
的一個(gè)虛方法
OnInit
。可以用這個(gè)方法在程序啟動(dòng)時(shí)做一些驗(yàn)證,如果驗(yàn)證失敗,可以通過(guò)返回
false
退出應(yīng)用程序。當(dāng)然,由于這個(gè)函數(shù)是應(yīng)用程序的入口點(diǎn),所以建立主窗體的工作要在這個(gè)函數(shù)中完成。
2.
建立窗體類(lèi)
wxWidgets
中關(guān)于窗體的類(lèi)很多,如果要建立一般窗體的話(huà),可以從
wxFrame
繼承。
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
class
MyFrame:
public
wxFrame
//
窗體類(lèi)
{
public
:
MyFrame(
const
wxString
&
title);
//
窗體的構(gòu)造函數(shù)
};
3.
向窗體中加入控件
在本文中向這個(gè)窗體加入了一個(gè)菜單條
(Menu Bar)
、一個(gè)狀態(tài)條、一個(gè)
Panel
和一個(gè)按鈕。一般我們會(huì)在主窗體的構(gòu)造函數(shù)中加入這些控件。
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
MyFrame::MyFrame(
const
wxString
&
title):wxFrame(NULL,wxID_ANY,title)
{
wxMenu
*
fileMenu
=
new
wxMenu;
//
建立“文件”菜單
wxMenu
*
helpMenu
=
new
wxMenu;
//
建立“幫助”菜單
//
向菜單中添加子項(xiàng)
helpMenu
->
Append(wxID_ABOUT,_T(
"
關(guān)于
\tF1
"
),_T(
"
顯示關(guān)于對(duì)話(huà)框
"
));
fileMenu
->
Append(wxID_EXIT,_T(
"
退出\tAlt-X
"
),_T(
"
退出應(yīng)用程序
"
));
wxMenuBar
*
menuBar
=
new
wxMenuBar();
//
建立一個(gè)菜單條
menuBar
->
Append(fileMenu,_T(
"
文件
"
));
//
將“文件”菜單加入到菜單條
menuBar
->
Append(helpMenu,_T(
"
幫助
"
));
//
將“幫助”菜單加入到菜單條
SetMenuBar(menuBar);
//
將菜單條放到窗體上
wxPanel
*
panel
=
new
wxPanel(
this
);
//
建立一個(gè)Panel
wxButton
*
button
=
new
wxButton(panel,wxID_ABOUT,
"
關(guān)于
"
,wxPoint(
20
,
20
), wxSize(
50
,
30
));
//
建立一個(gè)Button
CreateStatusBar(
2
);
//
建立一個(gè)兩欄的狀態(tài)欄
SetStatusText(_T(
"
歡迎使用wxWidgets!
"
));
//
設(shè)置狀態(tài)欄的文本
在數(shù)組
sample_xpm
中描述了
sample.ico
的屬性和圖標(biāo)本身。如
X
代表紅色;
o
代表黃色等。然后在源程序中通過(guò)
include “sample.xpm”
引用這個(gè)資源文件。要想從這個(gè)資源文件中裝載圖標(biāo)。可使用
SetIcon(wxICON(sample)); wxICON
讀取資源文件,而
SetIcon
將這個(gè)圖標(biāo)設(shè)置為
frame
的標(biāo)題欄圖標(biāo)。要想將
ico
文件轉(zhuǎn)換為這種資源文件,可使用一個(gè)免費(fèi)軟件
XnView
進(jìn)行轉(zhuǎn)換。
5.
顯示主窗體
顯示主窗體非常簡(jiǎn)單,只需要將上面建立的
MyFrame
類(lèi)實(shí)例化,并調(diào)用
wxFrame
的
Show
方法顯示即可。這些代碼可以寫(xiě)在
MyApp
類(lèi)的
OnInit
方法中。
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
bool
MyApp::OnInit()
{
//
建立MyFrame類(lèi)的實(shí)例
MyFrame
*
frame
=
new
MyFrame(_T(
"
第一個(gè)wxWidgets程序
"
));
frame
->
Show(
true
);
//
顯示主窗體
return
true
;
//
必須返回true,否則應(yīng)用程序?qū)⑼顺?
}
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:Wingdings; panose-1:5 0 0 0 0 0 0 0 0 0;} @font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋體"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {} @list l0:level1 { margin-left:42.0pt; text-indent:-21.0pt; font-family:Wingdings;} @list l1 {} @list l1:level1 { margin-left:42.0pt; text-indent:-21.0pt; font-family:Wingdings;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->
在以上代碼中
Show
方法有一個(gè)參數(shù),如果為
true
,則以模式窗口的形式顯示,否則以非模式窗口的形式顯示。
6.
向窗體中加入事件
到目前為止,這個(gè)程序的界面已經(jīng)完成了,但還未響應(yīng)任何事件,下面就詳細(xì)闡述如何向這個(gè)應(yīng)用程序中加入事件代碼。
對(duì)于事件來(lái)說(shuō),一般都會(huì)由兩部分組成。
(1)
調(diào)用事件部分
當(dāng)程序發(fā)生某個(gè)動(dòng)作時(shí),如點(diǎn)擊按鈕;選中某個(gè)控件,可能需要執(zhí)行一段代碼。而這段代碼一般是由系統(tǒng)負(fù)責(zé)調(diào)用的,也就是說(shuō)系統(tǒng)通過(guò)事件函數(shù)指針調(diào)用相應(yīng)的代碼。
(2)
事件函數(shù)本身
事件函數(shù)與普通函數(shù)一樣,只不過(guò)它是在發(fā)生了事件之后,由系統(tǒng)調(diào)用的。
在
wxWidgets
中是通過(guò)事件哈希表
(Event Hash Table)
來(lái)進(jìn)行事件處理的,即將相應(yīng)的事件函數(shù)指針保存在一個(gè)哈希表中,然后當(dāng)事件發(fā)生時(shí),從這個(gè)哈希表中找到相應(yīng)的事件函數(shù)指針,然后通過(guò)函數(shù)指針調(diào)用函數(shù)。在使用事件哈希表之前,必須定義它。由于定義哈希表非常復(fù)雜,而且每個(gè)需要處理事件的類(lèi)都需要同樣的代碼,因此,
wxWidgets
為此定義了一個(gè)宏
DECLARE_EVENT_TABLE()
來(lái)定義哈希表。可將這個(gè)宏寫(xiě)在
MyFrame
類(lèi)的任何位置。它相當(dāng)于將以下語(yǔ)句放到了
MyFrame
類(lèi)中。
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
private
:
static
const
wxEventTableEntrysm_eventTableEntries[];
protected
:
static
const
wxEventTablesm_eventTable;
virtual
const
wxEventTable
*
GetEventTable()
const
;
static
wxEventHashTablesm_eventHashTable;
virtual
wxEventHashTable
&
GetEventHashTable()
const
;
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋體"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->
其中靜態(tài)數(shù)組變量
sm_eventTableEntries
保存了
MyFrame
類(lèi)中的所有的事件信息。
上面的代碼聲明了處理事件哈希表的一些方法,即然聲明了,就得實(shí)現(xiàn)。由于實(shí)現(xiàn)代碼也都一樣,因此,
wxWidgets
也為實(shí)現(xiàn)這些方法定義了一組宏。實(shí)現(xiàn)這些方法的宏如下所示。
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
BEGIN_EVENT_TABLE(MyFrame,wxFrame)
EVT_MENU(wxID_EXIT,MyFrame::OnQuit)
EVT_MENU(wxID_ABOUT,MyFrame::OnAbout)
EVT_BUTTON(wxID_ABOUT,MyFrame::OnAbout)
END_EVENT_TABLE()
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋體"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->
其中
BEGIN_EVENT_TABLE(…)
實(shí)現(xiàn)了上面定義的方法,以及初始化了靜態(tài)變量
sm_eventTable
。后面兩個(gè)
EVT_MENU
和一個(gè)
EVT_BUTTON
宏初始化了靜態(tài)變量
sm_eventTableEntries
,即將這兩個(gè)事件函數(shù)的指針
(button
和
about
菜單使用一個(gè)事件函數(shù)
OnAbout)
和控件
ID
保存在
sm_eventTableEntries
中,最后的
END_EVENT_TABLE()
宏做為一個(gè)空的事件函數(shù)指針賦給了
sm_eventTableEntries
,這有些象
C
語(yǔ)言中處理字符串,將最后一個(gè)字符賦為
’"0’
,這樣就可以知道哪是結(jié)尾了。
向窗體中加入事件的最后一步是聲明和實(shí)現(xiàn)事件函數(shù)。在本例中聲明了兩個(gè)事件函數(shù)。
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
void
OnQuit(wxCommandEvent
&
event
);
void
OnAbout(wxCommandEvent
&
event
);
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋體"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->
可以將這兩個(gè)函數(shù)聲明放到
MyFrame
中的任何位置。下面是它們的實(shí)現(xiàn)代碼。
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
void
MyFrame::OnQuit(wxCommandEvent
&
WXUNUSED(
event
))
{
Close(
true
);
}
void
MyFrame::OnAbout(wxCommandEvent
&
WXUNUSED(
event
))
{
wxStringmsg;
msg.Printf(_T(
"
這是一個(gè)關(guān)于對(duì)話(huà)框的例子.\n
"
)
_T(
"
歡迎使用%s
"
),wxVERSION_STRING);
wxMessageBox(msg,_T(
"
1關(guān)于
"
),wxOK
|
wxICON_INFORMATION,
this
);
}
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:Wingdings; panose-1:5 0 0 0 0 0 0 0 0 0;} @font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋體"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {} @list l0:level1 { margin-left:42.0pt; text-indent:-21.0pt; font-family:Wingdings;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->
其中
OnQuit
函數(shù)調(diào)用
Close(true)
關(guān)閉
MyFrame
,由于
MyFrame
是主窗體,因此,在
MyFrame
關(guān)閉后,應(yīng)用程序也隨之關(guān)閉了。
OnAbout
使用
wxMessageBox
函數(shù)彈出一個(gè)信息對(duì)話(huà)框。
7.
運(yùn)行程序
到目前為止,這個(gè)程序的代碼已經(jīng)基本完成了,但是在前面曾說(shuō)過(guò),
MyApp
中的
OnInit
方法在應(yīng)用程序啟動(dòng)時(shí)執(zhí)行,那么是誰(shuí)調(diào)用了
OnInit
方法呢?答案當(dāng)然是
wxWidgets
。
wxWidgets
為了調(diào)用這個(gè)方法,提供了一個(gè)宏
IMPLEMENT_APP(…)
,這個(gè)宏有一個(gè)參數(shù),需要將
MyApp
做為參數(shù)傳入。即
IMPLEMENT_APP(MyApp)
。這個(gè)宏相當(dāng)于一個(gè)
WinMain
函數(shù)(和控制臺(tái)程序的
main
函數(shù)類(lèi)似),即在
WinMain
函數(shù)中調(diào)用了
MyApp
中的
OnInit
函數(shù)。在加入這個(gè)宏后,就可使用一個(gè)
C++
編譯器將以上的源程序編譯生成
exe
文件了。應(yīng)用程序的界面如圖
1
、圖
2
所示。
圖1 Windows下的程序界面
圖2 Linux下的程序界面
<meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C02%5Cclip_filelist.xml"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if !mso]> <style> st1":*{behavior:url(#ieooui) } </style> <![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋體"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {} @list l0:level1 { text-indent:-18.0pt;} @list l1 {} @list l1:level1 { margin-left:39.0pt; text-indent:-18.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->
注:在
windows
下使用的
wxWidgets
版本是
wxWidgets2.6.2
,在
linux
下使用的
wxWidgets
版本號(hào)是
wxWidgets2.6.3
,因此,在
windows
和
linux
下的
wxVERSION_STRING
值不一樣。
wxWidgets
的優(yōu)勢(shì)和不足
通過(guò)上面的介紹,相信讀者已經(jīng)對(duì)如何使用
wxWidgets
編寫(xiě)
GUI
程序有了一定的了解。
wxWidgets
在開(kāi)發(fā)跨平臺(tái)的軟件上有著許多其它軟件開(kāi)發(fā)包不具備的優(yōu)勢(shì),下面就總結(jié)一下
wxWidgets
所具有的優(yōu)勢(shì)。
1. 跨平臺(tái)
wxWidgets
支持非常多的操作系統(tǒng)平臺(tái),如
Windows
、
Linux
、
Unix
等。
2.
豐富的組件
wxWidgets
擁有上百個(gè)組件可供用戶(hù)選擇。有了這些組件,將會(huì)給我們帶來(lái)更加豐富的用戶(hù)體驗(yàn)。
3.
支持多種語(yǔ)言
wxWidgets
不僅可以在
C++
中使用,而且也可以在其它語(yǔ)言中使用,這些語(yǔ)言包括
python
、
perl
、
c#
等。
4.
使用本地控制
從上面給出的兩個(gè)應(yīng)用程序界面可以看出,在
Windows
和
Linux
下運(yùn)行這個(gè)應(yīng)用程序保持了各自的風(fēng)格。這是因?yàn)?
wxWidgets
采用了本地的
API
,而不象其它的跨平臺(tái)庫(kù)去模擬它們。因此,使用
wxWidgets
開(kāi)發(fā)和在
Windows
下使用
Win32 API
或在
Linux
下使用
GTK
開(kāi)發(fā)沒(méi)有什么區(qū)別。
5.
免費(fèi)開(kāi)源
這個(gè)世界上免費(fèi)的開(kāi)發(fā)包很多,強(qiáng)大的開(kāi)發(fā)包也很多,當(dāng)然,開(kāi)源的開(kāi)發(fā)包就更多了。但是要想同時(shí)滿(mǎn)足這三點(diǎn):免費(fèi)、開(kāi)源、強(qiáng)大,又同時(shí)具有本地程序一樣的性能,恐怕
wxWidgets
是唯一的選擇,至少是最佳的選擇。
相信上面關(guān)于
wxWidgets
的
5
個(gè)優(yōu)勢(shì)已經(jīng)足以成為我們選擇它的理由了。也就是說(shuō),如果選擇
wxWidgets
,不僅可以獲得強(qiáng)大的功能、卓越的性能,而且您不必為此付一分錢(qián)。當(dāng)然,人無(wú)完人、物無(wú)完物。
wxWidgets
也并不是沒(méi)有缺點(diǎn)。下面就說(shuō)一下
wxWidgets
的不足之處。
1. IDE
支持不夠
對(duì)于
wxWidgets
來(lái)說(shuō),最大的優(yōu)點(diǎn)也就是它最大的缺點(diǎn)。由于
wxWidgets
所提供的組件很多,但到現(xiàn)在為止還沒(méi)有一個(gè)強(qiáng)大的
IDE
來(lái)支持它,這將給大型系統(tǒng)的開(kāi)發(fā)帶來(lái)麻煩。
2.
對(duì)雙字節(jié)字符的支持不理想
wxWidgets
中的有些組件,如
xml
組件,無(wú)法識(shí)別雙字節(jié)字符,如漢字會(huì)被認(rèn)為是非法字符而無(wú)法裝載
xml
文檔。
綜合上述,
wxWidgets
從總體上來(lái)說(shuō)還是一個(gè)非常強(qiáng)大的跨平臺(tái)軟件開(kāi)發(fā)包。如果您沒(méi)有足夠的資金來(lái)購(gòu)買(mǎi)商業(yè)的軟件開(kāi)發(fā)包,也許
wxWidgets
是最好的選擇。雖然
wxWidgets
也有一些不足,但這并不能阻礙
wxWidgets
的發(fā)展。
wxWidgets
的功能還很多,由于篇幅所限,本文只能從一個(gè)簡(jiǎn)單的例子來(lái)討論如何用
wxWidgets
來(lái)開(kāi)發(fā)一個(gè)跨平臺(tái)的
GUI
程序,如果讀者對(duì)
wxWidgets
感性趣,可以訪問(wèn)
http://www.wxWidgets.org
獲得更多的信息。
要在Linux下使用Eclipse開(kāi)發(fā)wxWidgets程序,請(qǐng)讀者參閱
《快速配置Linux + Eclipse + wxWidgets開(kāi)發(fā)環(huán)境
》
國(guó)內(nèi)最棒的Google Android技術(shù)社區(qū)(eoeandroid),歡迎訪問(wèn)!
《銀河系列原創(chuàng)教程》
發(fā)布
《Java Web開(kāi)發(fā)速學(xué)寶典》
出版,歡迎定購(gòu)
使用wxWidgets進(jìn)行跨平臺(tái)的C++開(kāi)發(fā)