XPButton.cpp
上传用户:lj3531212
上传日期:2007-06-18
资源大小:346k
文件大小:7k
源码类别:

绘图程序

开发平台:

Visual C++

  1. // XPButton.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "XPButton.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CXPButton
  12. CXPButton::CXPButton()
  13. {
  14. m_BoundryPen.CreatePen(PS_INSIDEFRAME | PS_SOLID, 1, RGB(0, 0, 0));
  15. m_InsideBoundryPenLeft.CreatePen(PS_INSIDEFRAME | PS_SOLID, 3, RGB(250, 196, 88)); 
  16. m_InsideBoundryPenRight.CreatePen(PS_INSIDEFRAME | PS_SOLID, 3, RGB(251, 202, 106));
  17. m_InsideBoundryPenTop.CreatePen(PS_INSIDEFRAME | PS_SOLID, 2, RGB(252, 210, 121));
  18. m_InsideBoundryPenBottom.CreatePen(PS_INSIDEFRAME | PS_SOLID, 2, RGB(229, 151, 0));
  19. m_FillActive.CreateSolidBrush(RGB(223, 222, 236));
  20. m_FillInactive.CreateSolidBrush(RGB(222, 223, 236));
  21. m_InsideBoundryPenLeftSel.CreatePen(PS_INSIDEFRAME | PS_SOLID, 3, RGB(153, 198, 252)); 
  22. m_InsideBoundryPenTopSel.CreatePen(PS_INSIDEFRAME | PS_SOLID, 2, RGB(162, 201, 255));
  23. m_InsideBoundryPenRightSel.CreatePen(PS_INSIDEFRAME | PS_SOLID, 3, RGB(162, 189, 252));
  24. m_InsideBoundryPenBottomSel.CreatePen(PS_INSIDEFRAME | PS_SOLID, 2, RGB(162, 201, 255));
  25. m_bOver = m_bSelected = m_bTracking = m_bFocus = FALSE;
  26. }
  27. CXPButton::~CXPButton()
  28. {
  29. m_BoundryPen.DeleteObject();
  30. m_InsideBoundryPenLeft.DeleteObject();
  31. m_InsideBoundryPenRight.DeleteObject();
  32. m_InsideBoundryPenTop.DeleteObject();
  33. m_InsideBoundryPenBottom.DeleteObject();
  34. m_FillActive.DeleteObject();
  35. m_FillInactive.DeleteObject();
  36. m_InsideBoundryPenLeftSel.DeleteObject();
  37. m_InsideBoundryPenTopSel.DeleteObject();
  38. m_InsideBoundryPenRightSel.DeleteObject();
  39. m_InsideBoundryPenBottomSel.DeleteObject();
  40. }
  41. BEGIN_MESSAGE_MAP(CXPButton, CButton)
  42. //{{AFX_MSG_MAP(CXPButton)
  43. ON_WM_MOUSEMOVE()
  44. ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
  45. ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
  46. ON_WM_ERASEBKGND()
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CXPButton message handlers
  51. //添加Owner Draw属性
  52. void CXPButton::PreSubclassWindow() 
  53. {
  54. // TODO: Add your specialized code here and/or call the base class
  55. CButton::PreSubclassWindow();
  56. ModifyStyle(0, BS_OWNERDRAW);
  57. //设置按钮的有效区域
  58. CRgn rgn;
  59. CRect rc;
  60. GetClientRect(&rc);
  61. //有效区域为一个角半径为5的圆角矩形
  62. rgn.CreateRoundRectRgn(rc.left,rc.top,rc.right,rc.bottom,5,5);
  63. SetWindowRgn(rgn,TRUE);
  64. rgn.DeleteObject();
  65. }
  66. void CXPButton::OnMouseMove(UINT nFlags, CPoint point) 
  67. {
  68. // TODO: Add your message handler code here and/or call default
  69. if (!m_bTracking)
  70. {
  71. TRACKMOUSEEVENT tme;
  72. tme.cbSize = sizeof(tme);
  73. tme.hwndTrack = m_hWnd;
  74. tme.dwFlags = TME_LEAVE | TME_HOVER;
  75. tme.dwHoverTime = 1;
  76. m_bTracking = _TrackMouseEvent(&tme);
  77. }
  78. CButton::OnMouseMove(nFlags, point);
  79. }
  80. LRESULT CXPButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
  81. {
  82. m_bOver = FALSE;
  83. m_bTracking = FALSE;
  84. InvalidateRect(NULL, FALSE);
  85. return 0;
  86. }
  87. LRESULT CXPButton::OnMouseHover(WPARAM wParam, LPARAM lParam)
  88. {
  89. m_bOver = TRUE;
  90. InvalidateRect(NULL);
  91. return 0;
  92. }
  93. void CXPButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  94. {
  95. //从lpDrawItemStruct获取控件的相关信息
  96. CRect rect =  lpDrawItemStruct->rcItem;
  97. CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
  98. int nSaveDC=pDC->SaveDC();
  99. UINT state = lpDrawItemStruct->itemState;
  100. POINT pt ;
  101. TCHAR strText[MAX_PATH + 1];
  102. ::GetWindowText(m_hWnd, strText, MAX_PATH);
  103. //画按钮的外边框,它是一个半径为5的圆角矩形
  104. pt.x = 5;
  105. pt.y = 5;
  106. CPen* hOldPen = pDC->SelectObject(&m_BoundryPen);
  107. pDC->RoundRect(&rect, pt);
  108. //获取按钮的状态
  109. if (state & ODS_FOCUS)
  110. {
  111. m_bFocus = TRUE;
  112. m_bSelected = TRUE;
  113. }
  114. else
  115. {
  116. m_bFocus = FALSE;
  117. m_bSelected = FALSE;
  118. }
  119. if (state & ODS_SELECTED || state & ODS_DEFAULT)
  120. {
  121. m_bFocus = TRUE;
  122. }
  123. pDC->SelectObject(hOldPen);
  124. rect.DeflateRect(CSize(GetSystemMetrics(SM_CXEDGE), GetSystemMetrics(SM_CYEDGE)));
  125. //根据按钮的状态填充按钮的底色
  126. CBrush* pOldBrush;
  127. if (m_bOver)
  128. {
  129. pOldBrush = pDC->SelectObject(&m_FillActive);
  130. DoGradientFill(pDC, &rect);
  131. }
  132. else
  133. {
  134. pOldBrush = pDC->SelectObject(&m_FillInactive);
  135. DoGradientFill(pDC, &rect);
  136. }
  137. //根据按钮的状态绘制内边框
  138. if (m_bOver || m_bSelected)
  139. DrawInsideBorder(pDC, &rect);
  140. pDC->SelectObject(pOldBrush);
  141. //显示按钮的文本
  142. if (strText!=NULL)
  143. {
  144. CFont* hFont = GetFont();
  145. CFont* hOldFont = pDC->SelectObject(hFont);
  146. CSize szExtent = pDC->GetTextExtent(strText, lstrlen(strText));
  147. CPoint pt( rect.CenterPoint().x - szExtent.cx / 2, rect.CenterPoint().y - szExtent.cy / 2);
  148. if (state & ODS_SELECTED) 
  149. pt.Offset(1, 1);
  150. int nMode = pDC->SetBkMode(TRANSPARENT);
  151. if (state & ODS_DISABLED)
  152. pDC->DrawState(pt, szExtent, strText, DSS_DISABLED, TRUE, 0, (HBRUSH)NULL);
  153. else
  154. pDC->DrawState(pt, szExtent, strText, DSS_NORMAL, TRUE, 0, (HBRUSH)NULL);
  155. pDC->SelectObject(hOldFont);
  156. pDC->SetBkMode(nMode);
  157. }
  158. pDC->RestoreDC(nSaveDC);
  159. }
  160. //绘制按钮的底色
  161. void CXPButton::DoGradientFill(CDC *pDC, CRect* rect)
  162. {
  163. CBrush brBk[64];
  164. int nWidth = rect->Width();
  165. int nHeight = rect->Height();
  166. CRect rct;
  167. for (int i = 0; i < 64; i ++)
  168. {
  169. if (m_bOver)
  170. {
  171. if (m_bFocus)
  172. brBk[i].CreateSolidBrush(RGB(255 - (i / 4), 255 - (i / 4), 255 - (i / 3)));
  173. else
  174. brBk[i].CreateSolidBrush(RGB(255 - (i / 4), 255 - (i / 4), 255 - (i / 5)));
  175. }
  176. else
  177. {
  178. if (m_bFocus)
  179. brBk[i].CreateSolidBrush(RGB(255 - (i / 3), 255 - (i / 3), 255 - (i / 4)));
  180. else
  181. brBk[i].CreateSolidBrush(RGB(255 - (i / 3), 255 - (i / 3), 255 - (i / 5)));
  182. }
  183. }
  184. for (i = rect->top; i <= nHeight + 2; i ++) 
  185. {
  186. rct.SetRect(rect->left, i, nWidth + 2, i + 1);
  187. pDC->FillRect(&rct, &brBk[((i * 63) / nHeight)]);
  188. }
  189. for (i = 0; i < 64; i ++)
  190. brBk[i].DeleteObject();
  191. }
  192. //绘制按钮的内边框
  193. void CXPButton::DrawInsideBorder(CDC *pDC, CRect* rect)
  194. {
  195. CPen *pLeft, *pRight, *pTop, *pBottom;
  196. if (m_bSelected && !m_bOver)
  197. {
  198. pLeft = & m_InsideBoundryPenLeftSel;
  199. pRight = &m_InsideBoundryPenRightSel;
  200. pTop = &m_InsideBoundryPenTopSel;
  201. pBottom = &m_InsideBoundryPenBottomSel;
  202. }
  203. else
  204. {
  205. pLeft = &m_InsideBoundryPenLeft;
  206. pRight = &m_InsideBoundryPenRight;
  207. pTop = &m_InsideBoundryPenTop;
  208. pBottom = &m_InsideBoundryPenBottom;
  209. }
  210. CPoint oldPoint = pDC->MoveTo(rect->left, rect->bottom - 1);
  211. CPen* pOldPen = pDC->SelectObject(pLeft);
  212. pDC->LineTo(rect->left, rect->top + 1);
  213. pDC->SelectObject(pRight);
  214. pDC->MoveTo(rect->right - 1, rect->bottom - 1);
  215. pDC->LineTo(rect->right - 1, rect->top);
  216. pDC->SelectObject(pTop);
  217. pDC->MoveTo(rect->left - 1, rect->top);
  218. pDC->LineTo(rect->right - 1, rect->top);
  219. pDC->SelectObject(pBottom);
  220. pDC->MoveTo(rect->left, rect->bottom);
  221. pDC->LineTo(rect->right - 1, rect->bottom);
  222. pDC->SelectObject(pOldPen);
  223. pDC->MoveTo(oldPoint);
  224. if (m_bSelected && !m_bOver)
  225. DrawFocusRect(pDC->m_hDC,rect);
  226. }
  227. BOOL CXPButton::OnEraseBkgnd(CDC* pDC) 
  228. {
  229. // TODO: Add your message handler code here and/or call default
  230. return true;
  231. }