#define WX_PCH_H_INCLUDED
#if ( defined(USE_PCH) && !defined(WX_PRECOMP) ) #define WX_PRECOMP #endif // USE_PCH
// basic wxWidgets headers #include <wx/wxprec.h>
// for use xrc files #include <wx/xrc/xmlres.h>
#ifdef __BORLANDC__ #pragma hdrstop #endif
#ifndef WX_PRECOMP #include <wx/wx.h> #endif
#ifdef USE_PCH // put here all your rarely-changing header files
#endif // USE_PCH
#endif // WX_PCH_H_INCLUDED
wxWidgets官方文档是大概也是这样推荐,Code::Blocks中基本上就是这样子,我只是简单的增加了一行“#include <wx/xrc/xmlres.h>”(为了使用XRC文件)。
以后,工程中的源文件,只要包含(include) wx_pch.h 文件就可以了。
创建wxApp子类
点击菜单 Insert -> New Class...,新建一个名称为“App”的类(类名称可以随意),考虑到代码的跨平台性,建议将其所在文件的名称修改为全部使用小写字母(如 app.h/app.cpp)。此操作将生成文件 app.h 和 app.cpp。
VC在这里生成的类代码显然是不满足我们的要求的,需要进行以下修改:
app.h
增加预编译头文件 wx_pch.h 的包含(以后创建的每个.h文件都要包含它):#include "wx_pch.h"
指定App类的父类为wxApp:即将“class App”修改为“class App : public wxApp”
为类增加虚方法OnInit()的声明:virtual bool OnInit();
在类声明的下方增加 wxWidgets App 声明:DECLARE_APP(App)
最终 app.h 的内容如下(其中经过手工改写的地方已用黄色背景突出显示): // by: liigo.com
#if !defined(AFX_APP_H__B4514AF3_2125_487B_BD66_AF638A80E73A__INCLUDED_) #define AFX_APP_H__B4514AF3_2125_487B_BD66_AF638A80E73A__INCLUDED_
#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000
#include "wx_pch.h"
class App : public wxApp { public: App(); virtual ~App(); virtual bool OnInit(); };
DECLARE_APP(App)
#endif // !defined(AFX_APP_H__B4514AF3_2125_487B_BD66_AF638A80E73A__INCLUDED_)
app.cpp
增加头文件包含(此头文件将在下面创建MainFrame类时创建):#include "mainframe.h"
增加 OnInit() 方法的定义(其中用到的MainFrame类定义于mainframe.h,见后文): bool App::OnInit() { MainFrame* mainFrame = new MainFrame(NULL, _("MainFrame by liigo.com")); mainFrame->Show(); SetTopWindow(mainFrame); return true; }
在类定义的上方增加 wxWidgets App 定义:IMPLEMENT_APP(App)
最终 app.cpp 的内容如下(其中经过手工改写的地方已用黄色背景突出显示): #include "app.h"
IMPLEMENT_APP(App)
App::App() { }
App::~App() { }
bool App::OnInit() { MainFrame* mainFrame = new MainFrame(NULL, _("MainFrame by liigo.com")); mainFrame->Show();
上一篇:外企面试官最爱提的问题TOP10
下一篇:windows与unix体系结构图
|