// 繼承鏈示意 class CHelloATLWnd : public CWindowImpl> { ... }; // CWindowImpl 三個模板參數 template class CWindowImpl : public CWindowImplRoot , public CMessageMap { ... }; // CWindowImplRoot 繼承 TBase (CWindow) 與 CMessageMap // T 用於 CRTP(編譯期多型),TBase 封裝 HWND 與視窗 API,TWinTraits 封裝樣式常數
templateclass CWinTraits { public: static DWORD GetWndStyle(DWORD dwStyle) { return dwStyle == 0 ? t_dwStyle : dwStyle; } static DWORD GetWndExStyle(DWORD dwExStyle) { return dwExStyle == 0 ? t_dwExStyle : dwExStyle; } };
使用:`CWinTraits
BEGIN_MSG_MAP` / `END_MSG_MAP` 巨集建立靜態訊息映射表。ALT_MSG_MAP(id)` 實現同一類別處理多個視窗(如子類別化)。class CMainWnd : public CWindowImpl> { public: DECLARE_WND_CLASS(_T("MyMainWnd")) BEGIN_MSG_MAP(CMainWnd) MESSAGE_HANDLER(WM_PAINT, OnPaint) MESSAGE_HANDLER(WM_DESTROY, OnDestroy) END_MSG_MAP() LRESULT OnPaint(UINT, WPARAM, LPARAM, BOOL&) { PAINTSTRUCT ps; HDC hdc = BeginPaint(m_hWnd, &ps); DrawText(hdc, _T("Hello, ATL!"), -1, &ps.rcPaint, DT_CENTER|DT_VCENTER|DT_SINGLELINE); EndPaint(m_hWnd, &ps); return 0; } LRESULT OnDestroy(UINT, WPARAM, LPARAM, BOOL&) { PostQuitMessage(0); return 0; } };