ThemeUtil.cpp
上传用户:tang1250
上传日期:2020-10-23
资源大小:86k
文件大小:6k
源码类别:

PropertySheet

开发平台:

Visual C++

  1. /**********************************************************************
  2. **
  3. ** ThemeUtil.cpp : implementation file of CThemeUtil class
  4. **
  5. ** by Andrzej Markowski June 2004
  6. **
  7. **********************************************************************/
  8. #include "stdafx.h"
  9. #include "themeutil.h"
  10. CThemeUtil::CThemeUtil()
  11. {
  12. m_hUxThemeDll = NULL;
  13. m_hRcDll = NULL;
  14. m_hTheme = NULL;
  15. FreeLibrary();
  16. if(IsWinXP())
  17. m_hUxThemeDll = AfxLoadLibrary(CString(_T("UxTheme.dll")));
  18. }
  19. CThemeUtil::~CThemeUtil()
  20. {
  21. FreeLibrary();
  22. }
  23. void CThemeUtil::FreeLibrary()
  24. {
  25. CloseThemeData();
  26. if(m_hUxThemeDll!=NULL)
  27. AfxFreeLibrary(m_hUxThemeDll);
  28. m_hUxThemeDll = NULL;
  29. }
  30. BOOL CThemeUtil::IsWinXP(void)
  31. {
  32. OSVERSIONINFO osvi;
  33. ::ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
  34. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  35. ::GetVersionEx(&osvi);
  36. return MAKELONG(osvi.dwMajorVersion,osvi.dwMinorVersion) >= MAKELONG(5,1);
  37. }
  38. BOOL CThemeUtil::OpenThemeData(HWND hWnd, LPCWSTR pszClassList)
  39. {
  40. if(m_hUxThemeDll==NULL || hWnd==NULL || m_hTheme) 
  41. return FALSE;
  42. UINT (PASCAL* pfnIsThemeActive)(); // IsThemeActive
  43. (FARPROC&)pfnIsThemeActive=GetProcAddress(m_hUxThemeDll,"IsThemeActive");
  44. if(pfnIsThemeActive && pfnIsThemeActive())
  45. {
  46. UINT (PASCAL* pfnOpenThemeData)(HWND hwnd, LPCWSTR pszClassList);
  47. (FARPROC&)pfnOpenThemeData=GetProcAddress(m_hUxThemeDll,"OpenThemeData");
  48. if(pfnOpenThemeData)
  49. m_hTheme=pfnOpenThemeData(hWnd, pszClassList);
  50. if(m_hTheme)
  51. {
  52. WCHAR szThemeFileName[MAX_PATH];
  53. WCHAR szColorBuff[MAX_PATH];
  54. WCHAR szSizeBuff[MAX_PATH];
  55. if(GetCurrentThemeName(szThemeFileName,MAX_PATH,szColorBuff,MAX_PATH,szSizeBuff,MAX_PATH))
  56. {
  57. if((m_hRcDll = AfxLoadLibrary(CString(szThemeFileName))))
  58. return TRUE;
  59. }
  60. CloseThemeData();
  61. return FALSE;
  62. }
  63. }
  64. return FALSE;
  65. }
  66. void CThemeUtil::CloseThemeData()
  67. {
  68. if(m_hTheme)
  69. {
  70. UINT (PASCAL* pfnCloseThemeData)(HANDLE hTheme);
  71. (FARPROC&)pfnCloseThemeData=GetProcAddress(m_hUxThemeDll,"CloseThemeData");
  72. if(pfnCloseThemeData)
  73. pfnCloseThemeData((HANDLE)m_hTheme);
  74. }
  75. m_hTheme = NULL;
  76. if(m_hRcDll!=NULL)
  77. AfxFreeLibrary(m_hRcDll);
  78. m_hRcDll = NULL;
  79. }
  80. BOOL CThemeUtil::DrawThemePart(HDC hdc, int iPartId, int iStateId, const RECT *pRect)
  81. {
  82. if(m_hTheme==NULL)
  83. return FALSE;
  84. UINT (PASCAL* pfnDrawThemeBackground)(UINT hTheme, HDC hdc, int iPartId, int iStateId,
  85. const RECT *pRect, const RECT* pClipRect);
  86. (FARPROC&)pfnDrawThemeBackground=GetProcAddress(m_hUxThemeDll,"DrawThemeBackground");
  87. if(pfnDrawThemeBackground)
  88. {
  89. pfnDrawThemeBackground(m_hTheme, hdc, iPartId, iStateId, pRect, NULL);
  90. return TRUE;
  91. }
  92. return FALSE;
  93. }
  94. BOOL CThemeUtil::GetThemeColor(int iPartId, int iStateId, int iPropId, const COLORREF *pColor)
  95. {
  96. if(m_hTheme==NULL)
  97. return FALSE;
  98. UINT (PASCAL* pfnGetThemeColor)(UINT hTheme, int iPartId, int iStateId, int iPropId, 
  99. const COLORREF *pColor);
  100. (FARPROC&)pfnGetThemeColor=GetProcAddress(m_hUxThemeDll,"GetThemeColor");
  101. if(pfnGetThemeColor)
  102. {
  103. pfnGetThemeColor(m_hTheme, iPartId, iStateId, iPropId, pColor);
  104. return TRUE;
  105. }
  106. return FALSE;
  107. }
  108. BOOL CThemeUtil::GetThemeEnumValue(int iPartId, int iStateId, int iPropId, const int *piVal)
  109. {
  110. if(m_hTheme==NULL)
  111. return FALSE;
  112. UINT (PASCAL* pfnGetThemeEnumValue)(UINT hTheme, int iPartId, int iStateId, int iPropId, 
  113. const int *piVal);
  114. (FARPROC&)pfnGetThemeEnumValue=GetProcAddress(m_hUxThemeDll,"GetThemeEnumValue");
  115. if(pfnGetThemeEnumValue)
  116. {
  117. pfnGetThemeEnumValue(m_hTheme, iPartId, iStateId, iPropId, piVal);
  118. return TRUE;
  119. }
  120. return FALSE;
  121. }
  122. BOOL CThemeUtil::GetThemeInt(int iPartId, int iStateId, int iPropId, const int *piVal)
  123. {
  124. if(m_hTheme==NULL)
  125. return FALSE;
  126. UINT (PASCAL* pfnGetThemeInt)(UINT hTheme, int iPartId, int iStateId, int iPropId, 
  127. const int *piVal);
  128. (FARPROC&)pfnGetThemeInt=GetProcAddress(m_hUxThemeDll,"GetThemeInt");
  129. if(pfnGetThemeInt)
  130. {
  131. pfnGetThemeInt(m_hTheme, iPartId, iStateId, iPropId, piVal);
  132. return TRUE;
  133. }
  134. return FALSE;
  135. }
  136. BOOL CThemeUtil::GetThemeMargins(int iPartId, int iStateId, int iPropId, const MY_MARGINS *pMargins)
  137. {
  138. if(m_hTheme==NULL)
  139. return FALSE;
  140. UINT (PASCAL* pfnGetThemeMargins)(UINT hTheme, OPTIONAL HDC hdc, int iPartId, 
  141. int iStateId, int iPropId, OPTIONAL RECT *prc, const MY_MARGINS *pMargins);
  142. (FARPROC&)pfnGetThemeMargins=GetProcAddress(m_hUxThemeDll,"GetThemeMargins");
  143. if(pfnGetThemeMargins)
  144. {
  145. pfnGetThemeMargins(m_hTheme, NULL, iPartId, iStateId, iPropId, NULL, pMargins);
  146. return TRUE;
  147. }
  148. return FALSE;
  149. }
  150. BOOL CThemeUtil::GetThemeFilename(int iPartId, int iStateId, int iPropId, 
  151.   OUT LPWSTR pszThemeFileName, int cchMaxBuffChars)
  152. {
  153. if(m_hTheme==NULL)
  154. return FALSE;
  155. UINT (PASCAL* pfnGetThemeFilename)(UINT hTheme, int iPartId, int iStateId, int iPropId, 
  156. OUT LPWSTR pszThemeFileName, int cchMaxBuffChars);
  157. (FARPROC&)pfnGetThemeFilename=GetProcAddress(m_hUxThemeDll,"GetThemeFilename");
  158. if(pfnGetThemeFilename)
  159. {
  160. pfnGetThemeFilename(m_hTheme, iPartId, iStateId, iPropId, pszThemeFileName, cchMaxBuffChars);
  161. return TRUE;
  162. }
  163. return FALSE;
  164. }
  165. BOOL CThemeUtil::GetCurrentThemeName(OUT LPWSTR pszThemeFileName, int cchMaxNameChars, 
  166. OUT OPTIONAL LPWSTR pszColorBuff, int cchMaxColorChars,
  167. OUT OPTIONAL LPWSTR pszSizeBuff, int cchMaxSizeChars)
  168. {
  169. if(m_hUxThemeDll==NULL)
  170. return FALSE;
  171. UINT (PASCAL* pfnGetCurrentThemeName)(OUT LPWSTR pszThemeFileName, 
  172. int cchMaxNameChars, 
  173. OUT OPTIONAL LPWSTR pszColorBuff, 
  174. int cchMaxColorChars,
  175. OUT OPTIONAL LPWSTR pszSizeBuff, 
  176. int cchMaxSizeChars);
  177. (FARPROC&)pfnGetCurrentThemeName=GetProcAddress(m_hUxThemeDll,"GetCurrentThemeName");
  178. if(pfnGetCurrentThemeName)
  179. {
  180. pfnGetCurrentThemeName(pszThemeFileName,cchMaxNameChars,pszColorBuff,cchMaxColorChars,pszSizeBuff,cchMaxSizeChars);
  181. return TRUE;
  182. }
  183. return FALSE;
  184. }
  185. HBITMAP CThemeUtil::LoadBitmap(LPWSTR pszBitmapName)
  186. {
  187. if(m_hRcDll==NULL)
  188. return NULL;
  189. try
  190. {
  191. CString sBitmapName(pszBitmapName);
  192. sBitmapName.Replace('\','_');
  193. sBitmapName.Replace('.','_');
  194. return ::LoadBitmap(m_hRcDll,sBitmapName);
  195. }
  196. catch(CMemoryException* e)
  197. {
  198. e->Delete();
  199. }
  200. return NULL;
  201. }