一个简单的AppFramework

来源:互联网 发布:发现网络配置存在问题 编辑:程序博客网 时间:2024/06/06 02:24

在MFC中,你看不到WinMain()入口函数,因为MFC把它封装了。

在自己写一个UI Framework时是否也可以借鉴。

 

这里我简单得贴源码:

// App.h: interface for the CApp class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_APP_H__FFF66B76_C2C9_47C9_9CB6_934682AA12B9__INCLUDED_)
#define AFX_APP_H__FFF66B76_C2C9_47C9_9CB6_934682AA12B9__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class __declspec(dllexport) CApp 
{
public:
 CApp();
 virtual ~CApp();
 static CApp* getApp();
 int get();
protected:
 virtual bool InitInstance();

};

#endif // !defined(AFX_APP_H__FFF66B76_C2C9_47C9_9CB6_934682AA12B9__INCLUDED_)

 

// App.cpp: implementation of the CApp class.
//
//////////////////////////////////////////////////////////////////////

#include "App.h"
#include <iostream>
using namespace std;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CApp * App;

CApp::CApp()
{
 App = this;
}

CApp::~CApp()
{

}

int CApp::get()
{
 InitInstance();

 return 0;
}
CApp* CApp::getApp()
{
 return App;
}
bool CApp::InitInstance()
{
 cout<<"CApp InitInstance";
 return true;
}

 

//////////////////////////////////////////////////////////////////////////
// main.cpp 程序入口
//////////////////////////////////////////////////////////////////////////


#include "App.h"

int main()
{
 CApp* app;
 app = CApp::getApp();
 app->get();
 return 0;
}

 

编译后将生成 两个obj文件App.obj main.obj 当然你可以把CApp类写到dll中。

 


新建一个项目,创建一个类来继承CApp

// MyApp.h: interface for the CMyApp class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_MYAPP_H__39F7F636_F96C_4A7A_BA6F_7752E4CFBBB6__INCLUDED_)
#define AFX_MYAPP_H__39F7F636_F96C_4A7A_BA6F_7752E4CFBBB6__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "App.h"

class CMyApp : public CApp
{
public:
 CMyApp();
 virtual ~CMyApp();
protected:
 virtual bool InitInstance();

};

#endif // !defined(AFX_MYAPP_H__39F7F636_F96C_4A7A_BA6F_7752E4CFBBB6__INCLUDED_)

 

// MyApp.cpp: implementation of the CMyApp class.
//
//////////////////////////////////////////////////////////////////////

#include "MyApp.h"
#include <iostream>
using namespace std;

CMyApp theApp;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMyApp::CMyApp()
{

}

CMyApp::~CMyApp()
{

}

bool CMyApp::InitInstance()
{
 cout<<"CMyApp::InitInstance"<<endl;
 return true;
}

 

将两个obj 和 CApp.h头文件加入到工程中,编译运行。输出CMyApp::InitInstance