---
title: "VC视频教程笔记(第3课)"
author: "lampeter123"
date: "2009-07-19T19:05:27Z"
tags: ["HTML", "MFC", "VC++", "thread", "工作"]
slug: "vc-3"
draft: false
lastmod: "2009-07-20T07:59:52Z"
id: 428968
url: "https://lampeter123.iteye.com/blog/428968"
---

http://www.sunxin.org/forum/thread/210.html?page=1
作者：虎非龙 

第3课
1.在main或WinMain之前，全局变量已经被分配内存并初始化了。
2.在MFC中在WinMain之前有个theApp全局变量先被构造并被初始化，而由于子类构造函数执行前，其父类的构造函数先被执行，所以CTestApp的父类CWinAPP的构造函数先执行。产生了theApp对象后，在WinMain()中的指针*pThread和*pApp就有了内容。
3.MFC大致流程：
CTestApp theApp;//构造全局对象
WinMain()
{
AfxWinMain();//调用下面的函数
}
AfxWinMain()
{
pThread->Initinstance();//初始化工作和注册窗口类，窗口显示和更新
pThread->Run();//消息循环
}
而在BOOL CTestApp::InitInstance()中的代码
 CSingleDocTemplate* pDocTemplate;
 pDocTemplate = new CSingleDocTemplate(
  IDR_MAINFRAME,
  RUNTIME_CLASS(CTestDoc),
  RUNTIME_CLASS(CMainFrame),       // main SDI frame window
  RUNTIME_CLASS(CTestView));
 AddDocTemplate(pDocTemplate);
完成了将这三个类关联起来的工作。
4.如何在单文档文件中显示一个CButton的对象？
在CMainFrame::OnCreate()中定义一个CButton的对象btn;然后调用btn.Create("维新",WS_DISABLED   |WS_CHILD | WS_VISIBLE | BS_AUTO3STATE,
  CRect(0,0,300,100),/*GetParent(),*/this,123);
注意点：
     (1).此处btn不能是局部变量，否则它的生命周期太短，将不能显示。
     (2).在create函数的第二个参数中加入WS_VISIBLE 参数才行。否则必须调用ShowWindow
也可以在view的OnCreate消息响应函数中加入
     (3).CButton类的定义头文件在afxwin.h中，而stdafx.h包含了afxwin.h,所以可以直接使用。因为MFC中的每一个类中都有#include "stdafx.h"的声明。