Windows Template Library (WTL) is a C++ library for developing Windows applications and UI components. It extends ATL (Active Template Library) and provides a set of classes for controls, dialogs, frame windows, GDI objects, and more.
以下代码包含了如下内容:
定义以及创建窗口和对话框
消息映射
WTL程序结构
以及一些其他方面的东西
参考资料:WTL for MFC Programmers
#include <atlbase.h>
#include <atlwin.h>
#include "resource.h"
extern CComModule _Module;
class CAboutDlg : public CDialogImpl<CAboutDlg>
{
public:
enum { IDD = IDD_ABOUT };
BEGIN_MSG_MAP(CAboutDlg)
MESSAGE_HANDLER(WM_INITDIALOG,OnInitDialog)
MESSAGE_HANDLER(WM_CLOSE,OnClose)
COMMAND_ID_HANDLER(IDOK,OnOKCancel)
COMMAND_ID_HANDLER(IDCANCEL,OnOKCancel)
END_MSG_MAP()
public:
LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
CenterWindow();
return TRUE;
}
LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
EndDialog(IDCANCEL);
return 0;
}
LRESULT OnOKCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
EndDialog(wID);
return 0;
}
};
template <class T,COLORREF t_crBrushColor>
class CPaintBkgnd : public CMessageMap
{
public:
CPaintBkgnd() { m_hbrBkgnd = CreateSolidBrush(t_crBrushColor); }
~CPaintBkgnd() { DeleteObject(m_hbrBkgnd); }
BEGIN_MSG_MAP(CPaintBkgnd)
MESSAGE_HANDLER(WM_ERASEBKGND,OnEraseBkgnd)
END_MSG_MAP()
public:
LRESULT OnEraseBkgnd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
T* pT = static_cast<T*>(this);
HDC dc = (HDC) wParam;
RECT rcClient;
pT->GetClientRect ( &rcClient );
FillRect ( dc, &rcClient, m_hbrBkgnd );
return TRUE;
}
protected:
HBRUSH m_hbrBkgnd;
};
typedef CWinTraits<WS_OVERLAPPEDWINDOW> ChypWinTraits;
class CFirstWindow : public CWindowImpl<CFirstWindow,CWindow,ChypWinTraits>,
public CPaintBkgnd<CFirstWindow,RGB(0,0,255)>
{
public:
DECLARE_WND_CLASS("hyp First WTL Window Class")
typedef CPaintBkgnd<CFirstWindow, RGB(0,0,255)> CPaintBkgndBase;
BEGIN_MSG_MAP(CFirstWindow)
MESSAGE_HANDLER(WM_CLOSE,OnClose)
MESSAGE_HANDLER(WM_DESTROY,OnDestroy)
MESSAGE_HANDLER(WM_CREATE,OnCreate)
COMMAND_ID_HANDLER(IDC_ABOUT,OnAbout)
CHAIN_MSG_MAP(CPaintBkgndBase)
END_MSG_MAP()
public:
LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
DestroyWindow();
return 0;
}
LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
PostQuitMessage(0);
return 0;
}
LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
HMENU hmenu = LoadMenu ( _Module.GetResourceInstance(),MAKEINTRESOURCE(IDR_MENU) );
SetMenu ( hmenu );
return 0;
}
LRESULT OnAbout(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
CAboutDlg dlg;
dlg.DoModal();
return 0;
}
};
CComModule _Module;
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
_Module.Init(NULL,hInstance);
CFirstWindow wndMain;
MSG msg;
if ( wndMain.Create(NULL,CWindow::rcDefault,"hyp First WTL Window") == NULL)
{
return FALSE;
}
wndMain.ShowWindow(nCmdShow);
wndMain.UpdateWindow();
while ( GetMessage(&msg,wndMain.m_hWnd,0,0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
_Module.Term();
return (int)msg.wParam;
}
原文:http://hi.baidu.com/hypkb/blog/item/37655d58c26483232834f0c0.html