wxWidget为跨平台的图形界面应用程序的开发提供了强大的支持。wxWidget提供了类似MFC的应用程序框架的支持,这个框架采用C++开发,以类的形势提供给应用程序员。wxWidget的跨平台特性体现在使用wxWidget的应用程序不依赖于特定的OS和特定的C++编译环境。只要在开发过程中不使用任何依赖特定OS和编译器的特性,wxWidget程序就可以在任何一种编译环境中无须修改的编译。目前,wxWidget支持以下的OS:Windows/Windows CE、Unix/Linux、Mac OS X、OS/2、Palm OS。
为什么是wxWidget而不是Java
Java对于基于Web的应用程序是十分出色的,但对于桌面系统来说,Java并不总是一个最好的选择。一般来说,使用wxWidget的基于C++的应用程序具有更好的运行时效率,本地化的外观。由于不需要Java虚拟机的支持,因此,wxWidget更加易于安装。同时,C++更易于访问底层的功能,更易于与已有的C/C++代码集成。由于以上这些原因,只有很少的桌面应用程序是基于Java的。wxWidget提供了超出想象的高性能、本地化的应用程序。(本段文字译自《Cross-Platform GUI Programming with wxWidgets》)
在VC6中使用wxWidget
a首先要在你的OS中安装VC++6。:)
b需要获得wxWidget的源文件,可以通过以下网址下载
http://sourceforge.net/projects/wxwindows。得到源文件后就可以通过 wxWidget安装目录\build\msw\mx.dsw 编译它们了。wxWidget提供了包括Win32 Debug和Win32 Release在内的多种编译配置,可以根据自己的需要选择地编译。在下文中仅以Win32 Debug为例,其它的配置类似。
c打开VC6,通过 new->projects->Win32 Application 新建一个空白工程。
d通过Project->Settings设置工程属性。相应属性设置如下:
1在C/C++->General中添加预编译宏定义:_WXDEBUG_和_WXMSW_。
2在C/C++->Code Generation->Use run-time library中选择Debug Multithreaded DLL。
3在Link->General中添加连接时使用的lib文件:wxmsw27d_xrc.lib wxmsw27d_html.lib wxmsw27d_adv.lib wxmsw27d_core.lib wxbase27d_xml.lib wxbase27d.lib wxtiffd.lib wxjpegd.lib wxpngd.lib wxzlibd.lib wxregexd.lib wxexpatd.lib。
e设置头文件和库文件目录。
1在Tools->Options->Directories->Include files中加入如下两个目录:wxWidget安装目录\include和wxWidget安装目录\include\msvc。
2在Tools->Options->Directories->Library files中加入以下目录:wxWidget安装目录\lib\vc_lib。
f通过Project->Add to project->New向工程中添加一个空白C++源文件。并将以下代码复制到该文件中。
// Name: minimal.cpp
// Purpose: Minimal wxWidgets sample
// Author: Julian Smart
//changed: Huang
#include "wx/wx.h"
// Declare the application class
class MyApp : public wxApp
{
public:
// Called on application startup
virtual bool OnInit();
};
// Declare our main frame class
class MyFrame : public wxFrame
{
public:
// Constructor
MyFrame(const wxString& title);
// Event handlers
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
private:
// This class handles events
DECLARE_EVENT_TABLE()
};
// Implements MyApp& GetApp()
DECLARE_APP(MyApp)
// Give wxWidgets the means to create a MyApp object
IMPLEMENT_APP(MyApp)
// Initialize the application
bool MyApp::OnInit()
{
// Create the main application window
MyFrame *frame = new MyFrame(wxT("Minimal wxWidgets App"));
// Show it
frame->Show(true);
// Start the event loop
return true;
}
// Event table for MyFrame
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
END_EVENT_TABLE()
void MyFrame::OnAbout(wxCommandEvent& event)
{
wxString msg;
msg.Printf(wxT("Hello and welcome to %s"),
wxVERSION_STRING);
wxMessageBox(msg, wxT("About Minimal"),
wxOK | wxICON_INFORMATION, this);
}
void MyFrame::OnQuit(wxCommandEvent& event)
{
// Destroy the frame
Close();
}
//#include "mondrian.xpm"
MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title)
{
// Set the frame icon
//SetIcon(wxIcon(mondrian_xpm));
// Create a menu bar
wxMenu *fileMenu = new wxMenu;
// The "About" item should be in the help menu
wxMenu *helpMenu = new wxMenu;
helpMenu->Append(wxID_ABOUT, wxT("&About...\tF1"),
wxT("Show about dialog"));
fileMenu->Append(wxID_EXIT, wxT("E&xit\tAlt-X"),
wxT("Quit this program"));
// Now append the freshly created menu to the menu bar...
wxMenuBar *menuBar = new wxMenuBar();
menuBar->Append(fileMenu, wxT("&File"));
menuBar->Append(helpMenu, wxT("&Help"));
// ... and attach this menu bar to the frame
SetMenuBar(menuBar);
// Create a status bar just for fun
CreateStatusBar(2);
SetStatusText(wxT("Welcome to wxWidgets!"));
}
g编译连接。