MFC 应用程序的操作步骤可归结为四步:
(1) 创建应用程序对象theApp
(2) 执行MFC提供的WinMain()函数
(3) WinMain()调用InitInstance()函数,此函数创建文档模板,主框架窗口,文档和视图
(4) WinMain()调用Run()函数,此函数执行主消息循环,以获取和分派Windows消息。
WinMain()是函数的入口点,该函数的主要任务是完成一些初始化的工作和维护了一个消息循环。他们的工作流程如下:入口(WinMain())---->MyRegisterClass()---->InitInstance ()--->while消息循环。函数由入口开始执行,之后调用 MyRegisterClass()注册窗口类,之后InitInstance ()生成并显示窗口,这样之后,就完成了一个窗口的初始化工作了(当然,在 MyRegisterClass(),InitInstance ()中都需要调用相应的API函数来具体的实现),然后就是维护消息循环,至此,程序的基本结构就差不多建立了。以后程序的运作就靠个消息循环来推动了。
MFC程序工作原理:
在Class View 中的Global中的文件theApp CTestApp theApp;
这条语句声明了应用程序类CTestApp 的一个实际对象theApp。 在创建过theApp对象之后,
MFC提供的WinMain()函数被调用。 该函数再调用theApp 对象的两个成员函数。首先调用的是InitInstance(),此函数执行任何必要的初始化应用程序的工作,然后调用的是Run(),此函数提供对Windows消息的初步处理。 WinMain()函数没有显示出现在项目的源代码中,因为它是由MFC类库提供的,在应用程序启动时将被自动调用。 InitInstance()函数的基本功能:进行一些基本的初始化工作并进行 注册 。 Run()函数是从应用程序基类CWinApp继承的,该函数是虚函数,我们可以用自己的版本代替基类的Run()函数版本。 Run()函数获取所有以该应用程序为目的的windows消息,并确保把每一条消息都传递给程序中指定的服务该消息的函数(如果有这样的函数的话) 。因此,只要应用程序在运行,Run()函数就会继续执行。该函数在应用程序关闭时才会终止。
#include
class animal
{
public:
animal(int weight,int height)
{
cout<<"animal construct"<
~animal()
{
cout<<"animal disconstruct"<
void eat()
{
cout<<"animal eat"<
void sleep()
{
cout<<"animal sleep"<
virtual void breathe()
{
cout<<"animal breathe"<
};
class godenfish:public animal
{
public:
godenfish():animal(200,100)
{
cout<<"godenfish construct"<
~godenfish()
{
cout<<"godenfish disconstruct"<
virtual void breathe()
{
cout<<"godenfish breathe"<
};
class fish:public animal
{
public:
godenfish gfh;
fish():animal(400,300),a(10)
{
cout<<"fish construct"<
~fish()
{
cout<<"fish disconstruct"<
virtual void breathe()
{
// animal::breathe();
cout<<"fish breathe"<
private:
const int a;
};
//void fn(animal *pn)
//{
// pn->breathe();
//};
void main()
{
// animal *pn;
fish fh;
// pn=&fh;
// fn(pn);
// fh.breathe();
fh.gfh.breathe();
}