您的位置:首页 > 其它

wxWidgets程序一般执行过程

2009-12-23 14:37 225 查看
下面大概的描述一下整个程序的执行过程:
1. 依照系统平台的不同,不同的main函数或者winmain函数或者其它类似的函数被调用(这个
函数是由wxWidgets内部提供的,而不是由应用程序提供的).wxWidgets 初始化它自己的数
据结构并且创建一个MyApp的实例.
2. wxWidgets调用MyApp::OnInit函数, 这个函数会创建一个MyFrame的实例.
3. MyFrame的构造函数通过它的基类wxFrame的构造函数创建一个窗口,然后给这个窗口增加
图标,菜单栏和状态栏.
4. MyApp::OnInit函数显示主窗口并且返回真.

5. wxWidgets开始事件循环,等待事件发生并且将事件分发给相应的处理过程.
就目前我们所知道的,应用程序会在以下情况下退出:主窗口被关闭,用户选择退出菜单或者系
按钮和系统菜单中的关闭选项(这些系统菜单和系统按钮在不同的系统中就往往千差万别了)。

// Name : minimal . cpp
// Purpose : Minimal wxWidgets sample
// Author : Julian Smart
# include "wx/wx.h"
// 定义应用程序类
class MyApp : public wxApp
{
public :
// 这个函数将会在程序启动的时候被调用
virtual bool OnInit ();
};
// 定义主窗口类
class MyFrame : public wxFrame
{
public :
// 主窗口类的构造函数
MyFrame ( const wxString & title );
// 事件处理函数
void OnQuit ( wxCommandEvent & event );
void OnAbout ( wxCommandEvent & event );
private :
// 声明事件表
DECLARE EVENT TABLE ()
};
// 有了这一行就可以使用 MyApp & wxGetApp了()
DECLARE APP ( MyApp )
// 告诉主应用程序是哪个类wxWidgets
IMPLEMENT APP ( MyApp )

// 初始化程序
bool MyApp :: OnInit ()
{
// 创建主窗口
MyFrame ?frame = new MyFrame ( wxT (" Minimal wxWidgets App " ));
// 显示主窗口
frame?>Show ( true );
// 开始事件处理循环
return true ;
}
// 类的事件表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 )
{
// 释放主窗口
Close ();
}
# include " mondrian . xpm "
MyFrame :: MyFrame ( const wxString & title )
: wxFrame (NULL , wxID ANY , title )
{
// 设置窗口图标
SetIcon ( wxIcon ( mondrian xpm ));
// 创建菜单条
wxMenu ? fileMenu = new wxMenu ;
// 添加“关于”菜单项
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 " ));
// 将菜单项添加到菜单条中
wxMenuBar ? menuBar = new wxMenuBar ();
menuBar?>Append ( fileMenu , wxT ("& File " ));
menuBar?>Append ( helpMenu , wxT ("& Help " ));
// 然后将菜单条放置在主窗口上...

SetMenuBar ( menuBar );
// 创建一个状态条来让一切更有趣些。
CreateStatusBar (2);
SetStatusText ( wxT (" Welcome to wxWidgets !" ));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: