ThemeUtil.cpp
上传用户:huashan618
上传日期:2013-03-26
资源大小:261k
文件大小:6k
源码类别:

浏览器

开发平台:

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