WTL-一个用于简化WTL应用程序创建工作的类
手动编写WTL程序的入口点代码比较繁琐,将其封装成一个应用程序类之后可简化很多:
template<typename T, typename TMainWindow, bool bInitializeCom = true>class CWTLApplicationT :public CAppModule{public://Call this method in the application's entry point(_tWinMain(),ect...)int WinMain(HINSTANCE hInstance,LPTSTR lpCmdLine,int nCmdShow){UNREFERENCED_PARAMETER(lpCmdLine);T* pT = static_cast<T*>(this);pT->OnStartup();HRESULT hr = S_OK;if (bInitializeCom){hr = ::CoInitialize(NULL);// 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.// hr = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);CHECKHR(hr);}// This resolves ATL window thunking problem when Microsoft Layer// for Unicode (MSLU) is used::DefWindowProc(NULL, 0, 0, 0L);pT->OnInitCommonControl();hr = Init(NULL, hInstance);CHECKHR(hr);int nRet = 0;{CMessageLoop msgLoop;AddMessageLoop(&msgLoop);TMainWindow wndMain;if(wndMain.Create(NULL,CWindow::rcDefault) == NULL){ATLTRACE(_T("Failed to create Main window!\n"));return 0;}wndMain.ShowWindow(nCmdShow);wndMain.UpdateWindow();nRet = msgLoop.Run();RemoveMessageLoop();}Term();if (bInitializeCom)::CoUninitialize();pT->OnShutdown();return nRet;}protected://Overridable methodsvoid OnStartup(){}void OnShutdown(){}void OnInitCommonControl(){// Add flags to support other controlsAtlInitCommonControls(ICC_COOL_CLASSES|ICC_BAR_CLASSES|ICC_WIN95_CLASSES);}};template<typename TMainWindow, bool bInitializeCom = true>class CWTLApplication :public CWTLApplicationT<CWTLApplication<TMainWindow,bInitializeCom>, TMainWindow, bInitializeCom>{};
使用方法:
1.如果需要修改CWTLApplicationT的功能,可从CWTLApplicationT派生自己的新类,覆盖OnStartup()等函数,以添加自定义功能:
class CMainApp :public CWTLApplicationT<CMainApp,CMainWindow,false>{public:void OnStartup(){//Do some work when starting up...}void OnShutdown(){//Do some work when shuting down...}};CMainApp g_MainApp;
2.如果不需要修改CWTLApplicationT提供的功能,可直接使用typedef来定义一个新类:
typedef CWTLApplication<CMainWindow,false> CMainApp;CMainApp g_MainApp;
3.定义完毕后,添加_tWinMain()入口函数:
int WINAPI _tWinMain(HINSTANCE hInstance,HINSTANCE,LPTSTR lpCmdLine,int nCmdShow){g_MainApp.WinMain(hInstance,lpCmdLine,nCmdShow);}
浙公网安备 33010602011771号