1.包含常用頭文件和宏定義,用於簡化WTL程序編寫的自定義頭文件:WTLHelper.h
//WTLHelper.h
#pragma once
#define WINVER 0x0601 //win7
#define _WIN32_WINNT 0x0601 //win7
#define _WIN32_IE 0x0800 //IE 8.0
#define _RICHEDIT_VER 0x0300
#define _WTL_USE_CSTRING //use CString class in WTL
//ATL header files
#include
#include
//WTL header files
#include //CWindowImpl
#include //BEGIN_MSG_MAP_EX, message crack
#include //CString, CRect, CSize
#ifndef WTLHELPER_NOT_USE_COMMON_CONTROL_STYLE
#if defined _M_IX86
#pragma comment(linker, "/manifestdependency:/"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'/"")
#elif defined _M_IA64
#pragma comment(linker, "/manifestdependency:/"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'/"")
#elif defined _M_X64
#pragma comment(linker, "/manifestdependency:/"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'/"")
#else
#pragma comment(linker, "/manifestdependency:/"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'/"")
#endif
#endif // WTL_NOT_ADD_COMMON_CONTROL_MENIFEST
#ifndef ATLASSERTHR
#define ATLASSERTHR(hr) ATLASSERT(SUCCEEDED((hr)))
#endif // ATLASSERTHR
2.預編譯頭文件:stdafx.h
//stdafx.h
#pragma once
#include "WTLHelper.h"
extern CAppModule g_AppModule;
3.主窗口定義:MainWindow.h
//MainWindow.h
#pragma once
#include "stdafx.h"
typedef CWinTraits<WS_OVERLAPPEDWINDOW> CMainWinTraits;
class CMainWindow :
public CWindowImpl<CMainWindow, CWindow, CMainWinTraits>
{
public:
DECLARE_WND_CLASS(_T("Basic_Main_Window"))
BEGIN_MSG_MAP_EX(CMainWindow)
MSG_WM_CREATE(OnCreate)
MSG_WM_DESTROY(OnDestroy)
MSG_WM_PAINT(OnPaint)
MSG_WM_LBUTTONDOWN(OnLButtonDown)
END_MSG_MAP()
public:
CMainWindow()
{
CWndClassInfo& wci = GetWndClassInfo();
if(!wci.m_atom) // The window class has not been register yet
{
//Set the background brush of the window class
wci.m_wc.hbrBackground = AtlGetStockBrush(GRAY_BRUSH);
}
}
int OnCreate(LPCREATESTRUCT /*lpCreateStruct*/)
{
//Create font to draw text in the client window
CLogFont logFont;
TCHAR fontName[] = _T("Arial");
_tcscpy_s(logFont.lfFaceName, fontName);
logFont.lfHeight = 60;
logFont.lfItalic = TRUE;
logFont.lfWeight = FW_BOLD;
logFont.lfStrikeOut = TRUE;
m_FontText.CreateFontIndirect(&logFont);
return 0;
}
void OnDestroy()
{
PostQuitMessage(0);
}
void OnPaint(CDCHandle)
{
CPaintDC dc(m_hWnd);
//Do painting work here
CRect rc;
GetClientRect(&rc);
dc.SaveDC();
dc.SelectFont(m_FontText);
LPCTSTR lpText = _T("A basic WTL application.");
dc.DrawText(lpText, -1, &rc, DT_CENTER|DT_VCENTER| DT_SINGLELINE);
dc.RestoreDC(-1);
}
void OnLButtonDown(UINT nFlags, CPoint point)
{
MessageBox(_T("left button down!"),_T("Main window message"), MB_OK|MB_ICONINFORMATION);
}
private:
CFont m_FontText;
};
4.主程序文件:BasicApp.cpp
#include "stdafx.h"
#include "main.h"
CAppModule g_AppModule;
int Run(int nShowCmd)
{
CMessageLoop msgLoop;
g_AppModule.AddMessageLoop(&msgLoop);
CMainWindow mainWnd;
mainWnd.Create(NULL, NULL, _T("WTL main window"));
mainWnd.ShowWindow(nShowCmd);
mainWnd.CenterWindow();
//Start message loop
int result = msgLoop.Run();
g_AppModule.RemoveMessageLoop();
return result;
}
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE pPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
HRESULT hr = g_AppModule.Init(NULL, hInstance);
ATLASSERTHR(hr);
int result = Run(nShowCmd);
g_AppModule.Term();
return result;
}