• WTL--SDI框架分析


    创建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 controls
    
    	hRes = _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);		// 保存全局应用theLoop
    
    	CMainFrame wndMain;
    
    	if(wndMain.CreateEx() == NULL)
    	{
    		ATLTRACE(_T("Main window creation failed!
    "));
    		return 0;
    	}
    
    	wndMain.ShowWindow(nCmdShow);
    
    	int nRet = theLoop.Run();				// 不断的从消息队列里取消息,然后分发给对应的窗口
    
    	_Module.RemoveMessageLoop();
    	return nRet;
    }
    

    run函数的主要功能: 

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

    2.建立消息的循环。

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

  • 相关阅读:
    Docker 命令自动补全?要的
    Windows 的这款工具,有时让我觉得 Mac 不是很香
    cheat.sh在手,天下我有
    妙用 Intellij IDEA 创建临时文件,Git 跟踪不到的那种
    AWS Lambda 借助 Serverless Framework,迅速起飞
    你说说对Java中SPI的理解吧
    你说一下对Java中的volatile的理解吧
    TCP 队列溢出了
    #未来时间思考
    #此平台不支持虚拟化的Intel VT-x/EPT #VMware Workstation 与 Device/Credential Guard 不兼容,且在禁用Device/Credential Guard后才能运行VMware
  • 原文地址:https://www.cnblogs.com/tangshiguang/p/6735723.html
Copyright © 2020-2023  润新知