This mini tutorial aims to help people make quick judgements concerning syntax and basic principles. The complete Hello World program is here
You have to include wxWidgets's header files, of course. This can be done on a file by file basis (such as #include "wx/window.h") or using one global include (#include "wx/wx.h"). This is also useful when using precompiled headers.
#include "wx/wx.h"
Practically every app should define a new class derived from wxApp. By overriding wxApp's OnInit() the program can be initialized, e.g. by creating a new main window.
class MyApp: public wxApp
{
virtual bool OnInit();
};
The main window is created by deriving a class from wxFrame and giving it a menu and a status bar in its constructor. Also, any class that wishes to respond to any "event" (such as mouse clicks or messages from the menu or a button) must declare an event table using the macro below. Finally, the way to react to such events must be done in "handlers". In our sample, we react to two menu items, one for "Quit" and one for displaying an "About" window. These handlers should not be virtual.
class MyFrame: public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
DECLARE_EVENT_TABLE()
};
In order to be able to react to a menu command, it must be given a unique identifier such as a const or an enum.
enum
{
ID_Quit = 1,
ID_About,
};
We then procede to actually implement an event table in which the events are routed to their respective handler functions in the class MyFrame. There are predefined macros for routing all common events, ranging from the selection of a list box entry to a resize event when a user resizes a window on the screen.
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Quit, MyFrame::OnQuit)
EVT_MENU(ID_About, MyFrame::OnAbout)
END_EVENT_TABLE()
As in all programs there must be a "main" function. Under wxWidgets main is implemented using this macro, which creates an application instance and starts the program.
IMPLEMENT_APP(MyApp)
As mentionend above, wxApp::OnInit() is called upon startup and should be used to initialize the program, maybe showing a "splash screen" and creating the main window (or several). The frame should get a title bar text ("Hello World") and a position and start-up size. One frame can also be declared to be the top window. Returning TRUE indicates a successful intialization.
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame( _("Hello World"), wxPoint(50, 50), wxSize(450, 340) );
frame->Show(true);
SetTopWindow(frame);
return true;
}
In the constructor of the main window (or later on) we create a menu with two menu items as well as a status bar to be shown at the bottom of the main window. Both have to be "announced" to the frame with respective calls.
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, -1, title, pos, size)
{
wxMenu *menuFile = new wxMenu;
menuFile->Append( ID_About, _("&About...") );
menuFile->AppendSeparator();
menuFile->Append( ID_Quit, _("E&xit") );
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, _("&File") );
SetMenuBar( menuBar );
CreateStatusBar();
SetStatusText( _("Welcome to wxWidgets!") );
}
Here are the actual event handlers. MyFrame::OnQuit() closes the main window by calling Close(). The paramter TRUE indicates that other windows have no veto power such as after asking "Do you really want to close?". If there is no other main window left, the applicatin will quit.
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(true);
}
MyFrame::OnAbout() will display a small window with some text in it. In this case a typical "About" window with information about the program.
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox( _("This is a wxWidgets Hello world sample"),
_("About Hello World"),
wxOK | wxICON_INFORMATION, this );
}
Under Unix (including Linux and OS X and nearly any system that uses GCC), you can use the following statement to compile the program:
g++ hworld.cpp `wx-config --libs` `wx-config --cxxflags` -o hworld
Finally, you might want to start the program with:
./hworld
A version of hello world using Connect() instead of event tables is here
引出出处:http://www.wxwidgets.org/docs/tutorials/hello.htm
分享到:
相关推荐
wxWidget-HelloWorld
Wxwidgets2.8的examples,把这些例子都弄明白了,在做东西就很容易。 在示例程序中,还可以一睹 Wxwidgets开发者的芳容。
该例子是移植了wxWidget多语言的方法,详细介绍在空间的文章。
跨平台编程,Wxwidget是个不错的选择,作为GUI编程的开发库,显然使用手册是必须的。
一个wxwidget 的换肤工具不规则窗体 贴图等
wxWidget 自绘窗体,使用的Widget,只是实现了标题栏
wxWidget参考资料 安装步骤,参考书,帮助文档
wxWidget文档,可以进行C++跨平台编程,是新手学习的好资料
一个基于 wxWidget 的小型基因组查看器程序。 我是生物信息学的新手。 希望这会在使用 C++ 构建 bio-GUI 项目时为人们提供帮助。 该软件用于查看基因组,包括多种类型的文件,如bed、gff、gtf、vcf、fasta、bam。
中文看的快啊. 详细的说明了wxWidget的一些使用。如果参照英文手册,那么很多都可以获得解决。
个人总结的完整版的CodeBlocks的安装配置,还有wxWidget在windows下的wxMSW的安装及配置。
一个简单易懂的wxWidget动态加载资源XRC的例子
wxWidget 示例,附带说明文档, doc中附带环境搭建,以及窗体应用程序的搭建VC + wxWidgets-2.9.2 (注意,版本不一样,可能包含的库的文件不一样,读者可以根据wxWidgets自带的包的Vc中设置环境,采取相应的设置即可...
为了写正则表达式方便,分别用C++版本(MFC+libcurl+boost)以及python(re+wxWidget库)版本两个解析器,因为这两种解析器对于正则解析稍微有区别,例如python对于范围默认为最大,boost就默认为最小。版本提供了网页...
很全面的教程~循序渐进,适合各种人士,学习工作必备参考资料
如题目,MFC多语言-wxWidgets移植法采用的动态库源码。
《Cross Platform GUI Programming With wxWidget》中文版
wxWidget 2.9.2 用MinGW 4.7编译时需要的两个patch
wxWidget环境配置[归纳].pdf
使用codeBlocks + wxWidget做的扫雷程序,支持3种级别,支持后门程序一键开启。时钟显示暂时没有加,下个版本加上来。