windowsTheme.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include "./windowsTheme.h"
  2. #include <shlwapi.h>
  3. typedef UXTHEME (WINAPI *UXOPENTHEMEDATA)(HWND hwnd, LPCWSTR /*pszClassList*/);
  4. typedef HRESULT (WINAPI *UXCLOSETHEMEDATA)(UXTHEME /*hTheme*/);
  5. typedef HRESULT (WINAPI *UXSETWINDOWTHEME)(HWND /*hwnd*/, LPCWSTR /*pszSubAppName*/, LPCWSTR /*pszSubIdList*/);
  6. typedef HRESULT (WINAPI *UXDRAWTHEMEBACKGROUND)(UXTHEME /*hTheme*/, HDC /*hdc*/, INT /*iPartId*/, INT /*iStateId*/,
  7. LPCRECT /*pRect*/, LPCRECT /*pClipRect*/);
  8. typedef HRESULT (WINAPI *UXDRAWTHEMEPARENTBACKGROUND)(HWND /*hwnd*/, HDC /*hdc*/, const RECT * /*prc*/);
  9. typedef HRESULT (WINAPI *UXDRAWTHEMEPARENTBACKGROUNDEX)(HWND /*hwnd*/, HDC /*hdc*/, DWORD /*dwFlags*/, const RECT * /*prc*/);
  10. typedef HRESULT (WINAPI *UXDRAWTHEMETEXT)(UXTHEME /*hTheme*/, HDC /*hdc*/, INT /*iPartId*/, INT /*iStateId*/,
  11. LPCWSTR /*pszText*/, INT /*iCharCount*/, DWORD /*dwTextFlags*/,
  12. DWORD /*dwTextFlags2*/, LPCRECT /*pRect*/);
  13. typedef COLORREF (WINAPI *UXGETTHEMESYSCOLOR)(UXTHEME /*hTheme*/, INT /*iColorID*/);
  14. typedef HRESULT (WINAPI *UXGETTHEMECOLOR)(UXTHEME /*hTheme*/, int /*iPartId*/, int /*iStateId*/, int /*iPropId*/, LPCOLORREF /*pColor*/);
  15. typedef HRESULT (WINAPI *UXGETTHEMEINT)(UXTHEME /*hTheme*/, int /*iPartId*/, int /*iStateId*/, int /*iPropId*/, LPINT /*piVal*/);
  16. typedef HRESULT (WINAPI *UXGETTHEMEMETRIC)(UXTHEME /*hTheme*/, HDC /*hdc*/, int /*iPartId*/, int /*iStateId*/, int /*iPropId*/, LPINT /*piVal*/);
  17. typedef HRESULT (WINAPI *UXGETTHEMEPARTSIZE)(UXTHEME /*hTheme*/, HDC /*hdc*/, int /*iPartId*/, int /*iStateId*/, LPCRECT /*prc*/, UXTHEMESIZE /*eSize*/, LPSIZE /*psz*/);
  18. typedef HRESULT (WINAPI *UXGETTHEMEBACKGROUNDCONTENTRECT)(UXTHEME /*hTheme*/, HDC /*hdc*/, int /*iPartId*/, int /*iStateId*/, LPCRECT /*pBoundingRect*/, LPRECT /*pContentRect*/);
  19. typedef HRESULT (WINAPI *UXENABLETHEMEDIALOGTEXTURE)(HWND /*hwnd*/, DWORD /*dwFlags*/);
  20. typedef BOOL (WINAPI *UXISAPPTHEMED)(void);
  21. typedef BOOL (WINAPI *UXISTHEMEACTIVE)(void);
  22. typedef BOOL (WINAPI *UXISTHEMEBACKGROUNDPARTIALLYTRANSPARENT)(UXTHEME /*hTheme*/, int /*iPartId*/, int /*iStateId*/);
  23. static UXOPENTHEMEDATA uxOpenThemeData = NULL;
  24. static UXCLOSETHEMEDATA uxCloseThemeData = NULL;
  25. static UXSETWINDOWTHEME uxSetWindowTheme = NULL;
  26. static UXISAPPTHEMED uxIsAppThemed = NULL;
  27. static UXISTHEMEACTIVE uxIsThemeActive = NULL;
  28. static UXDRAWTHEMEBACKGROUND uxDrawThemeBackground = NULL;
  29. static UXDRAWTHEMEPARENTBACKGROUND uxDrawThemeParentBackground = NULL;
  30. static UXDRAWTHEMEPARENTBACKGROUNDEX uxDrawThemeParentBackgroundEx = NULL;
  31. static UXDRAWTHEMETEXT uxDrawThemeText = NULL;
  32. static UXGETTHEMESYSCOLOR uxGetThemeSysColor = NULL;
  33. static UXGETTHEMECOLOR uxGetThemeColor = NULL;
  34. static UXGETTHEMEINT uxGetThemeInt = NULL;
  35. static UXGETTHEMEMETRIC uxGetThemeMetric = NULL;
  36. static UXGETTHEMEPARTSIZE uxGetThemePartSize = NULL;
  37. static UXGETTHEMEBACKGROUNDCONTENTRECT uxGetThemeBackgroundContentRect = NULL;
  38. static UXISTHEMEBACKGROUNDPARTIALLYTRANSPARENT uxIsThemeBackgroundPartiallyTransparent = NULL;
  39. static UXENABLETHEMEDIALOGTEXTURE uxEnableThemeDialogTexture = NULL;
  40. static HMODULE uxModule = NULL;
  41. static HRESULT uxLoadResult = E_WINTHEME_NOTLOADED;
  42. static BOOL fSysCheckOk = -1;
  43. static BOOL fComCtlCheckOk = -1;
  44. #define UXTHEME_LOADCHECK(__uxFunction){\
  45. if (NULL == uxModule && FAILED(UxTheme_LoadLibrary())) return uxLoadResult;\
  46. if (NULL == (__uxFunction)) return E_WINTHEME_BADFUNCTION;}
  47. #define UXTHEME_LOADCHECK_RESULT(__uxFunction, __failResult){\
  48. if ((NULL == uxModule && FAILED(UxTheme_LoadLibrary())) || NULL == (__uxFunction))\
  49. return (__failResult);}
  50. HRESULT STDAPICALLTYPE UxTheme_LoadLibrary(void)
  51. {
  52. if (E_WINTHEME_NOTLOADED == uxLoadResult)
  53. {
  54. uxModule = LoadLibraryW(L"uxtheme.dll");
  55. if (NULL == uxModule)
  56. {
  57. uxLoadResult = E_WINTHEME_LOADFAILED;
  58. }
  59. else
  60. {
  61. uxSetWindowTheme = (UXSETWINDOWTHEME)GetProcAddress(uxModule, "SetWindowTheme");
  62. uxIsAppThemed = (UXISAPPTHEMED)GetProcAddress(uxModule, "IsAppThemed");
  63. uxIsThemeActive = (UXISTHEMEACTIVE)GetProcAddress(uxModule, "IsThemeActive");
  64. uxOpenThemeData = (UXOPENTHEMEDATA)GetProcAddress(uxModule, "OpenThemeData");
  65. uxCloseThemeData = (UXCLOSETHEMEDATA)GetProcAddress(uxModule, "CloseThemeData");
  66. uxDrawThemeBackground = (UXDRAWTHEMEBACKGROUND)GetProcAddress(uxModule, "DrawThemeBackground");
  67. uxDrawThemeParentBackground = (UXDRAWTHEMEPARENTBACKGROUND)GetProcAddress(uxModule, "DrawThemeParentBackground");
  68. uxDrawThemeParentBackgroundEx = (UXDRAWTHEMEPARENTBACKGROUNDEX)GetProcAddress(uxModule, "DrawThemeParentBackgroundEx");
  69. uxDrawThemeText = (UXDRAWTHEMETEXT)GetProcAddress(uxModule, "DrawThemeText");
  70. uxGetThemeSysColor = (UXGETTHEMESYSCOLOR)GetProcAddress(uxModule, "GetThemeSysColor");
  71. uxGetThemeColor = (UXGETTHEMECOLOR)GetProcAddress(uxModule, "GetThemeColor");
  72. uxGetThemeInt = (UXGETTHEMEINT)GetProcAddress(uxModule, "GetThemeInt");
  73. uxGetThemeMetric = (UXGETTHEMEMETRIC)GetProcAddress(uxModule, "GetThemeMetric");
  74. uxGetThemePartSize = (UXGETTHEMEPARTSIZE)GetProcAddress(uxModule, "GetThemePartSize");
  75. uxGetThemeBackgroundContentRect = (UXGETTHEMEBACKGROUNDCONTENTRECT)GetProcAddress(uxModule, "GetThemeBackgroundContentRect");
  76. uxIsThemeBackgroundPartiallyTransparent = (UXISTHEMEBACKGROUNDPARTIALLYTRANSPARENT)GetProcAddress(uxModule, "IsThemeBackgroundPartiallyTransparent");
  77. uxEnableThemeDialogTexture = (UXENABLETHEMEDIALOGTEXTURE)GetProcAddress(uxModule, "EnableThemeDialogTexture");
  78. uxLoadResult = S_OK;
  79. }
  80. }
  81. if (-1 == fSysCheckOk)
  82. {
  83. OSVERSIONINFOW vi;
  84. vi.dwOSVersionInfoSize = sizeof(vi);
  85. if (FALSE != GetVersionEx(&vi) &&
  86. (vi.dwMajorVersion > 5 || (vi.dwMajorVersion == 5 && vi.dwMinorVersion > 0)))
  87. {
  88. fSysCheckOk = TRUE;
  89. }
  90. else
  91. {
  92. fSysCheckOk = FALSE;
  93. }
  94. }
  95. if (-1 == fComCtlCheckOk)
  96. {
  97. fComCtlCheckOk = FALSE;
  98. HMODULE hComCtl = LoadLibraryW(L"comctl32.dll");
  99. if (NULL != hComCtl)
  100. {
  101. HRESULT (CALLBACK *waDllGetVersion)(DLLVERSIONINFO*) = (HRESULT (CALLBACK *)(DLLVERSIONINFO*))GetProcAddress(hComCtl, "DllGetVersion");
  102. if (NULL != waDllGetVersion)
  103. {
  104. DLLVERSIONINFO dllVer;
  105. dllVer.cbSize = sizeof(DLLVERSIONINFO);
  106. if (S_OK == waDllGetVersion(&dllVer) && dllVer.dwMajorVersion >= 6)
  107. {
  108. fComCtlCheckOk = TRUE;
  109. }
  110. FreeLibrary(hComCtl);
  111. }
  112. }
  113. }
  114. return uxLoadResult;
  115. }
  116. HRESULT STDAPICALLTYPE UxTheme_IsThemeActive(void)
  117. {
  118. if (NULL == uxIsAppThemed)
  119. {
  120. if(FAILED(UxTheme_LoadLibrary()))
  121. return uxLoadResult;
  122. if (NULL == uxIsAppThemed) return E_WINTHEME_BADFUNCTION;
  123. }
  124. if (NULL == uxIsThemeActive)
  125. {
  126. if(FAILED(UxTheme_LoadLibrary()))
  127. return uxLoadResult;
  128. if (NULL == uxIsThemeActive) return E_WINTHEME_BADFUNCTION;
  129. }
  130. if (TRUE != fSysCheckOk || TRUE != fComCtlCheckOk ||
  131. FALSE == uxIsAppThemed() || FALSE == uxIsThemeActive())
  132. {
  133. return S_FALSE;
  134. }
  135. return S_OK;
  136. }
  137. HRESULT STDAPICALLTYPE UxTheme_GetLoadResult(void)
  138. {
  139. return uxLoadResult;
  140. }
  141. UXTHEME STDAPICALLTYPE UxOpenThemeData(HWND hwnd, LPCWSTR pszClassList)
  142. {
  143. UXTHEME_LOADCHECK_RESULT(uxOpenThemeData, NULL);
  144. return uxOpenThemeData(hwnd, pszClassList);
  145. }
  146. HRESULT STDAPICALLTYPE UxCloseThemeData(UXTHEME hTheme)
  147. {
  148. UXTHEME_LOADCHECK(uxCloseThemeData);
  149. return uxCloseThemeData(hTheme);
  150. }
  151. HRESULT STDAPICALLTYPE UxSetWindowTheme(HWND hwnd, LPCWSTR pszSubAppName, LPCWSTR pszSubIdList)
  152. {
  153. UXTHEME_LOADCHECK(uxSetWindowTheme);
  154. return uxSetWindowTheme(hwnd, pszSubAppName, pszSubIdList);
  155. }
  156. BOOL STDAPICALLTYPE UxIsAppThemed(void)
  157. {
  158. UXTHEME_LOADCHECK_RESULT(uxIsAppThemed, FALSE);
  159. return uxIsAppThemed();
  160. }
  161. BOOL STDAPICALLTYPE UxIsThemeActive(void)
  162. {
  163. UXTHEME_LOADCHECK_RESULT(UxIsThemeActive, FALSE);
  164. return UxIsThemeActive();
  165. }
  166. HRESULT STDAPICALLTYPE UxDrawThemeBackground(UXTHEME hTheme, HDC hdc, int iPartId, int iStateId,
  167. const RECT *pRect, OPTIONAL const RECT *pClipRect)
  168. {
  169. UXTHEME_LOADCHECK(uxDrawThemeBackground);
  170. return uxDrawThemeBackground(hTheme, hdc, iPartId, iStateId, pRect, pClipRect);
  171. }
  172. HRESULT STDAPICALLTYPE UxDrawThemeParentBackground(HWND hwnd, HDC hdc, const RECT *prc)
  173. {
  174. UXTHEME_LOADCHECK(uxDrawThemeParentBackground);
  175. return uxDrawThemeParentBackground(hwnd, hdc, prc);
  176. }
  177. HRESULT STDAPICALLTYPE UxDrawThemeParentBackgroundEx(HWND hwnd, HDC hdc, DWORD dwFlags, const RECT *prc)
  178. {
  179. UXTHEME_LOADCHECK(uxDrawThemeParentBackgroundEx);
  180. return uxDrawThemeParentBackgroundEx(hwnd, hdc, dwFlags, prc);
  181. }
  182. HRESULT STDAPICALLTYPE UxDrawThemeText(UXTHEME hTheme, HDC hdc, int iPartId, int iStateId, LPCWSTR pszText,
  183. int iCharCount, DWORD dwTextFlags, DWORD dwTextFlags2, const RECT *pRect)
  184. {
  185. UXTHEME_LOADCHECK(uxDrawThemeText);
  186. return uxDrawThemeText(hTheme, hdc, iPartId, iStateId, pszText, iCharCount, dwTextFlags, dwTextFlags2, pRect);
  187. }
  188. COLORREF STDAPICALLTYPE UxGetThemeSysColor(UXTHEME hTheme, int iColorID)
  189. {
  190. UXTHEME_LOADCHECK_RESULT(uxGetThemeSysColor, RGB(255, 0, 255));
  191. return uxGetThemeSysColor(hTheme, iColorID);
  192. }
  193. HRESULT STDAPICALLTYPE UxGetThemeColor(UXTHEME hTheme, int iPartId, int iStateId, int iPropId, COLORREF *pColor)
  194. {
  195. UXTHEME_LOADCHECK(uxGetThemeColor);
  196. return uxGetThemeColor(hTheme, iPartId, iStateId, iPropId, pColor);
  197. }
  198. HRESULT STDAPICALLTYPE UxGetThemeInt(UXTHEME hTheme, int iPartId, int iStateId, int iPropId, int *piVal)
  199. {
  200. UXTHEME_LOADCHECK(uxGetThemeInt);
  201. return uxGetThemeInt(hTheme, iPartId, iStateId, iPropId, piVal);
  202. }
  203. HRESULT STDAPICALLTYPE UxGetThemeMetric(UXTHEME hTheme, HDC hdc, int iPartId, int iStateId, int iPropId, int *piVal)
  204. {
  205. UXTHEME_LOADCHECK(uxGetThemeMetric);
  206. return uxGetThemeMetric(hTheme, hdc, iPartId, iStateId, iPropId, piVal);
  207. }
  208. HRESULT STDAPICALLTYPE UxGetThemePartSize(UXTHEME hTheme, HDC hdc, int iPartId, int iStateId, LPCRECT prc, UXTHEMESIZE eSize, SIZE *psz)
  209. {
  210. UXTHEME_LOADCHECK(uxGetThemePartSize);
  211. return uxGetThemePartSize(hTheme, hdc, iPartId, iStateId, prc, eSize, psz);
  212. }
  213. HRESULT STDAPICALLTYPE UxGetThemeBackgroundContentRect(UXTHEME hTheme, HDC hdc, int iPartId, int iStateId, LPCRECT pBoundingRect, LPRECT pContentRect)
  214. {
  215. UXTHEME_LOADCHECK(uxGetThemeBackgroundContentRect);
  216. return uxGetThemeBackgroundContentRect(hTheme, hdc, iPartId, iStateId, pBoundingRect, pContentRect);
  217. }
  218. BOOL STDAPICALLTYPE UxIsThemeBackgroundPartiallyTransparent(UXTHEME hTheme, int iPartId, int iStateId)
  219. {
  220. UXTHEME_LOADCHECK_RESULT(uxIsThemeBackgroundPartiallyTransparent, FALSE);
  221. return uxIsThemeBackgroundPartiallyTransparent(hTheme, iPartId, iStateId);
  222. }
  223. HRESULT STDAPICALLTYPE UxEnableThemeDialogTexture(HWND hwnd, DWORD dwFlags)
  224. {
  225. UXTHEME_LOADCHECK(uxEnableThemeDialogTexture);
  226. return uxEnableThemeDialogTexture(hwnd, dwFlags);
  227. }