这个章节主要介绍MFC的执行过程,类似于Win32.。。。。
看了两遍,介绍MFC程序的运行是如何进行的,看的有点乱。
个人觉得不是特别重要,文章貌似主要在讲内部是如何实现的,既然已经封装好了,我们能用就行了。
看的不是特别明白,说说我看了以后了解到的内容
项目创建完成后有五个文件。分别是CMainFrame,CXXXApp,CXXXDoc,CXXXView,CAboutDolg
MFC的执行过程是通过类产生的对象进行控制的theApp,不同于Win32程序产生句柄,所以要对程序对应的对象进行初始化。
所以执行过程和Win32程序的执行有点区别,感觉作者给我们讲解的目的貌似是在说明:MFC的执行过程和Win32的
执行过程类似,只不过MFC进行了封装。
/
// Standard WinMain implementation
// Can be replaced as long as 'AfxWinInit' is called first
int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
ASSERT(hPrevInstance == NULL);
int nReturnCode = -1;
CWinThread* pThread = AfxGetThread(); //指向上面的theApp
CWinApp* pApp = AfxGetApp(); //指向上面的theApp
// AFX internal initialization
if (!AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow))
goto InitFailure;
// App global initializations (rare)
if (pApp != NULL && !pApp->InitApplication())
goto InitFailure;
// Perform specific initializations
if (!pThread->InitInstance()) //进行初始化
{
if (pThread->m_pMainWnd != NULL)
{
TRACE0("Warning: Destroying non-NULL m_pMainWnd\n");
pThread->m_pMainWnd->DestroyWindow();
}
nReturnCode = pThread->ExitInstance();
goto InitFailure;
}
nReturnCode = pThread->Run(); //进行消息循环
InitFailure:
#ifdef _DEBUG
// Check for missing AfxLockTempMap calls
if (AfxGetModuleThreadState()->m_nTempMapLock != 0)
{
TRACE1("Warning: Temp map lock count non-zero (%ld).\n",
AfxGetModuleThreadState()->m_nTempMapLock);
}
AfxLockTempMaps();
AfxUnlockTempMaps(-1);
#endif
AfxWinTerm();
return nReturnCode;
}
/
BOOL AFXAPI AfxEndDeferRegisterClass(LONG fToRegister)
{
// mask off all classes that are already registered
AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
fToRegister &= ~pModuleState->m_fRegisteredClasses;
if (fToRegister == 0)
return TRUE;
LONG fRegisteredClasses = 0;
// common initialization
WNDCLASS wndcls; //设计窗口类
memset(&wndcls, 0, sizeof(WNDCLASS)); // start with NULL defaults
wndcls.lpfnWndProc = DefWindowProc;
wndcls.hInstance = AfxGetInstanceHandle();
wndcls.hCursor = afxData.hcurArrow;
INITCOMMONCONTROLSEX init;
init.dwSize = sizeof(init);
// work to register classes as specified by fToRegister, populate fRegisteredClasses as we go
if (fToRegister & AFX_WND_REG)
{
// Child windows - no brush, no icon, safest default class styles
wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wndcls.lpszClassName = _afxWnd;
if (AfxRegisterClass(&wndcls)) //注册窗口类
fRegisteredClasses |= AFX_WND_REG;
}
.
.
.
.
.
}
/
*
*
*
就不一一列举了,就那几个流程了
窗口类和窗口的关系
窗口类里面有具体的窗口定义。
当窗口类over了,窗口肯定也over了(窗口在窗口类里面)
当窗口over了,窗口类没over哦,只要窗口类愿意,窗口可以继续生成
在窗口中显示按钮。
想在哪里增加按钮就在那里添加WM_CREATE
然后添加BUTTON之类的东西,记得BUTTON的定义要放到类定义里面,不然花括号}结束 创建的BUTTON对象也就没有了,也就不会程序中显示出来
另外创建BUTTON之类的东西 记得要调用显示函数(showWindow),或者在BUTTON里的式样里指定显示WS_VISIBLE
代码如下:
int CAView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
m_btn.Create("刘凯",WS_CHILD |WS_VISIBLE| BS_AUTO3STATE ,CRect(0,0,100,100),this,1);
//m_btn.ShowWindow(SW_SHOWNORMAL);
return 0;
}