```cpp
// MFCTrain101023.cpp : 定义应用程序的类行为。
//
#include "stdafx.h"
#include "MFCTrain101023.h"
#include "MFCTrain101023Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CMFCTrain101023App
BEGIN_MESSAGE_MAP(CMFCTrain101023App, CWinApp)
ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()
// CMFCTrain101023App 构造
CMFCTrain101023App::CMFCTrain101023App()
{
// 支持重新启动管理器
m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;
// TODO: 在此处添加构造代码,
// 将所有重要的初始化放置在 InitInstance 中
}
// 唯一的一个 CMFCTrain101023App 对象
CMFCTrain101023App theApp;
// CMFCTrain101023App 初始化
BOOL CMFCTrain101023App::InitInstance()
{
// 如果一个运行在 Windows XP 上的应用程序清单指定要
// 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式,
//则需要 InitCommonControlsEx()。否则,将无法创建窗口。
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// 将它设置为包括所有要在应用程序中使用的
// 公共控件类。
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinApp::InitInstance();
AfxEnableControlContainer();
// 创建 shell 管理器,以防对话框包含
// 任何 shell 树视图控件或 shell 列表视图控件。
CShellManager *pShellManager = new CShellManager;
// 标准初始化
// 如果未使用这些功能并希望减小
// 最终可执行文件的大小,则应移除下列
// 不需要的特定初始化例程
// 更改用于存储设置的注册表项
// TODO: 应适当修改该字符串,
// 例如修改为公司或组织名
SetRegistryKey(_T("应用程序向导生成的本地应用程序"));
CMFCTrain101023Dlg* pDlg=new CMFCTrain101023Dlg; //在堆中生成对话框对象
m_pMainWnd=pDlg; //将生成的对话框对象作为应用程序对象的main window成员
//使用全局函数AfxGetApp()获得当前应用程序对象指针
CWinApp* pApp=AfxGetApp();
CMFCTrain101023App* pAppthis=&theApp;
if(pApp==pAppthis)
{int istop=0;}
CWnd* pWnd=pApp->m_pMainWnd;
if(pWnd==pDlg)
{int istop=0;}
//Debugin
//查看AfxGetMoudleState()->m_pCurrentWinApp;等内容的意义
//类class AFX_MODULE_STATE (global data for a module) 一个模块的全局相关数据 可由全局函数AfxGetModuleState()得到
AFX_MODULE_STATE* MYAFXAPI=AfxGetModuleState();
CWinApp* pCurApp=AfxGetModuleState()->m_pCurrentWinApp; //当前模块所在的应用程序对象的指针
HINSTANCE hCurInstance=AfxGetModuleState()->m_hCurrentInstanceHandle; //当前实例句柄是个什么?
HINSTANCE hCurRes=AfxGetModuleState()->m_hCurrentResourceHandle; //当前资源句柄是个什么?和当前实例句柄地址一样
LPCTSTR CurAppName=AfxGetModuleState()->m_lpszCurrentAppName; //当前应用程序名 "MFCTrain101023"
BYTE bDll=AfxGetModuleState()->m_bDLL; // TRUE if module is a DLL, FALSE if it is an EXE 0表示当前模块是在EXE而不是某个DLL中
DWORD fRegisteredClass=AfxGetModuleState()->m_fRegisteredClasses; //flags for registered window classes 0表示不是 当前自定义的窗口类不是注册窗口类
//Debugout
INT_PTR nResponse = pDlg->DoModal();
if (nResponse == IDOK)
{
// TODO: 在此放置处理何时用
// “确定”来关闭对话框的代码
}
else if (nResponse == IDCANCEL)
{
// TODO: 在此放置处理何时用
// “取消”来关闭对话框的代码
}
// 删除上面创建的 shell 管理器。
if (pShellManager != NULL)
{
delete pShellManager;
}
// 由于对话框已关闭,所以将返回 FALSE 以便退出应用程序,
// 而不是启动应用程序的消息泵。
return FALSE;
}
/*
//pDlg->DoModal();的详细语法
INT_PTR CDialog::DoModal()
{
// can be constructed with a resource template or InitModalIndirect
ASSERT(m_lpszTemplateName != NULL || m_hDialogTemplate != NULL ||
m_lpDialogTemplate != NULL);
// load resource as necessary
LPCDLGTEMPLATE lpDialogTemplate = m_lpDialogTemplate;
HGLOBAL hDialogTemplate = m_hDialogTemplate;
HINSTANCE hInst = AfxGetResourceHandle();
if (m_lpszTemplateName != NULL)
{
hInst = AfxFindResourceHandle(m_lpszTemplateName, RT_DIALOG);
HRSRC hResource = ::FindResource(hInst, m_lpszTemplateName, RT_DIALOG);
hDialogTemplate = LoadResource(hInst, hResource);
}
if (hDialogTemplate != NULL)
lpDialogTemplate = (LPCDLGTEMPLATE)LockResource(hDialogTemplate);
// return -1 in case of failure to load the dialog template resource
if (lpDialogTemplate == NULL)
return -1;
// disable parent (before creating dialog)
HWND hWndParent = PreModal(); //父窗口
AfxUnhookWindowCreate();
BOOL bEnableParent = FALSE;
#ifndef _AFX_NO_OLE_SUPPORT
CWnd* pMainWnd = NULL;
BOOL bEnableMainWnd = FALSE;
#endif
if (hWndParent && hWndParent != ::GetDesktopWindow() && ::IsWindowEnabled(hWndParent)) //父窗口存在且不是桌面窗口且为鼠标和键盘输入启用了该父窗口
{
::EnableWindow(hWndParent, FALSE); //禁用向父窗口窗口(或控件)的鼠标和键盘输入
bEnableParent = TRUE;
#ifndef _AFX_NO_OLE_SUPPORT
pMainWnd = AfxGetMainWnd(); //调用全局函数{调用全局函数返回当前应用程序线程CWinThread,返回当前应用程序线程的m_pMainWnd窗口指针 }
if (pMainWnd && pMainWnd->IsFrameWnd() && pMainWnd->IsWindowEnabled()) //当前窗口存在且是框架窗口且为鼠标和键盘输入启用了该窗口
{
//
// We are hosted by non-MFC container
//
pMainWnd->EnableWindow(FALSE); //启用向当前窗口(或控件)的鼠标和键盘输入
bEnableMainWnd = TRUE;
}
#endif
}
TRY
{
// create modeless dialog
AfxHookWindowCreate(this);
if (CreateDlgIndirect(lpDialogTemplate,
CWnd::FromHandle(hWndParent), hInst))
{
if (m_nFlags & WF_CONTINUEMODAL)
{
// enter modal loop
DWORD dwFlags = MLF_SHOWONIDLE;
if (GetStyle() & DS_NOIDLEMSG)
dwFlags |= MLF_NOIDLEMSG;
VERIFY(RunModalLoop(dwFlags) == m_nModalResult);
}
// hide the window before enabling the parent, etc. 在启用父窗口前隐藏该窗口
if (m_hWnd != NULL)
SetWindowPos(NULL, 0, 0, 0, 0, SWP_HIDEWINDOW|
SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
}
}
CATCH_ALL(e)
{
DELETE_EXCEPTION(e);
m_nModalResult = -1;
}
END_CATCH_ALL
#ifndef _AFX_NO_OLE_SUPPORT
if (bEnableMainWnd)
pMainWnd->EnableWindow(TRUE); //启用向当前窗口(或控件)的鼠标和键盘输入
#endif
if (bEnableParent)
::EnableWindow(hWndParent, TRUE); //启用向父窗口(或控件)的鼠标和键盘输入
if (hWndParent != NULL && ::GetActiveWindow() == m_hWnd)
::SetActiveWindow(hWndParent); //该SetActiveWindow函数激活一个窗口,但如果应用程序是在后台。当系统激活窗口时,如果其应用程序位于前台,则该窗口将被置于前台(Z-Order的顶部)
// destroy modal window
DestroyWindow(); //销毁Modal模式窗口
PostModal(); //Modal模式窗口后处理
// unlock/free resources as necessary
if (m_lpszTemplateName != NULL || m_hDialogTemplate != NULL)
UnlockResource(hDialogTemplate);
if (m_lpszTemplateName != NULL)
FreeResource(hDialogTemplate);
return m_nModalResult;
}
*/