📄 atlapp.h
字号:
#if (_ATL_VER < 0x0700)
#ifndef ATLVERIFY
#ifdef _DEBUG
#define ATLVERIFY(expr) ATLASSERT(expr)
#else
#define ATLVERIFY(expr) (expr)
#endif // DEBUG
#endif // ATLVERIFY
#endif //(_ATL_VER < 0x0700)
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, _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)
}
};
///////////////////////////////////////////////////////////////////////////////
// CAppModule - module class for an application
class CAppModule : public ATL::CComModule
{
public:
DWORD m_dwMainThreadID;
ATL::CSimpleMap<DWORD, CMessageLoop*>* m_pMsgLoopMap;
ATL::CSimpleArray<HWND>* m_pSettingChangeNotify;
// Overrides of CComModule::Init and Term
HRESULT Init(ATL::_ATL_OBJMAP_ENTRY* pObjMap, HINSTANCE hInstance, const GUID* pLibID = NULL)
{
HRESULT hRet = CComModule::Init(pObjMap, hInstance, pLibID);
if(FAILED(hRet))
return hRet;
m_dwMainThreadID = ::GetCurrentThreadId();
typedef ATL::CSimpleMap<DWORD, CMessageLoop*> _mapClass;
m_pMsgLoopMap = NULL;
ATLTRY(m_pMsgLoopMap = new _mapClass);
if(m_pMsgLoopMap == NULL)
return E_OUTOFMEMORY;
m_pSettingChangeNotify = NULL;
return hRet;
}
void Term()
{
TermSettingChangeNotify();
delete m_pMsgLoopMap;
CComModule::Term();
}
// Message loop map methods
BOOL AddMessageLoop(CMessageLoop* pMsgLoop)
{
CStaticDataInitCriticalSectionLock lock;
if(FAILED(lock.Lock()))
{
ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::AddMessageLoop.\n"));
ATLASSERT(FALSE);
return FALSE;
}
ATLASSERT(pMsgLoop != NULL);
ATLASSERT(m_pMsgLoopMap->Lookup(::GetCurrentThreadId()) == NULL); // not in map yet
BOOL bRet = m_pMsgLoopMap->Add(::GetCurrentThreadId(), pMsgLoop);
lock.Unlock();
return bRet;
}
BOOL RemoveMessageLoop()
{
CStaticDataInitCriticalSectionLock lock;
if(FAILED(lock.Lock()))
{
ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::RemoveMessageLoop.\n"));
ATLASSERT(FALSE);
return FALSE;
}
BOOL bRet = m_pMsgLoopMap->Remove(::GetCurrentThreadId());
lock.Unlock();
return bRet;
}
CMessageLoop* GetMessageLoop(DWORD dwThreadID = ::GetCurrentThreadId()) const
{
CStaticDataInitCriticalSectionLock lock;
if(FAILED(lock.Lock()))
{
ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::GetMessageLoop.\n"));
ATLASSERT(FALSE);
return NULL;
}
CMessageLoop* pLoop = m_pMsgLoopMap->Lookup(dwThreadID);
lock.Unlock();
return pLoop;
}
// Setting change notify methods
// Note: Call this from the main thread for MSDI apps
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -