atldwm.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. // Windows Template Library - WTL version 10.0
  2. // Copyright (C) Microsoft Corporation, WTL Team. All rights reserved.
  3. //
  4. // This file is a part of the Windows Template Library.
  5. // The use and distribution terms for this software are covered by the
  6. // Microsoft Public License (http://opensource.org/licenses/MS-PL)
  7. // which can be found in the file MS-PL.txt at the root folder.
  8. #ifndef __ATLDWM_H__
  9. #define __ATLDWM_H__
  10. #pragma once
  11. #ifndef __ATLAPP_H__
  12. #error atldwm.h requires atlapp.h to be included first
  13. #endif
  14. #ifndef __ATLWIN_H__
  15. #error atldwm.h requires atlwin.h to be included first
  16. #endif
  17. #if (_WIN32_WINNT < 0x0600)
  18. #error atldwm.h requires _WIN32_WINNT >= 0x0600
  19. #endif
  20. #ifndef _DWMAPI_H_
  21. #include <dwmapi.h>
  22. #endif
  23. #pragma comment(lib, "dwmapi.lib")
  24. // Note: To create an application that also runs on older versions of Windows,
  25. // use delay load of dwmapi.dll and ensure that no calls to the DWM API are
  26. // Delay load is NOT AUTOMATIC for VC++ 7, you have to link to delayimp.lib,
  27. // and add dwmapi.dll in the Linker.Input.Delay Loaded DLLs section of the
  28. // project properties.
  29. ///////////////////////////////////////////////////////////////////////////////
  30. // Classes in this file:
  31. //
  32. // CDwm
  33. // CDwmImpl<T, TBase>
  34. // CDwmWindowT<TBase> - CDwmWindow
  35. // CDwmThumbnailT<t_bManaged, TBase>
  36. // CDwmThumbnail
  37. // CDwmThumbnailHandle
  38. // CAeroControlImpl
  39. namespace WTL
  40. {
  41. ///////////////////////////////////////////////////////////////////////////////
  42. // CDwm - wrapper for DWM handle
  43. class CDwm
  44. {
  45. public:
  46. // Data members
  47. static int m_nIsDwmSupported;
  48. // Constructor
  49. CDwm()
  50. {
  51. IsDwmSupported();
  52. }
  53. // Dwm support helper
  54. static bool IsDwmSupported()
  55. {
  56. if(m_nIsDwmSupported == -1)
  57. {
  58. CStaticDataInitCriticalSectionLock lock;
  59. if(FAILED(lock.Lock()))
  60. {
  61. ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CDwm::IsDwmSupported.\n"));
  62. ATLASSERT(FALSE);
  63. return false;
  64. }
  65. if(m_nIsDwmSupported == -1)
  66. {
  67. HMODULE hDwmDLL = ::LoadLibrary(_T("dwmapi.dll"));
  68. m_nIsDwmSupported = (hDwmDLL != NULL) ? 1 : 0;
  69. if(hDwmDLL != NULL)
  70. ::FreeLibrary(hDwmDLL);
  71. }
  72. lock.Unlock();
  73. }
  74. ATLASSERT(m_nIsDwmSupported != -1);
  75. return (m_nIsDwmSupported == 1);
  76. }
  77. // Operations
  78. BOOL DwmIsCompositionEnabled() const
  79. {
  80. if(!IsDwmSupported())
  81. return FALSE;
  82. BOOL bRes = FALSE;
  83. return (SUCCEEDED(::DwmIsCompositionEnabled(&bRes)) && bRes) ? TRUE : FALSE;
  84. }
  85. BOOL DwmEnableComposition(UINT fEnable)
  86. {
  87. if(!IsDwmSupported())
  88. return FALSE;
  89. return SUCCEEDED(::DwmEnableComposition(fEnable)) ? TRUE : FALSE;
  90. }
  91. BOOL DwmEnableMMCSS(BOOL fEnableMMCSS)
  92. {
  93. if(!IsDwmSupported())
  94. return FALSE;
  95. return SUCCEEDED(::DwmEnableMMCSS(fEnableMMCSS)) ? TRUE : FALSE;
  96. }
  97. HRESULT DwmGetColorizationColor(DWORD* pcrColorization, BOOL* pfOpaqueBlend)
  98. {
  99. if(!IsDwmSupported())
  100. return E_NOTIMPL;
  101. return ::DwmGetColorizationColor(pcrColorization, pfOpaqueBlend);
  102. }
  103. HRESULT DwmFlush()
  104. {
  105. if(!IsDwmSupported())
  106. return E_NOTIMPL;
  107. return ::DwmFlush();
  108. }
  109. };
  110. __declspec(selectany) int CDwm::m_nIsDwmSupported = -1;
  111. ///////////////////////////////////////////////////////////////////////////////
  112. // CDwmImpl - DWM window support
  113. template <class T, class TBase = CDwm>
  114. class CDwmImpl : public TBase
  115. {
  116. public:
  117. HRESULT DwmEnableBlurBehindWindow(const DWM_BLURBEHIND* pBB)
  118. {
  119. if(!this->IsDwmSupported())
  120. return E_NOTIMPL;
  121. T* pT = static_cast<T*>(this);
  122. ATLASSERT(::IsWindow(pT->m_hWnd));
  123. return ::DwmEnableBlurBehindWindow(pT->m_hWnd, pBB);
  124. }
  125. HRESULT DwmExtendFrameIntoClientArea(const MARGINS* pMargins)
  126. {
  127. if(!this->IsDwmSupported())
  128. return E_NOTIMPL;
  129. T* pT = static_cast<T*>(this);
  130. ATLASSERT(::IsWindow(pT->m_hWnd));
  131. return ::DwmExtendFrameIntoClientArea(pT->m_hWnd, pMargins);
  132. }
  133. HRESULT DwmExtendFrameIntoEntireClientArea()
  134. {
  135. MARGINS margins = { -1 };
  136. return DwmExtendFrameIntoClientArea(&margins);
  137. }
  138. HRESULT DwmGetCompositionTimingInfo(DWM_TIMING_INFO* pTimingInfo)
  139. {
  140. if(!this->IsDwmSupported())
  141. return E_NOTIMPL;
  142. T* pT = static_cast<T*>(this);
  143. ATLASSERT(::IsWindow(pT->m_hWnd));
  144. return ::DwmGetCompositionTimingInfo(pT->m_hWnd, pTimingInfo);
  145. }
  146. HRESULT DwmGetWindowAttribute(DWORD dwAttribute, PVOID pvAttribute, DWORD cbAttribute)
  147. {
  148. if(!this->IsDwmSupported())
  149. return E_NOTIMPL;
  150. T* pT = static_cast<T*>(this);
  151. ATLASSERT(::IsWindow(pT->m_hWnd));
  152. return ::DwmGetWindowAttribute(pT->m_hWnd, dwAttribute, pvAttribute, cbAttribute);
  153. }
  154. HRESULT DwmModifyPreviousDxFrameDuration(INT cRefreshes, BOOL fRelative)
  155. {
  156. if(!this->IsDwmSupported())
  157. return E_NOTIMPL;
  158. T* pT = static_cast<T*>(this);
  159. ATLASSERT(::IsWindow(pT->m_hWnd));
  160. return ::DwmModifyPreviousDxFrameDuration(pT->m_hWnd, cRefreshes, fRelative);
  161. }
  162. HRESULT DwmSetDxFrameDuration(INT cRefreshes)
  163. {
  164. if(!this->IsDwmSupported())
  165. return E_NOTIMPL;
  166. T* pT = static_cast<T*>(this);
  167. ATLASSERT(::IsWindow(pT->m_hWnd));
  168. return ::DwmSetDxFrameDuration(pT->m_hWnd, cRefreshes);
  169. }
  170. HRESULT DwmSetPresentParameters(DWM_PRESENT_PARAMETERS* pPresentParams)
  171. {
  172. if(!this->IsDwmSupported())
  173. return E_NOTIMPL;
  174. T* pT = static_cast<T*>(this);
  175. ATLASSERT(::IsWindow(pT->m_hWnd));
  176. return ::DwmSetPresentParameters(pT->m_hWnd, pPresentParams);
  177. }
  178. HRESULT DwmSetWindowAttribute(DWORD dwAttribute, LPCVOID pvAttribute, DWORD cbAttribute)
  179. {
  180. if(!this->IsDwmSupported())
  181. return E_NOTIMPL;
  182. T* pT = static_cast<T*>(this);
  183. ATLASSERT(::IsWindow(pT->m_hWnd));
  184. return ::DwmSetWindowAttribute(pT->m_hWnd, dwAttribute, pvAttribute, cbAttribute);
  185. }
  186. HRESULT DwmAttachMilContent()
  187. {
  188. if(!this->IsDwmSupported())
  189. return E_NOTIMPL;
  190. T* pT = static_cast<T*>(this);
  191. ATLASSERT(::IsWindow(pT->m_hWnd));
  192. return ::DwmAttachMilContent(pT->m_hWnd);
  193. }
  194. HRESULT DwmDetachMilContent()
  195. {
  196. if(!this->IsDwmSupported())
  197. return E_NOTIMPL;
  198. T* pT = static_cast<T*>(this);
  199. ATLASSERT(::IsWindow(pT->m_hWnd));
  200. return ::DwmDetachMilContent(pT->m_hWnd);
  201. }
  202. };
  203. template <class TBase>
  204. class CDwmWindowT : public TBase, public CDwmImpl<CDwmWindowT< TBase > >
  205. {
  206. public:
  207. CDwmWindowT(HWND hWnd = NULL) : TBase(hWnd)
  208. { }
  209. CDwmWindowT< TBase >& operator =(HWND hWnd)
  210. {
  211. this->m_hWnd = hWnd;
  212. return *this;
  213. }
  214. };
  215. typedef CDwmWindowT<ATL::CWindow> CDwmWindow;
  216. ///////////////////////////////////////////////////////////////////////////////
  217. // CDwmThumbnail - provides DWM thumbnail support
  218. template <bool t_bManaged, class TBase = CDwm>
  219. class CDwmThumbnailT : public TBase
  220. {
  221. public:
  222. // Data members
  223. HTHUMBNAIL m_hThumbnail;
  224. // Constructor
  225. CDwmThumbnailT(HTHUMBNAIL hThumbnail = NULL) : m_hThumbnail(hThumbnail)
  226. { }
  227. ~CDwmThumbnailT()
  228. {
  229. if(t_bManaged && (m_hThumbnail != NULL))
  230. Unregister();
  231. }
  232. // Operations
  233. CDwmThumbnailT<t_bManaged, TBase>& operator =(HTHUMBNAIL hThumbnail)
  234. {
  235. Attach(hThumbnail);
  236. return *this;
  237. }
  238. void Attach(HTHUMBNAIL hThumbnailNew)
  239. {
  240. if(t_bManaged && (m_hThumbnail != NULL) && (m_hThumbnail != hThumbnailNew))
  241. Unregister();
  242. m_hThumbnail = hThumbnailNew;
  243. }
  244. HTHUMBNAIL Detach()
  245. {
  246. HTHUMBNAIL hThumbnail = m_hThumbnail;
  247. m_hThumbnail = NULL;
  248. return hThumbnail;
  249. }
  250. HRESULT Register(HWND hwndDestination, HWND hwndSource)
  251. {
  252. ATLASSERT(::IsWindow(hwndDestination));
  253. ATLASSERT(::IsWindow(hwndSource));
  254. ATLASSERT(m_hThumbnail==NULL);
  255. if(!this->IsDwmSupported())
  256. return E_NOTIMPL;
  257. return ::DwmRegisterThumbnail(hwndDestination, hwndSource, &m_hThumbnail);
  258. }
  259. HRESULT Unregister()
  260. {
  261. if(!this->IsDwmSupported())
  262. return E_NOTIMPL;
  263. if(m_hThumbnail == NULL)
  264. return S_FALSE;
  265. HRESULT Hr = ::DwmUnregisterThumbnail(m_hThumbnail);
  266. if(SUCCEEDED(Hr))
  267. m_hThumbnail = NULL;
  268. return Hr;
  269. }
  270. operator HTHUMBNAIL() const { return m_hThumbnail; }
  271. bool IsNull() const { return (m_hThumbnail == NULL); }
  272. HRESULT UpdateProperties(const DWM_THUMBNAIL_PROPERTIES* ptnProperties)
  273. {
  274. if(!this->IsDwmSupported())
  275. return E_NOTIMPL;
  276. ATLASSERT(m_hThumbnail != NULL);
  277. return ::DwmUpdateThumbnailProperties(m_hThumbnail, ptnProperties);
  278. }
  279. // Attributes
  280. HRESULT QuerySourceSize(PSIZE pSize)
  281. {
  282. if(!this->IsDwmSupported())
  283. return E_NOTIMPL;
  284. ATLASSERT(m_hThumbnail != NULL);
  285. return ::DwmQueryThumbnailSourceSize(m_hThumbnail, pSize);
  286. }
  287. };
  288. typedef CDwmThumbnailT<true, CDwm> CDwmThumbnail;
  289. typedef CDwmThumbnailT<false, CDwm> CDwmThumbnailHandle;
  290. #ifdef __ATLTHEME_H__
  291. ///////////////////////////////////////////////////////////////////////////////
  292. // CAeroControlImpl - Base class for controls on Glass
  293. template <class T, class TBase = ATL::CWindow, class TWinTraits = ATL::CControlWinTraits>
  294. class CAeroControlImpl : public CThemeImpl<T>,
  295. public CBufferedPaintImpl<T>,
  296. public ATL::CWindowImpl<T, TBase, TWinTraits>
  297. {
  298. public:
  299. typedef CThemeImpl<T> _themeClass;
  300. typedef CBufferedPaintImpl<T> _baseClass;
  301. typedef ATL::CWindowImpl<T, TBase, TWinTraits> _windowClass;
  302. CAeroControlImpl()
  303. {
  304. this->m_PaintParams.dwFlags = BPPF_ERASE;
  305. }
  306. static LPCWSTR GetThemeName()
  307. {
  308. #ifdef _UNICODE
  309. return TBase::GetWndClassName();
  310. #else
  311. ATLASSERT(!_T("Return UNICODE string of window classname / theme class"));
  312. return NULL;
  313. #endif // _UNICODE
  314. }
  315. // Message map and handlers
  316. BEGIN_MSG_MAP(CAeroControlImpl)
  317. MESSAGE_HANDLER(WM_CREATE, OnCreate)
  318. MESSAGE_HANDLER(WM_ACTIVATE, OnActivate)
  319. CHAIN_MSG_MAP(_themeClass)
  320. CHAIN_MSG_MAP(_baseClass)
  321. END_MSG_MAP()
  322. LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
  323. {
  324. T* pT = static_cast<T*>(this);
  325. pT->Init();
  326. bHandled = FALSE;
  327. return 0;
  328. }
  329. LRESULT OnActivate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
  330. {
  331. if(this->IsThemingSupported())
  332. this->Invalidate(FALSE);
  333. bHandled = FALSE;
  334. return 0;
  335. }
  336. // Operations
  337. BOOL SubclassWindow(HWND hWnd)
  338. {
  339. ATLASSERT(this->m_hWnd == NULL);
  340. ATLASSERT(::IsWindow(hWnd));
  341. BOOL bRet = _windowClass::SubclassWindow(hWnd);
  342. if(bRet)
  343. {
  344. T* pT = static_cast<T*>(this);
  345. pT->Init();
  346. }
  347. return bRet;
  348. }
  349. // Implementation
  350. LRESULT DefWindowProc()
  351. {
  352. const ATL::_ATL_MSG* pMsg = this->m_pCurrentMsg;
  353. LRESULT lRes = 0;
  354. if(pMsg != NULL)
  355. lRes = DefWindowProc(pMsg->message, pMsg->wParam, pMsg->lParam);
  356. return lRes;
  357. }
  358. LRESULT DefWindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  359. {
  360. T* pT = static_cast<T*>(this);
  361. LRESULT lRes = 0;
  362. if(::DwmDefWindowProc(pT->m_hWnd, uMsg, wParam, lParam, &lRes) != FALSE)
  363. return lRes;
  364. return _windowClass::DefWindowProc(uMsg, wParam, lParam);
  365. }
  366. void DoBufferedPaint(HDC hDC, RECT& rcPaint)
  367. {
  368. T* pT = static_cast<T*>(this);
  369. HDC hDCPaint = NULL;
  370. RECT rcClient = {};
  371. this->GetClientRect(&rcClient);
  372. this->m_BufferedPaint.Begin(hDC, &rcClient, this->m_dwFormat, &this->m_PaintParams, &hDCPaint);
  373. ATLASSERT(hDCPaint != NULL);
  374. pT->DoAeroPaint(hDCPaint, rcClient, rcPaint);
  375. this->m_BufferedPaint.End();
  376. }
  377. void DoPaint(HDC /*hdc*/, RECT& /*rcClient*/)
  378. {
  379. DefWindowProc();
  380. }
  381. // Overridables
  382. void Init()
  383. {
  384. T* pT = static_cast<T*>(this);
  385. (void)pT; // avoid level 4 warning
  386. this->SetThemeClassList(pT->GetThemeName());
  387. if(this->m_lpstrThemeClassList != NULL)
  388. this->OpenThemeData();
  389. }
  390. void DoAeroPaint(HDC hDC, RECT& /*rcClient*/, RECT& rcPaint)
  391. {
  392. DefWindowProc(WM_PAINT, (WPARAM) hDC, 0L);
  393. this->m_BufferedPaint.MakeOpaque(&rcPaint);
  394. }
  395. };
  396. #endif // __ATLTHEME_H__
  397. } // namespace WTL
  398. #endif // __ATLDWM_H__