WTL--SDI框架分析

来源:互联网 发布:aws rds mysql 连接 编辑:程序博客网 时间:2024/06/06 02:55

创建SDI产生的基本类:CMainFrame,CAboutDlg和CWTLView(WTL为项目名)。

由此可见,不同于MFC,WTL少了文档类,它的结构就只有简单的窗口类和视图类,而至于串行化(MFC文档类的主要功能)则可以添加在窗口类或者视图类中,也可以自己编写一个文档类(按个人需求而定)。

SDI应用程序的入口和MFC的入口同名----_tWinMain,以下是源码及注释(被注释的代码是添加和销毁rich edit control的代码):

// 初始化COM环境,公用控件和_Module,调用全局函数Run()int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow){HRESULT hRes = ::CoInitialize(NULL);// 初始化COM环境// If you are running on NT 4.0 or higher you can use the following call instead to // make the EXE free threaded. This means that calls come in on a random RPC thread.//HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);ATLASSERT(SUCCEEDED(hRes));// this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used::DefWindowProc(NULL, 0, 0, 0L);// ?AtlInitCommonControls(ICC_BAR_CLASSES);// add flags to support other controlshRes = _Module.Init(NULL, hInstance);// 应用程序初始化ATLASSERT(SUCCEEDED(hRes));// 添加rich edit control//HINSTANCE hInstRich = ::LoadLibrary(CRichEditCtrl::GetLibraryName());//ATLASSERT(hInstRich != NULL);//AtlAxWinInit();int nRet = Run(lpstrCmdLine, nCmdShow);//::FreeLibrary(hInstRich);_Module.Term();// 销毁应用程序::CoUninitialize();// COM环境销毁return nRet;}

_tWinMain的主要功能就是初始化一些环境并调用run函数,那么我们就来看下run函数都执行了哪些动作:

// Run函数的主要作用是创建主框架窗口,进入消息循环int Run(LPTSTR /*lpstrCmdLine*/ = NULL, int nCmdShow = SW_SHOWDEFAULT){CMessageLoop theLoop;// 消息循环类_Module.AddMessageLoop(&theLoop);// 保存全局应用theLoopCMainFrame wndMain;if(wndMain.CreateEx() == NULL){ATLTRACE(_T("Main window creation failed!\n"));return 0;}wndMain.ShowWindow(nCmdShow);int nRet = theLoop.Run();// 不断的从消息队列里取消息,然后分发给对应的窗口_Module.RemoveMessageLoop();return nRet;}

run函数的主要功能:

1.创建并初始化一个框架对象CMainFrame wndMain;

2.建立消息的循环。

这样,一个窗口的框架就建立完毕了。




未完待续。。。。

0 0
原创粉丝点击