123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- // Windows Template Library - WTL version 10.0
- // Copyright (C) Microsoft Corporation, WTL Team. All rights reserved.
- //
- // This file is a part of the Windows Template Library.
- // The use and distribution terms for this software are covered by the
- // Microsoft Public License (http://opensource.org/licenses/MS-PL)
- // which can be found in the file MS-PL.txt at the root folder.
- #ifndef __ATLDWM_H__
- #define __ATLDWM_H__
- #pragma once
- #ifndef __ATLAPP_H__
- #error atldwm.h requires atlapp.h to be included first
- #endif
- #ifndef __ATLWIN_H__
- #error atldwm.h requires atlwin.h to be included first
- #endif
- #if (_WIN32_WINNT < 0x0600)
- #error atldwm.h requires _WIN32_WINNT >= 0x0600
- #endif
- #ifndef _DWMAPI_H_
- #include <dwmapi.h>
- #endif
- #pragma comment(lib, "dwmapi.lib")
- // Note: To create an application that also runs on older versions of Windows,
- // use delay load of dwmapi.dll and ensure that no calls to the DWM API are
- // Delay load is NOT AUTOMATIC for VC++ 7, you have to link to delayimp.lib,
- // and add dwmapi.dll in the Linker.Input.Delay Loaded DLLs section of the
- // project properties.
- ///////////////////////////////////////////////////////////////////////////////
- // Classes in this file:
- //
- // CDwm
- // CDwmImpl<T, TBase>
- // CDwmWindowT<TBase> - CDwmWindow
- // CDwmThumbnailT<t_bManaged, TBase>
- // CDwmThumbnail
- // CDwmThumbnailHandle
- // CAeroControlImpl
- namespace WTL
- {
- ///////////////////////////////////////////////////////////////////////////////
- // CDwm - wrapper for DWM handle
- class CDwm
- {
- public:
- // Data members
- static int m_nIsDwmSupported;
- // Constructor
- CDwm()
- {
- IsDwmSupported();
- }
- // Dwm support helper
- static bool IsDwmSupported()
- {
- if(m_nIsDwmSupported == -1)
- {
- CStaticDataInitCriticalSectionLock lock;
- if(FAILED(lock.Lock()))
- {
- ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CDwm::IsDwmSupported.\n"));
- ATLASSERT(FALSE);
- return false;
- }
- if(m_nIsDwmSupported == -1)
- {
- HMODULE hDwmDLL = ::LoadLibrary(_T("dwmapi.dll"));
- m_nIsDwmSupported = (hDwmDLL != NULL) ? 1 : 0;
- if(hDwmDLL != NULL)
- ::FreeLibrary(hDwmDLL);
- }
- lock.Unlock();
- }
- ATLASSERT(m_nIsDwmSupported != -1);
- return (m_nIsDwmSupported == 1);
- }
- // Operations
- BOOL DwmIsCompositionEnabled() const
- {
- if(!IsDwmSupported())
- return FALSE;
- BOOL bRes = FALSE;
- return (SUCCEEDED(::DwmIsCompositionEnabled(&bRes)) && bRes) ? TRUE : FALSE;
- }
- BOOL DwmEnableComposition(UINT fEnable)
- {
- if(!IsDwmSupported())
- return FALSE;
- return SUCCEEDED(::DwmEnableComposition(fEnable)) ? TRUE : FALSE;
- }
- BOOL DwmEnableMMCSS(BOOL fEnableMMCSS)
- {
- if(!IsDwmSupported())
- return FALSE;
- return SUCCEEDED(::DwmEnableMMCSS(fEnableMMCSS)) ? TRUE : FALSE;
- }
- HRESULT DwmGetColorizationColor(DWORD* pcrColorization, BOOL* pfOpaqueBlend)
- {
- if(!IsDwmSupported())
- return E_NOTIMPL;
- return ::DwmGetColorizationColor(pcrColorization, pfOpaqueBlend);
- }
- HRESULT DwmFlush()
- {
- if(!IsDwmSupported())
- return E_NOTIMPL;
- return ::DwmFlush();
- }
- };
- __declspec(selectany) int CDwm::m_nIsDwmSupported = -1;
- ///////////////////////////////////////////////////////////////////////////////
- // CDwmImpl - DWM window support
- template <class T, class TBase = CDwm>
- class CDwmImpl : public TBase
- {
- public:
- HRESULT DwmEnableBlurBehindWindow(const DWM_BLURBEHIND* pBB)
- {
- if(!this->IsDwmSupported())
- return E_NOTIMPL;
- T* pT = static_cast<T*>(this);
- ATLASSERT(::IsWindow(pT->m_hWnd));
- return ::DwmEnableBlurBehindWindow(pT->m_hWnd, pBB);
- }
- HRESULT DwmExtendFrameIntoClientArea(const MARGINS* pMargins)
- {
- if(!this->IsDwmSupported())
- return E_NOTIMPL;
- T* pT = static_cast<T*>(this);
- ATLASSERT(::IsWindow(pT->m_hWnd));
- return ::DwmExtendFrameIntoClientArea(pT->m_hWnd, pMargins);
- }
- HRESULT DwmExtendFrameIntoEntireClientArea()
- {
- MARGINS margins = { -1 };
- return DwmExtendFrameIntoClientArea(&margins);
- }
- HRESULT DwmGetCompositionTimingInfo(DWM_TIMING_INFO* pTimingInfo)
- {
- if(!this->IsDwmSupported())
- return E_NOTIMPL;
- T* pT = static_cast<T*>(this);
- ATLASSERT(::IsWindow(pT->m_hWnd));
- return ::DwmGetCompositionTimingInfo(pT->m_hWnd, pTimingInfo);
- }
- HRESULT DwmGetWindowAttribute(DWORD dwAttribute, PVOID pvAttribute, DWORD cbAttribute)
- {
- if(!this->IsDwmSupported())
- return E_NOTIMPL;
- T* pT = static_cast<T*>(this);
- ATLASSERT(::IsWindow(pT->m_hWnd));
- return ::DwmGetWindowAttribute(pT->m_hWnd, dwAttribute, pvAttribute, cbAttribute);
- }
- HRESULT DwmModifyPreviousDxFrameDuration(INT cRefreshes, BOOL fRelative)
- {
- if(!this->IsDwmSupported())
- return E_NOTIMPL;
- T* pT = static_cast<T*>(this);
- ATLASSERT(::IsWindow(pT->m_hWnd));
- return ::DwmModifyPreviousDxFrameDuration(pT->m_hWnd, cRefreshes, fRelative);
- }
- HRESULT DwmSetDxFrameDuration(INT cRefreshes)
- {
- if(!this->IsDwmSupported())
- return E_NOTIMPL;
- T* pT = static_cast<T*>(this);
- ATLASSERT(::IsWindow(pT->m_hWnd));
- return ::DwmSetDxFrameDuration(pT->m_hWnd, cRefreshes);
- }
- HRESULT DwmSetPresentParameters(DWM_PRESENT_PARAMETERS* pPresentParams)
- {
- if(!this->IsDwmSupported())
- return E_NOTIMPL;
- T* pT = static_cast<T*>(this);
- ATLASSERT(::IsWindow(pT->m_hWnd));
- return ::DwmSetPresentParameters(pT->m_hWnd, pPresentParams);
- }
- HRESULT DwmSetWindowAttribute(DWORD dwAttribute, LPCVOID pvAttribute, DWORD cbAttribute)
- {
- if(!this->IsDwmSupported())
- return E_NOTIMPL;
- T* pT = static_cast<T*>(this);
- ATLASSERT(::IsWindow(pT->m_hWnd));
- return ::DwmSetWindowAttribute(pT->m_hWnd, dwAttribute, pvAttribute, cbAttribute);
- }
- HRESULT DwmAttachMilContent()
- {
- if(!this->IsDwmSupported())
- return E_NOTIMPL;
- T* pT = static_cast<T*>(this);
- ATLASSERT(::IsWindow(pT->m_hWnd));
- return ::DwmAttachMilContent(pT->m_hWnd);
- }
- HRESULT DwmDetachMilContent()
- {
- if(!this->IsDwmSupported())
- return E_NOTIMPL;
- T* pT = static_cast<T*>(this);
- ATLASSERT(::IsWindow(pT->m_hWnd));
- return ::DwmDetachMilContent(pT->m_hWnd);
- }
- };
- template <class TBase>
- class CDwmWindowT : public TBase, public CDwmImpl<CDwmWindowT< TBase > >
- {
- public:
- CDwmWindowT(HWND hWnd = NULL) : TBase(hWnd)
- { }
- CDwmWindowT< TBase >& operator =(HWND hWnd)
- {
- this->m_hWnd = hWnd;
- return *this;
- }
- };
- typedef CDwmWindowT<ATL::CWindow> CDwmWindow;
- ///////////////////////////////////////////////////////////////////////////////
- // CDwmThumbnail - provides DWM thumbnail support
- template <bool t_bManaged, class TBase = CDwm>
- class CDwmThumbnailT : public TBase
- {
- public:
- // Data members
- HTHUMBNAIL m_hThumbnail;
- // Constructor
- CDwmThumbnailT(HTHUMBNAIL hThumbnail = NULL) : m_hThumbnail(hThumbnail)
- { }
- ~CDwmThumbnailT()
- {
- if(t_bManaged && (m_hThumbnail != NULL))
- Unregister();
- }
- // Operations
- CDwmThumbnailT<t_bManaged, TBase>& operator =(HTHUMBNAIL hThumbnail)
- {
- Attach(hThumbnail);
- return *this;
- }
- void Attach(HTHUMBNAIL hThumbnailNew)
- {
- if(t_bManaged && (m_hThumbnail != NULL) && (m_hThumbnail != hThumbnailNew))
- Unregister();
- m_hThumbnail = hThumbnailNew;
- }
- HTHUMBNAIL Detach()
- {
- HTHUMBNAIL hThumbnail = m_hThumbnail;
- m_hThumbnail = NULL;
- return hThumbnail;
- }
- HRESULT Register(HWND hwndDestination, HWND hwndSource)
- {
- ATLASSERT(::IsWindow(hwndDestination));
- ATLASSERT(::IsWindow(hwndSource));
- ATLASSERT(m_hThumbnail==NULL);
- if(!this->IsDwmSupported())
- return E_NOTIMPL;
- return ::DwmRegisterThumbnail(hwndDestination, hwndSource, &m_hThumbnail);
- }
- HRESULT Unregister()
- {
- if(!this->IsDwmSupported())
- return E_NOTIMPL;
- if(m_hThumbnail == NULL)
- return S_FALSE;
- HRESULT Hr = ::DwmUnregisterThumbnail(m_hThumbnail);
- if(SUCCEEDED(Hr))
- m_hThumbnail = NULL;
- return Hr;
- }
- operator HTHUMBNAIL() const { return m_hThumbnail; }
- bool IsNull() const { return (m_hThumbnail == NULL); }
- HRESULT UpdateProperties(const DWM_THUMBNAIL_PROPERTIES* ptnProperties)
- {
- if(!this->IsDwmSupported())
- return E_NOTIMPL;
- ATLASSERT(m_hThumbnail != NULL);
- return ::DwmUpdateThumbnailProperties(m_hThumbnail, ptnProperties);
- }
- // Attributes
- HRESULT QuerySourceSize(PSIZE pSize)
- {
- if(!this->IsDwmSupported())
- return E_NOTIMPL;
- ATLASSERT(m_hThumbnail != NULL);
- return ::DwmQueryThumbnailSourceSize(m_hThumbnail, pSize);
- }
- };
- typedef CDwmThumbnailT<true, CDwm> CDwmThumbnail;
- typedef CDwmThumbnailT<false, CDwm> CDwmThumbnailHandle;
- #ifdef __ATLTHEME_H__
- ///////////////////////////////////////////////////////////////////////////////
- // CAeroControlImpl - Base class for controls on Glass
- template <class T, class TBase = ATL::CWindow, class TWinTraits = ATL::CControlWinTraits>
- class CAeroControlImpl : public CThemeImpl<T>,
- public CBufferedPaintImpl<T>,
- public ATL::CWindowImpl<T, TBase, TWinTraits>
- {
- public:
- typedef CThemeImpl<T> _themeClass;
- typedef CBufferedPaintImpl<T> _baseClass;
- typedef ATL::CWindowImpl<T, TBase, TWinTraits> _windowClass;
- CAeroControlImpl()
- {
- this->m_PaintParams.dwFlags = BPPF_ERASE;
- }
- static LPCWSTR GetThemeName()
- {
- #ifdef _UNICODE
- return TBase::GetWndClassName();
- #else
- ATLASSERT(!_T("Return UNICODE string of window classname / theme class"));
- return NULL;
- #endif // _UNICODE
- }
- // Message map and handlers
- BEGIN_MSG_MAP(CAeroControlImpl)
- MESSAGE_HANDLER(WM_CREATE, OnCreate)
- MESSAGE_HANDLER(WM_ACTIVATE, OnActivate)
- CHAIN_MSG_MAP(_themeClass)
- CHAIN_MSG_MAP(_baseClass)
- END_MSG_MAP()
- LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
- {
- T* pT = static_cast<T*>(this);
- pT->Init();
- bHandled = FALSE;
- return 0;
- }
- LRESULT OnActivate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
- {
- if(this->IsThemingSupported())
- this->Invalidate(FALSE);
- bHandled = FALSE;
- return 0;
- }
- // Operations
- BOOL SubclassWindow(HWND hWnd)
- {
- ATLASSERT(this->m_hWnd == NULL);
- ATLASSERT(::IsWindow(hWnd));
- BOOL bRet = _windowClass::SubclassWindow(hWnd);
- if(bRet)
- {
- T* pT = static_cast<T*>(this);
- pT->Init();
- }
- return bRet;
- }
- // Implementation
- LRESULT DefWindowProc()
- {
- const ATL::_ATL_MSG* pMsg = this->m_pCurrentMsg;
- LRESULT lRes = 0;
- if(pMsg != NULL)
- lRes = DefWindowProc(pMsg->message, pMsg->wParam, pMsg->lParam);
- return lRes;
- }
- LRESULT DefWindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- T* pT = static_cast<T*>(this);
- LRESULT lRes = 0;
- if(::DwmDefWindowProc(pT->m_hWnd, uMsg, wParam, lParam, &lRes) != FALSE)
- return lRes;
- return _windowClass::DefWindowProc(uMsg, wParam, lParam);
- }
- void DoBufferedPaint(HDC hDC, RECT& rcPaint)
- {
- T* pT = static_cast<T*>(this);
- HDC hDCPaint = NULL;
- RECT rcClient = {};
- this->GetClientRect(&rcClient);
- this->m_BufferedPaint.Begin(hDC, &rcClient, this->m_dwFormat, &this->m_PaintParams, &hDCPaint);
- ATLASSERT(hDCPaint != NULL);
- pT->DoAeroPaint(hDCPaint, rcClient, rcPaint);
- this->m_BufferedPaint.End();
- }
- void DoPaint(HDC /*hdc*/, RECT& /*rcClient*/)
- {
- DefWindowProc();
- }
- // Overridables
- void Init()
- {
- T* pT = static_cast<T*>(this);
- (void)pT; // avoid level 4 warning
- this->SetThemeClassList(pT->GetThemeName());
- if(this->m_lpstrThemeClassList != NULL)
- this->OpenThemeData();
- }
- void DoAeroPaint(HDC hDC, RECT& /*rcClient*/, RECT& rcPaint)
- {
- DefWindowProc(WM_PAINT, (WPARAM) hDC, 0L);
- this->m_BufferedPaint.MakeOpaque(&rcPaint);
- }
- };
- #endif // __ATLTHEME_H__
- } // namespace WTL
- #endif // __ATLDWM_H__
|