WTL-A basic WTL application

1.包含常用頭文件和宏定義,用於簡化WTL程序編寫的自定義頭文件:WTLHelper.h
隱藏行號 複製代碼 這是一段程序代碼。
  1. //WTLHelper.h
  2. #pragma once
  3. #define WINVER 0x0601           //win7
  4. #define _WIN32_WINNT 0x0601     //win7
  5. #define _WIN32_IE 0x0800        //IE 8.0
  6. #define _RICHEDIT_VER 0x0300
  7. #define _WTL_USE_CSTRING    //use CString class in WTL
  8. //ATL header files
  9. #include 
  10. #include 
  11. //WTL header files
  12. #include //CWindowImpl
  13. #include //BEGIN_MSG_MAP_EX, message crack
  14. #include //CString, CRect, CSize
  15. #ifndef WTLHELPER_NOT_USE_COMMON_CONTROL_STYLE
  16. #if defined _M_IX86
  17. #pragma comment(linker, "/manifestdependency:/"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'/"")
  18. #elif defined _M_IA64
  19. #pragma comment(linker, "/manifestdependency:/"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'/"")
  20. #elif defined _M_X64
  21. #pragma comment(linker, "/manifestdependency:/"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'/"")
  22. #else
  23. #pragma comment(linker, "/manifestdependency:/"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'/"")
  24. #endif
  25. #endif // WTL_NOT_ADD_COMMON_CONTROL_MENIFEST
  26. #ifndef ATLASSERTHR
  27. #define ATLASSERTHR(hr) ATLASSERT(SUCCEEDED((hr)))
  28. #endif  // ATLASSERTHR
2.預編譯頭文件:stdafx.h
隱藏行號 複製代碼 這是一段程序代碼。
  1. //stdafx.h
    
  2. #pragma once
    
  3. #include "WTLHelper.h"
    
  4. 
    
  5. extern CAppModule g_AppModule;
    

 

3.主窗口定義:MainWindow.h
隱藏行號 複製代碼 這是一段程序代碼。
  1. //MainWindow.h
    
  2. #pragma once
    
  3. #include "stdafx.h"
    
  4. 
    
  5. typedef CWinTraits<WS_OVERLAPPEDWINDOW> CMainWinTraits;
    
  6. class CMainWindow :
    
  7.     public CWindowImpl<CMainWindow, CWindow, CMainWinTraits>
    
  8. {
    
  9. public:
    
  10.     DECLARE_WND_CLASS(_T("Basic_Main_Window"))
    
  11.     BEGIN_MSG_MAP_EX(CMainWindow)
    
  12.         MSG_WM_CREATE(OnCreate)
    
  13.         MSG_WM_DESTROY(OnDestroy)
    
  14.         MSG_WM_PAINT(OnPaint)
    
  15.         MSG_WM_LBUTTONDOWN(OnLButtonDown)
    
  16.     END_MSG_MAP()
    
  17. public:
    
  18.     CMainWindow()
    
  19.     {
    
  20.         CWndClassInfo& wci = GetWndClassInfo();
    
  21.         if(!wci.m_atom)    // The window class has not been register yet
    
  22.         {
    
  23.             //Set the background brush of the window class
    
  24.             wci.m_wc.hbrBackground = AtlGetStockBrush(GRAY_BRUSH);
    
  25.         }
    
  26.     }
    
  27.     int OnCreate(LPCREATESTRUCT /*lpCreateStruct*/)
    
  28.     {
    
  29.         //Create font to draw text in the client window
    
  30.         CLogFont logFont;
    
  31.         TCHAR fontName[] = _T("Arial");
    
  32.         _tcscpy_s(logFont.lfFaceName, fontName);
    
  33.         logFont.lfHeight    = 60;
    
  34.         logFont.lfItalic    = TRUE;
    
  35.         logFont.lfWeight    = FW_BOLD;
    
  36.         logFont.lfStrikeOut    = TRUE;
    
  37.         m_FontText.CreateFontIndirect(&logFont);
    
  38.         return 0;
    
  39.     }
    
  40.     void OnDestroy()
    
  41.     {
    
  42.         PostQuitMessage(0);
    
  43.     }
    
  44.     void OnPaint(CDCHandle)
    
  45.     {
    
  46.         CPaintDC dc(m_hWnd);
    
  47.         //Do painting work here
    
  48.         CRect rc;
    
  49.         GetClientRect(&rc);
    
  50.         dc.SaveDC();
    
  51.         dc.SelectFont(m_FontText);
    
  52.         LPCTSTR lpText = _T("A basic WTL application.");
    
  53.         dc.DrawText(lpText, -1, &rc, DT_CENTER|DT_VCENTER| DT_SINGLELINE);
    
  54.         dc.RestoreDC(-1);
    
  55.     }
    
  56.     void OnLButtonDown(UINT nFlags, CPoint point)
    
  57.     {
    
  58.         MessageBox(_T("left button down!"),_T("Main window message"), MB_OK|MB_ICONINFORMATION);
    
  59.     }
    
  60. private:
    
  61.     CFont m_FontText;
    
  62. };
    

 

4.主程序文件:BasicApp.cpp

 

隱藏行號 複製代碼 這是一段程序代碼。
  1. #include "stdafx.h"
    
  2. #include "main.h"
    
  3. 
    
  4. CAppModule g_AppModule;
    
  5. int Run(int nShowCmd)
    
  6. {
    
  7.     CMessageLoop msgLoop;
    
  8.     g_AppModule.AddMessageLoop(&msgLoop);
    
  9.     CMainWindow mainWnd;
    
  10.     mainWnd.Create(NULL, NULL, _T("WTL main window"));
    
  11.     mainWnd.ShowWindow(nShowCmd);
    
  12.     mainWnd.CenterWindow();
    
  13.     //Start message loop
    
  14.     int result = msgLoop.Run();
    
  15.     g_AppModule.RemoveMessageLoop();
    
  16.     return result;
    
  17. }
    
  18. int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE pPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    
  19. {
    
  20.     HRESULT hr = g_AppModule.Init(NULL, hInstance);
    
  21.     ATLASSERTHR(hr);
    
  22.     int result = Run(nShowCmd);
    
  23.     g_AppModule.Term();
    
  24.     return result;
    
  25. }
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章