SPDrawHelpers.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /********************************************
  2. ** 工作室:S&P工作室
  3. ** 作者 :张东斌
  4. ** 日期 :2006年3月
  5. *********************************************/
  6. #if !defined(__SPDRAWHELPERS_H__)
  7. #define __SPDRAWHELPERS_H__
  8. #if _MSC_VER > 1000
  9. #pragma once
  10. #endif
  11. #include "SPColorManager.h"
  12. //功能:辅助类,用来获取位图中的透明色。
  13. class CSPTransparentBitmap
  14. {
  15. public:
  16. CSPTransparentBitmap( HBITMAP hBitmap );
  17. //功能:获取位图的透明色。
  18. COLORREF GetTransparentColor() const;
  19. //功能: 基于位图创建图标。
  20. HICON ConvertToIcon() const;
  21. protected:
  22. HBITMAP m_hBitmap;
  23. };
  24. //功能:辅助类,用来获取光标在客户区中的位置。
  25. class CSPClientCursorPos : public CPoint
  26. {
  27. public:
  28. CSPClientCursorPos( CWnd * pWnd );
  29. };
  30. //功能:辅助类,用来创建空的CRect对象。
  31. class CSPEmptyRect : public CRect
  32. {
  33. public:
  34. CSPEmptyRect();
  35. };
  36. //功能:辅助类,用来获取制定窗口的大小。
  37. class CSPWindowRect : public CRect
  38. {
  39. public:
  40. CSPWindowRect( HWND hWnd );
  41. CSPWindowRect( const CWnd * pWnd );
  42. };
  43. //功能:辅助类,用来获取制定窗口的客户区位置。
  44. class CSPClientRect : public CRect
  45. {
  46. public:
  47. CSPClientRect( HWND hWnd );
  48. CSPClientRect( const CWnd * pWnd );
  49. };
  50. //功能:辅助类,用来创建缓存设备内容。
  51. class CSPBufferDC : public CDC
  52. {
  53. public:
  54. CSPBufferDC( HDC hDestDC , const CRect rcPaint );
  55. CSPBufferDC( CDC * pDC , const CRect rcPaint );
  56. virtual ~CSPBufferDC();
  57. CDC * GetDestDC();
  58. protected:
  59. HDC m_hDestDC; //指向目标的设备内容句柄
  60. CBitmap m_bitmap; //内存设备内容中的位图
  61. CRect m_rect; //绘制区域
  62. HGDIOBJ m_hOldBitmap; //先前被选进设备内容的位图
  63. };
  64. //功能:辅助类,用来选择字体到指定的设备内容。
  65. class CSPFontDC
  66. {
  67. public:
  68. CSPFontDC( CDC * pDC , CFont * pFont );
  69. virtual ~CSPFontDC();
  70. //功能:更改设备内容的字体。
  71. void SetFont( CFont * pFont );
  72. protected:
  73. HDC m_hDC;
  74. HGDIOBJ m_hOldFont;
  75. };
  76. //功能:辅助类,创建指定颜色的笔并选进设备内容。
  77. class CSPPenDC
  78. {
  79. public:
  80. CSPPenDC( HDC hDC , COLORREF crColor );
  81. CSPPenDC( CDC * pDC , COLORREF crColor );
  82. virtual ~CSPPenDC();
  83. void Color( COLORREF crColor );
  84. COLORREF Color();
  85. protected:
  86. CPen m_pen;
  87. HDC m_hDC;
  88. HPEN m_hOldPen;
  89. };
  90. //功能:辅助类,创建指定颜色的画刷并选进设备内容。
  91. class CSPBrushDC
  92. {
  93. public:
  94. CSPBrushDC( HDC hDC , COLORREF crColor );
  95. CSPBrushDC( CDC * pDC , COLORREF crColor );
  96. virtual ~CSPBrushDC();
  97. void Color( COLORREF crColor );
  98. protected:
  99. CBrush m_brush;
  100. HDC m_hDC;
  101. HBRUSH m_hOldBrush;
  102. };
  103. //功能:辅助类,创建同制定设备相兼容的内存设备。
  104. class CSPCompatibleDC : public CDC
  105. {
  106. public:
  107. CSPCompatibleDC( CDC * pDC , CBitmap * pBitmap );
  108. virtual ~CSPCompatibleDC();
  109. protected:
  110. CBitmap * m_pBmp;
  111. };
  112. //功能:辅助类,用来跟踪Splitter区域。
  113. class CSPSplitterTracker
  114. {
  115. public:
  116. CSPSplitterTracker( BOOL bSolid = FALSE );
  117. //功能:用来跟踪splitter的大小。
  118. BOOL Track( CWnd * pTrackWnd , CRect rcAvail , CRect & rectTracker , CPoint point , BOOL bHoriz );
  119. //功能:设置控件的角框。
  120. void SetBoundRect( CRect rcBoundRect );
  121. private:
  122. //功能:翻转指定位置的象素,对设备上下文的每个象素进行非操作。
  123. void OnInvertTracker( CRect rc );
  124. private:
  125. CDC * m_pDC;
  126. BOOL m_bSolid;
  127. CRect m_rcBoundRect;
  128. };
  129. //功能:辅助类,用来监视那嵌控件的鼠标消息。
  130. class CSPMouseMonitor
  131. {
  132. public:
  133. //功能:调用此函数来监视鼠标消息。
  134. static void SetupHook( CWnd * pWndMonitor );
  135. protected:
  136. static LRESULT CALLBACK MouseProc( int nCode , WPARAM wParam , LPARAM lParam );
  137. private:
  138. static HHOOK m_hHookMouse; //A handle to a hook.
  139. static CWnd * m_pWndMonitor; //A pointer to the in-place control.
  140. };
  141. //功能:辅助类,用来进行各种绘制任务。
  142. class CSPDrawHelpers
  143. {
  144. protected:
  145. //功能:指向 Windows 的 GradientFill 函数的函数指针。
  146. typedef BOOL ( __stdcall *PFNGRADIENTFILL )( HDC , PTRIVERTEX , ULONG , PVOID , ULONG , ULONG );
  147. public:
  148. CSPDrawHelpers();
  149. virtual ~CSPDrawHelpers();
  150. private:
  151. HMODULE m_hMsImgDll; //Handle to MsImg32.dll.
  152. public:
  153. //功能:绘制渐变颜色。
  154. void GradientFill( CDC * pDC , LPRECT lpRect , COLORREF crFrom , COLORREF crTo , BOOL bHorz );
  155. //功能:绘制渐变颜色。
  156. void GradientFill( CDC * pDC , LPRECT lpRect , CSPPaintManagerColorGradient & grc , BOOL bHorz );
  157. //功能:剪切矩形四个顶角。
  158. void ExcludeCorners( CDC * pDC , CRect rc );
  159. //功能:混合指定点象素。
  160. void BlurPoints( CDC * pDC , LPPOINT pts , int nCount );
  161. //功能:混合指定的颜色。
  162. COLORREF BlendColors( COLORREF crA , COLORREF crB , float fAmountA );
  163. //功能:判断指定的字体是否存在。
  164. static BOOL AFX_CDECL FontExists( CString & strFaceName );
  165. //功能:获取默认字体。
  166. static CString AFX_CDECL GetDefaultFontName();
  167. //功能:获取垂直字体名。
  168. CString GetVerticalFontName( BOOL bUseOfficeFont );
  169. //功能:绘制三角形。
  170. static void AFX_CDECL Triangle( CDC * pDC , CPoint pt0 , CPoint pt1 , CPoint pt2 , COLORREF clr );
  171. static void AFX_CDECL Triangle( CDC * pDC , CPoint pt0 , CPoint pt1 , CPoint pt2 ); //<combine CSPDrawHelpers::Triangle@CDC*@CPoint@CPoint@CPoint@COLORREF>
  172. //功能:判断当前系统是否低分辨率。
  173. static BOOL AFX_CDECL IsLowResolution( HDC hDC = 0 );
  174. //功能:获取当前XP风格。
  175. SPCurrentSystemTheme GetCurrentSystemTheme();
  176. //功能:判断当前渐变色是否快速绘制。
  177. BOOL IsFastFillAvailable();
  178. //功能:使用渐变色填充矩形或三角形。
  179. BOOL GradientFill( HDC hdc , PTRIVERTEX pVertex , ULONG dwNumVertex , PVOID pMesh , ULONG dwNumMesh , ULONG dwMode );
  180. //功能:获取当前的工作区域。
  181. static CRect AFX_CDECL GetWorkArea();
  182. static CRect AFX_CDECL GetWorkArea( const CRect & rect ); //<COMBINE CSPDrawHelpers::GetWorkArea>
  183. static CRect AFX_CDECL GetWorkArea( const CWnd * pWnd ); //<COMBINE CSPDrawHelpers::GetWorkArea>
  184. private:
  185. PFNGRADIENTFILL m_pfnFastGradientFill;
  186. void GradientFillFast( CDC * pDC , LPRECT lpRect , COLORREF crFrom , COLORREF crTo , BOOL bHorz );
  187. void GradientFillSlow( CDC * pDC , LPRECT lpRect , COLORREF crFrom , COLORREF crTo , BOOL bHorz );
  188. static CSPDrawHelpers m_instance;
  189. friend CSPDrawHelpers * SPDrawHelpers();
  190. };
  191. CSPDrawHelpers * SPDrawHelpers();
  192. AFX_INLINE CSPDrawHelpers * SPDrawHelpers()
  193. {
  194. return &CSPDrawHelpers::m_instance;
  195. }
  196. AFX_INLINE BOOL CSPDrawHelpers::IsFastFillAvailable()
  197. {
  198. return m_pfnFastGradientFill != NULL;
  199. }
  200. AFX_INLINE void CSPDrawHelpers::Triangle( CDC * pDC , CPoint pt0 , CPoint pt1 , CPoint pt2 )
  201. {
  202. CPoint pts[] ={pt0, pt1, pt2};
  203. pDC->Polygon( pts,3 );
  204. }
  205. AFX_INLINE void CSPSplitterTracker::SetBoundRect( CRect rcBoundRect )
  206. {
  207. m_rcBoundRect = rcBoundRect;
  208. }
  209. #endif