📄 atlapp.h
字号:
#endif // !defined(_WIN32_WCE) && (WINVER >= 0x0600) && !defined(NONCLIENTMETRICS_V1_SIZE)
#endif // !_WTL_NO_RUNTIME_STRUCT_SIZE
namespace RunTimeHelper
{
#ifndef _WIN32_WCE
inline bool IsCommCtrl6()
{
DWORD dwMajor = 0, dwMinor = 0;
HRESULT hRet = ATL::AtlGetCommCtrlVersion(&dwMajor, &dwMinor);
return (SUCCEEDED(hRet) && (dwMajor >= 6));
}
inline bool IsVista()
{
OSVERSIONINFO ovi = { sizeof(OSVERSIONINFO) };
BOOL bRet = ::GetVersionEx(&ovi);
return ((bRet != FALSE) && (ovi.dwMajorVersion >= 6));
}
#endif // !_WIN32_WCE
inline int SizeOf_REBARBANDINFO()
{
int nSize = sizeof(REBARBANDINFO);
#if !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
if(!(IsVista() && IsCommCtrl6()))
nSize = REBARBANDINFO_V6_SIZE;
#endif // !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
return nSize;
}
#if (_WIN32_WINNT >= 0x501)
inline int SizeOf_LVGROUP()
{
int nSize = sizeof(LVGROUP);
#if !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
if(!IsVista())
nSize = LVGROUP_V5_SIZE;
#endif // !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
return nSize;
}
inline int SizeOf_LVTILEINFO()
{
int nSize = sizeof(LVTILEINFO);
#if !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
if(!IsVista())
nSize = LVTILEINFO_V5_SIZE;
#endif // !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
return nSize;
}
#endif // (_WIN32_WINNT >= 0x501)
inline int SizeOf_MCHITTESTINFO()
{
int nSize = sizeof(MCHITTESTINFO);
#if !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && defined(NTDDI_VERSION) && (NTDDI_VERSION >= NTDDI_LONGHORN)
if(!(IsVista() && IsCommCtrl6()))
nSize = MCHITTESTINFO_V1_SIZE;
#endif // !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && defined(NTDDI_VERSION) && (NTDDI_VERSION >= NTDDI_LONGHORN)
return nSize;
}
#ifndef _WIN32_WCE
inline int SizeOf_NONCLIENTMETRICS()
{
int nSize = sizeof(NONCLIENTMETRICS);
#if !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (WINVER >= 0x0600)
if(!IsVista())
nSize = NONCLIENTMETRICS_V1_SIZE;
#endif // !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (WINVER >= 0x0600)
return nSize;
}
#endif // !_WIN32_WCE
};
///////////////////////////////////////////////////////////////////////////////
// ModuleHelper - helper functions for ATL3 and ATL7 module classes
namespace ModuleHelper
{
inline HINSTANCE GetModuleInstance()
{
#if (_ATL_VER >= 0x0700)
return ATL::_AtlBaseModule.GetModuleInstance();
#else // !(_ATL_VER >= 0x0700)
return ATL::_pModule->GetModuleInstance();
#endif // !(_ATL_VER >= 0x0700)
}
inline HINSTANCE GetResourceInstance()
{
#if (_ATL_VER >= 0x0700)
return ATL::_AtlBaseModule.GetResourceInstance();
#else // !(_ATL_VER >= 0x0700)
return ATL::_pModule->GetResourceInstance();
#endif // !(_ATL_VER >= 0x0700)
}
inline void AddCreateWndData(ATL::_AtlCreateWndData* pData, void* pObject)
{
#if (_ATL_VER >= 0x0700)
ATL::_AtlWinModule.AddCreateWndData(pData, pObject);
#else // !(_ATL_VER >= 0x0700)
ATL::_pModule->AddCreateWndData(pData, pObject);
#endif // !(_ATL_VER >= 0x0700)
}
inline void* ExtractCreateWndData()
{
#if (_ATL_VER >= 0x0700)
return ATL::_AtlWinModule.ExtractCreateWndData();
#else // !(_ATL_VER >= 0x0700)
return ATL::_pModule->ExtractCreateWndData();
#endif // !(_ATL_VER >= 0x0700)
}
};
///////////////////////////////////////////////////////////////////////////////
// SecureHelper - helper functions for VS2005 secure CRT
namespace SecureHelper
{
inline void strcpyA_x(char* lpstrDest, size_t cchDest, const char* lpstrSrc)
{
#if _SECURE_ATL
ATL::Checked::strcpy_s(lpstrDest, cchDest, lpstrSrc);
#else
if(cchDest > (size_t)lstrlenA(lpstrSrc))
ATLVERIFY(lstrcpyA(lpstrDest, lpstrSrc) != NULL);
else
ATLASSERT(FALSE);
#endif
}
inline void strcpyW_x(wchar_t* lpstrDest, size_t cchDest, const wchar_t* lpstrSrc)
{
#if _SECURE_ATL
ATL::Checked::wcscpy_s(lpstrDest, cchDest, lpstrSrc);
#else
if(cchDest > (size_t)lstrlenW(lpstrSrc))
ATLVERIFY(lstrcpyW(lpstrDest, lpstrSrc) != NULL);
else
ATLASSERT(FALSE);
#endif
}
inline void strcpy_x(LPTSTR lpstrDest, size_t cchDest, LPCTSTR lpstrSrc)
{
#ifdef _UNICODE
strcpyW_x(lpstrDest, cchDest, lpstrSrc);
#else
strcpyA_x(lpstrDest, cchDest, lpstrSrc);
#endif
}
inline errno_t strncpyA_x(char* lpstrDest, size_t cchDest, const char* lpstrSrc, size_t cchCount)
{
#if _SECURE_ATL
return ATL::Checked::strncpy_s(lpstrDest, cchDest, lpstrSrc, cchCount);
#else
errno_t nRet = 0;
if(lpstrDest == NULL || cchDest == 0 || lpstrSrc == NULL)
{
nRet = EINVAL;
}
else if(cchCount == _TRUNCATE)
{
cchCount = min(cchDest - 1, size_t(lstrlenA(lpstrSrc)));
nRet = STRUNCATE;
}
else if(cchDest <= cchCount)
{
lpstrDest[0] = 0;
nRet = EINVAL;
}
if(nRet == 0 || nRet == STRUNCATE)
nRet = (lstrcpynA(lpstrDest, lpstrSrc, (int)cchCount + 1) != NULL) ? nRet : EINVAL;
ATLASSERT(nRet == 0 || nRet == STRUNCATE);
return nRet;
#endif
}
inline errno_t strncpyW_x(wchar_t* lpstrDest, size_t cchDest, const wchar_t* lpstrSrc, size_t cchCount)
{
#if _SECURE_ATL
return ATL::Checked::wcsncpy_s(lpstrDest, cchDest, lpstrSrc, cchCount);
#else
errno_t nRet = 0;
if(lpstrDest == NULL || cchDest == 0 || lpstrSrc == NULL)
{
nRet = EINVAL;
}
else if(cchCount == _TRUNCATE)
{
cchCount = min(cchDest - 1, size_t(lstrlenW(lpstrSrc)));
nRet = STRUNCATE;
}
else if(cchDest <= cchCount)
{
lpstrDest[0] = 0;
nRet = EINVAL;
}
if(nRet == 0 || nRet == STRUNCATE)
nRet = (lstrcpynW(lpstrDest, lpstrSrc, (int)cchCount + 1) != NULL) ? nRet : EINVAL;
ATLASSERT(nRet == 0 || nRet == STRUNCATE);
return nRet;
#endif
}
inline errno_t strncpy_x(LPTSTR lpstrDest, size_t cchDest, LPCTSTR lpstrSrc, size_t cchCount)
{
#ifdef _UNICODE
return strncpyW_x(lpstrDest, cchDest, lpstrSrc, cchCount);
#else
return strncpyA_x(lpstrDest, cchDest, lpstrSrc, cchCount);
#endif
}
inline void strcatA_x(char* lpstrDest, size_t cchDest, const char* lpstrSrc)
{
#if _SECURE_ATL
ATL::Checked::strcat_s(lpstrDest, cchDest, lpstrSrc);
#else
if(cchDest > (size_t)lstrlenA(lpstrSrc))
ATLVERIFY(lstrcatA(lpstrDest, lpstrSrc) != NULL);
else
ATLASSERT(FALSE);
#endif
}
inline void strcatW_x(wchar_t* lpstrDest, size_t cchDest, const wchar_t* lpstrSrc)
{
#if _SECURE_ATL
ATL::Checked::wcscat_s(lpstrDest, cchDest, lpstrSrc);
#else
if(cchDest > (size_t)lstrlenW(lpstrSrc))
ATLVERIFY(lstrcatW(lpstrDest, lpstrSrc) != NULL);
else
ATLASSERT(FALSE);
#endif
}
inline void strcat_x(LPTSTR lpstrDest, size_t cchDest, LPCTSTR lpstrSrc)
{
#ifdef _UNICODE
strcatW_x(lpstrDest, cchDest, lpstrSrc);
#else
strcatA_x(lpstrDest, cchDest, lpstrSrc);
#endif
}
inline void memcpy_x(void* pDest, size_t cbDest, const void* pSrc, size_t cbSrc)
{
#if _SECURE_ATL
ATL::Checked::memcpy_s(pDest, cbDest, pSrc, cbSrc);
#else
if(cbDest >= cbSrc)
memcpy(pDest, pSrc, cbSrc);
else
ATLASSERT(FALSE);
#endif
}
inline void memmove_x(void* pDest, size_t cbDest, const void* pSrc, size_t cbSrc)
{
#if _SECURE_ATL
ATL::Checked::memmove_s(pDest, cbDest, pSrc, cbSrc);
#else
if(cbDest >= cbSrc)
memmove(pDest, pSrc, cbSrc);
else
ATLASSERT(FALSE);
#endif
}
inline int vsprintf_x(LPTSTR lpstrBuff, size_t cchBuff, LPCTSTR lpstrFormat, va_list args)
{
#if _SECURE_ATL && !defined(_ATL_MIN_CRT) && !defined(_WIN32_WCE)
return _vstprintf_s(lpstrBuff, cchBuff, lpstrFormat, args);
#else
cchBuff; // Avoid unused argument warning
return _vstprintf(lpstrBuff, lpstrFormat, args);
#endif
}
inline int wvsprintf_x(LPTSTR lpstrBuff, size_t cchBuff, LPCTSTR lpstrFormat, va_list args)
{
#if _SECURE_ATL && !defined(_ATL_MIN_CRT) && !defined(_WIN32_WCE)
return _vstprintf_s(lpstrBuff, cchBuff, lpstrFormat, args);
#else
cchBuff; // Avoid unused argument warning
return ::wvsprintf(lpstrBuff, lpstrFormat, args);
#endif
}
inline int sprintf_x(LPTSTR lpstrBuff, size_t cchBuff, LPCTSTR lpstrFormat, ...)
{
va_list args;
va_start(args, lpstrFormat);
int nRes = vsprintf_x(lpstrBuff, cchBuff, lpstrFormat, args);
va_end(args);
return nRes;
}
inline int wsprintf_x(LPTSTR lpstrBuff, size_t cchBuff, LPCTSTR lpstrFormat, ...)
{
va_list args;
va_start(args, lpstrFormat);
int nRes = wvsprintf_x(lpstrBuff, cchBuff, lpstrFormat, args);
va_end(args);
return nRes;
}
}; // namespace SecureHelper
///////////////////////////////////////////////////////////////////////////////
// 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, _T("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, _T("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)
HRESULT Lock()
{
#if (_ATL_VER >= 0x0700)
return m_cslock.Lock();
#else // !(_ATL_VER >= 0x0700)
::EnterCriticalSection(&ATL::_pModule->m_csStaticDataInit);
return S_OK;
#endif // !(_ATL_VER >= 0x0700)
}
void Unlock()
{
#if (_ATL_VER >= 0x0700)
m_cslock.Unlock();
#else // !(_ATL_VER >= 0x0700)
::LeaveCriticalSection(&ATL::_pModule->m_csStaticDataInit);
#endif // !(_ATL_VER >= 0x0700)
}
};
class CWindowCreateCriticalSectionLock
{
public:
#if (_ATL_VER >= 0x0700)
ATL::CComCritSecLock<ATL::CComCriticalSection> m_cslock;
CWindowCreateCriticalSectionLock() : m_cslock(ATL::_AtlWinModule.m_csWindowCreate, false)
{ }
#endif // (_ATL_VER >= 0x0700)
HRESULT Lock()
{
#if (_ATL_VER >= 0x0700)
return m_cslock.Lock();
#else // !(_ATL_VER >= 0x0700)
::EnterCriticalSection(&ATL::_pModule->m_csWindowCreate);
return S_OK;
#endif // !(_ATL_VER >= 0x0700)
}
void Unlock()
{
#if (_ATL_VER >= 0x0700)
m_cslock.Unlock();
#else // !(_ATL_VER >= 0x0700)
::LeaveCriticalSection(&ATL::_pModule->m_csWindowCreate);
#endif // !(_ATL_VER >= 0x0700)
}
};
///////////////////////////////////////////////////////////////////////////////
// CTempBuffer - helper class for stack allocations for ATL3
#ifndef _WTL_STACK_ALLOC_THRESHOLD
#define _WTL_STACK_ALLOC_THRESHOLD 512
#endif
#if (_ATL_VER >= 0x0700)
using ATL::CTempBuffer;
#else // !(_ATL_VER >= 0x0700)
#ifndef SIZE_MAX
#ifdef _WIN64
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -