📄 atlapp.h
字号:
// Windows Template Library - WTL version 7.1
// Copyright (C) 1997-2003 Microsoft Corporation
// All rights reserved.
//
// This file is a part of the Windows Template Library.
// The code and information is provided "as-is" without
// warranty of any kind, either expressed or implied.
#ifndef __ATLAPP_H__
#define __ATLAPP_H__
#pragma once
#ifndef __cplusplus
#error ATL requires C++ compilation (use a .cpp suffix)
#endif
#ifndef __ATLBASE_H__
#error atlapp.h requires atlbase.h to be included first
#endif
#ifndef _WIN32_WCE
#if (WINVER < 0x0400)
#error WTL requires Windows version 4.0 or higher
#endif
#if (_WIN32_IE < 0x0300)
#error WTL requires IE version 3.0 or higher
#endif
#endif
#ifdef _ATL_NO_COMMODULE
#error WTL requires that _ATL_NO_COMMODULE is not defined
#endif //_ATL_NO_COMMODULE
#if defined(_WIN32_WCE) && defined(_ATL_MIN_CRT)
#pragma message("Warning: WTL for Windows CE doesn't use _ATL_MIN_CRT")
#endif //defined(_WIN32_WCE) && defined(_ATL_MIN_CRT)
#include <limits.h>
#if !defined(_ATL_MIN_CRT) && defined(_MT)
#include <process.h> // for _beginthreadex
#endif
#include <commctrl.h>
#ifndef _WIN32_WCE
#pragma comment(lib, "comctl32.lib")
#endif //!_WIN32_WCE
#ifndef _WIN32_WCE
#include <atlres.h>
#else // CE specific
#include <atlresce.h>
#endif //_WIN32_WCE
///////////////////////////////////////////////////////////////////////////////
// WTL version number
#define _WTL_VER 0x0710
///////////////////////////////////////////////////////////////////////////////
// Classes in this file:
//
// CMessageFilter
// CIdleHandler
// CMessageLoop
//
// CAppModule
// CServerAppModule
//
// Global functions:
// AtlGetDefaultGuiFont()
// AtlCreateBoldFont()
// AtlInitCommonControls()
///////////////////////////////////////////////////////////////////////////////
// Global support for Windows CE
#ifdef _WIN32_WCE
#ifndef SW_SHOWDEFAULT
#define SW_SHOWDEFAULT SW_SHOWNORMAL
#endif //!SW_SHOWDEFAULT
#ifdef lstrlenW
#undef lstrlenW
#define lstrlenW (int)wcslen
#endif //lstrlenW
#define lstrlenA (int)strlen
#ifndef lstrcpyn
inline LPTSTR lstrcpyn(LPTSTR lpstrDest, LPCTSTR lpstrSrc, int nLength)
{
if(lpstrDest == NULL || lpstrSrc == NULL || nLength <= 0)
return NULL;
int nLen = min(lstrlen(lpstrSrc), nLength - 1);
LPTSTR lpstrRet = (LPTSTR)memcpy(lpstrDest, lpstrSrc, nLen * sizeof(TCHAR));
lpstrDest[nLen] = 0;
return lpstrRet;
}
#endif //!lstrcpyn
#ifdef TrackPopupMenu
#undef TrackPopupMenu
#endif // TrackPopupMenu
// This get's ORed in a constant and will have no effect.
// Defining it does reduced the number of #ifdefs required for CE.
#define LR_DEFAULTSIZE 0
#define DECLARE_WND_CLASS_EX(WndClassName, style, bkgnd) \
static CWndClassInfo& GetWndClassInfo() \
{ \
static CWndClassInfo wc = \
{ \
{ style, StartWindowProc, \
0, 0, NULL, NULL, NULL, (HBRUSH)(bkgnd + 1), NULL, WndClassName }, \
NULL, NULL, IDC_ARROW, TRUE, 0, _T("") \
}; \
return wc; \
}
#define _MAX_FNAME _MAX_PATH
inline int MulDiv(IN int nNumber, IN int nNumerator, IN int nDenominator)
{
__int64 multiple = nNumber * nNumerator;
return static_cast<int>(multiple / nDenominator);
}
inline BOOL IsMenu(HMENU hMenu)
{
MENUITEMINFO mii = { 0 };
mii.cbSize = sizeof(mii);
::SetLastError(0);
BOOL bRet = ::GetMenuItemInfo(hMenu, 0, TRUE, &mii);
if(!bRet)
bRet = (::GetLastError() != ERROR_INVALID_MENU_HANDLE) ? TRUE : FALSE;
return bRet;
}
extern "C" void WINAPI ListView_SetItemSpacing(HWND hwndLV, int iHeight);
#if (_WIN32_WCE < 400)
#define MAKEINTATOM(i) (LPTSTR)((ULONG_PTR)((WORD)(i)))
#define WHEEL_PAGESCROLL (UINT_MAX)
#define WHEEL_DELTA 120
#endif //(_WIN32_WCE < 400)
#endif //_WIN32_WCE
///////////////////////////////////////////////////////////////////////////////
// Global support for using original VC++ 6.0 headers with WTL
#ifndef _ATL_NO_OLD_HEADERS_WIN64
#if !defined(_WIN64) && (_ATL_VER < 0x0700)
#ifndef PSM_INSERTPAGE
#define PSM_INSERTPAGE (WM_USER + 119)
#endif //!PSM_INSERTPAGE
#ifndef GetClassLongPtr
#define GetClassLongPtrA GetClassLongA
#define GetClassLongPtrW GetClassLongW
#ifdef UNICODE
#define GetClassLongPtr GetClassLongPtrW
#else
#define GetClassLongPtr GetClassLongPtrA
#endif // !UNICODE
#endif // !GetClassLongPtr
#ifndef GCLP_HICONSM
#define GCLP_HICONSM (-34)
#endif //!GCLP_HICONSM
#ifndef GetWindowLongPtr
#define GetWindowLongPtrA GetWindowLongA
#define GetWindowLongPtrW GetWindowLongW
#ifdef UNICODE
#define GetWindowLongPtr GetWindowLongPtrW
#else
#define GetWindowLongPtr GetWindowLongPtrA
#endif // !UNICODE
#endif // !GetWindowLongPtr
#ifndef SetWindowLongPtr
#define SetWindowLongPtrA SetWindowLongA
#define SetWindowLongPtrW SetWindowLongW
#ifdef UNICODE
#define SetWindowLongPtr SetWindowLongPtrW
#else
#define SetWindowLongPtr SetWindowLongPtrA
#endif // !UNICODE
#endif // !SetWindowLongPtr
#ifndef GWLP_WNDPROC
#define GWLP_WNDPROC (-4)
#endif
#ifndef GWLP_HINSTANCE
#define GWLP_HINSTANCE (-6)
#endif
#ifndef GWLP_HWNDPARENT
#define GWLP_HWNDPARENT (-8)
#endif
#ifndef GWLP_USERDATA
#define GWLP_USERDATA (-21)
#endif
#ifndef GWLP_ID
#define GWLP_ID (-12)
#endif
#ifndef DWLP_MSGRESULT
#define DWLP_MSGRESULT 0
#endif
typedef long LONG_PTR;
typedef unsigned long ULONG_PTR;
typedef ULONG_PTR DWORD_PTR;
#ifndef HandleToUlong
#define HandleToUlong( h ) ((ULONG)(ULONG_PTR)(h) )
#endif
#ifndef HandleToLong
#define HandleToLong( h ) ((LONG)(LONG_PTR) (h) )
#endif
#ifndef LongToHandle
#define LongToHandle( h) ((HANDLE)(LONG_PTR) (h))
#endif
#ifndef PtrToUlong
#define PtrToUlong( p ) ((ULONG)(ULONG_PTR) (p) )
#endif
#ifndef PtrToLong
#define PtrToLong( p ) ((LONG)(LONG_PTR) (p) )
#endif
#ifndef PtrToUint
#define PtrToUint( p ) ((UINT)(UINT_PTR) (p) )
#endif
#ifndef PtrToInt
#define PtrToInt( p ) ((INT)(INT_PTR) (p) )
#endif
#ifndef PtrToUshort
#define PtrToUshort( p ) ((unsigned short)(ULONG_PTR)(p) )
#endif
#ifndef PtrToShort
#define PtrToShort( p ) ((short)(LONG_PTR)(p) )
#endif
#ifndef IntToPtr
#define IntToPtr( i ) ((VOID *)(INT_PTR)((int)i))
#endif
#ifndef UIntToPtr
#define UIntToPtr( ui ) ((VOID *)(UINT_PTR)((unsigned int)ui))
#endif
#ifndef LongToPtr
#define LongToPtr( l ) ((VOID *)(LONG_PTR)((long)l))
#endif
#ifndef ULongToPtr
#define ULongToPtr( ul ) ((VOID *)(ULONG_PTR)((unsigned long)ul))
#endif
#endif //!defined(_WIN64) && (_ATL_VER < 0x0700)
#endif //!_ATL_NO_OLD_HEADERS_WIN64
///////////////////////////////////////////////////////////////////////////////
// Miscellaneous global support
// define useful macros from winuser.h
#ifndef IS_INTRESOURCE
#define IS_INTRESOURCE(_r) (((ULONG_PTR)(_r) >> 16) == 0)
#endif //IS_INTRESOURCE
// protect template members from windowsx.h macros
#ifdef _INC_WINDOWSX
#undef SubclassWindow
#endif //_INC_WINDOWSX
// define useful macros from windowsx.h
#ifndef GET_X_LPARAM
#define GET_X_LPARAM(lParam) ((int)(short)LOWORD(lParam))
#endif
#ifndef GET_Y_LPARAM
#define GET_Y_LPARAM(lParam) ((int)(short)HIWORD(lParam))
#endif
// Dummy structs for compiling with /CLR
#if (_MSC_VER >= 1300) && defined(_MANAGED)
__if_not_exists(_IMAGELIST::_IMAGELIST) { struct _IMAGELIST { }; }
__if_not_exists(_TREEITEM::_TREEITEM) { struct _TREEITEM { }; }
__if_not_exists(_PSP::_PSP) { struct _PSP { }; }
#endif
namespace WTL
{
#if (_ATL_VER >= 0x0700)
DECLARE_TRACE_CATEGORY(atlTraceUI);
#ifdef _DEBUG
__declspec(selectany) ATL::CTraceCategory atlTraceUI(_T("atlTraceUI"));
#endif // _DEBUG
#else //!(_ATL_VER >= 0x0700)
enum wtlTraceFlags
{
atlTraceUI = 0x10000000
};
#endif //!(_ATL_VER >= 0x0700)
// Windows version helper
inline bool AtlIsOldWindows()
{
OSVERSIONINFO ovi = { 0 };
ovi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
BOOL bRet = ::GetVersionEx(&ovi);
return (!bRet || !((ovi.dwMajorVersion >= 5) || (ovi.dwMajorVersion == 4 && ovi.dwMinorVersion >= 90)));
}
// default GUI font helper
inline HFONT AtlGetDefaultGuiFont()
{
#ifndef _WIN32_WCE
return (HFONT)::GetStockObject(DEFAULT_GUI_FONT);
#else // CE specific
return (HFONT)::GetStockObject(SYSTEM_FONT);
#endif //_WIN32_WCE
}
// bold font helper (NOTE: Caller owns the font, and should destroy it when done using it)
inline HFONT AtlCreateBoldFont(HFONT hFont = NULL)
{
if(hFont == NULL)
hFont = AtlGetDefaultGuiFont();
ATLASSERT(hFont != NULL);
HFONT hFontBold = NULL;
LOGFONT lf = { 0 };
if(::GetObject(hFont, sizeof(LOGFONT), &lf) == sizeof(LOGFONT))
{
lf.lfWeight = FW_BOLD;
hFontBold = ::CreateFontIndirect(&lf);
ATLASSERT(hFontBold != NULL);
}
else
{
ATLASSERT(FALSE);
}
return hFontBold;
}
// Common Controls initialization helper
inline BOOL AtlInitCommonControls(DWORD dwFlags)
{
INITCOMMONCONTROLSEX iccx = { sizeof(INITCOMMONCONTROLSEX), dwFlags };
BOOL bRet = ::InitCommonControlsEx(&iccx);
ATLASSERT(bRet);
return bRet;
}
///////////////////////////////////////////////////////////////////////////////
// CMessageFilter - Interface for message filter support
class CMessageFilter
{
public:
virtual BOOL PreTranslateMessage(MSG* pMsg) = 0;
};
///////////////////////////////////////////////////////////////////////////////
// CIdleHandler - Interface for idle processing
class CIdleHandler
{
public:
virtual BOOL OnIdle() = 0;
};
#ifndef _ATL_NO_OLD_NAMES
// for compatilibility with old names only
typedef CIdleHandler CUpdateUIObject;
#define DoUpdate OnIdle
#endif //!_ATL_NO_OLD_NAMES
///////////////////////////////////////////////////////////////////////////////
// CMessageLoop - message loop implementation
class CMessageLoop
{
public:
ATL::CSimpleArray<CMessageFilter*> m_aMsgFilter;
ATL::CSimpleArray<CIdleHandler*> m_aIdleHandler;
MSG m_msg;
// Message filter operations
BOOL AddMessageFilter(CMessageFilter* pMessageFilter)
{
return m_aMsgFilter.Add(pMessageFilter);
}
BOOL RemoveMessageFilter(CMessageFilter* pMessageFilter)
{
return m_aMsgFilter.Remove(pMessageFilter);
}
// Idle handler operations
BOOL AddIdleHandler(CIdleHandler* pIdleHandler)
{
return m_aIdleHandler.Add(pIdleHandler);
}
BOOL RemoveIdleHandler(CIdleHandler* pIdleHandler)
{
return m_aIdleHandler.Remove(pIdleHandler);
}
#ifndef _ATL_NO_OLD_NAMES
// for compatilibility with old names only
BOOL AddUpdateUI(CIdleHandler* pIdleHandler)
{
ATLTRACE2(atlTraceUI, 0, "CUpdateUIObject and AddUpdateUI are deprecated. Please change your code to use CIdleHandler and OnIdle\n");
return AddIdleHandler(pIdleHandler);
}
BOOL RemoveUpdateUI(CIdleHandler* pIdleHandler)
{
ATLTRACE2(atlTraceUI, 0, "CUpdateUIObject and RemoveUpdateUI are deprecated. Please change your code to use CIdleHandler and OnIdle\n");
return RemoveIdleHandler(pIdleHandler);
}
#endif //!_ATL_NO_OLD_NAMES
// message loop
int Run()
{
BOOL bDoIdle = TRUE;
int nIdleCount = 0;
BOOL bRet;
for(;;)
{
while(bDoIdle && !::PeekMessage(&m_msg, NULL, 0, 0, PM_NOREMOVE))
{
if(!OnIdle(nIdleCount++))
bDoIdle = FALSE;
}
bRet = ::GetMessage(&m_msg, NULL, 0, 0);
if(bRet == -1)
{
ATLTRACE2(atlTraceUI, 0, _T("::GetMessage returned -1 (error)\n"));
continue; // error, don't process
}
else if(!bRet)
{
ATLTRACE2(atlTraceUI, 0, _T("CMessageLoop::Run - exiting\n"));
break; // WM_QUIT, exit message loop
}
if(!PreTranslateMessage(&m_msg))
{
::TranslateMessage(&m_msg);
::DispatchMessage(&m_msg);
}
if(IsIdleMessage(&m_msg))
{
bDoIdle = TRUE;
nIdleCount = 0;
}
}
return (int)m_msg.wParam;
}
static BOOL IsIdleMessage(MSG* pMsg)
{
// These messages should NOT cause idle processing
switch(pMsg->message)
{
case WM_MOUSEMOVE:
#ifndef _WIN32_WCE
case WM_NCMOUSEMOVE:
#endif //!_WIN32_WCE
case WM_PAINT:
case 0x0118: // WM_SYSTIMER (caret blink)
return FALSE;
}
return TRUE;
}
// Overrideables
// Override to change message filtering
virtual BOOL PreTranslateMessage(MSG* pMsg)
{
// loop backwards
for(int i = m_aMsgFilter.GetSize() - 1; i >= 0; i--)
{
CMessageFilter* pMessageFilter = m_aMsgFilter[i];
if(pMessageFilter != NULL && pMessageFilter->PreTranslateMessage(pMsg))
return TRUE;
}
return FALSE; // not translated
}
// override to change idle processing
virtual BOOL OnIdle(int /*nIdleCount*/)
{
for(int i = 0; i < m_aIdleHandler.GetSize(); i++)
{
CIdleHandler* pIdleHandler = m_aIdleHandler[i];
if(pIdleHandler != NULL)
pIdleHandler->OnIdle();
}
return FALSE; // don't continue
}
};
///////////////////////////////////////////////////////////////////////////////
// CStaticDataInitCriticalSectionLock and CWindowCreateCriticalSectionLock
// internal classes to manage critical sections for both ATL3 and ATl7
class CStaticDataInitCriticalSectionLock
{
public:
#if (_ATL_VER >= 0x0700)
ATL::CComCritSecLock<ATL::CComCriticalSection> m_cslock;
CStaticDataInitCriticalSectionLock() : m_cslock(ATL::_pAtlModule->m_csStaticDataInitAndTypeInfo, false)
{ }
#endif //(_ATL_VER >= 0x0700)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -