XPButton.cpp
上传用户:cuijiu615
上传日期:2007-03-28
资源大小:45k
文件大小: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. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CXPButton message handlers
  50. //添加Owner Draw属性
  51. void CXPButton::PreSubclassWindow() 
  52. {
  53. // TODO: Add your specialized code here and/or call the base class
  54. CButton::PreSubclassWindow();
  55. ModifyStyle(0, BS_OWNERDRAW);
  56. }
  57. void CXPButton::OnMouseMove(UINT nFlags, CPoint point) 
  58. {
  59. // TODO: Add your message handler code here and/or call default
  60. if (!m_bTracking)
  61. {
  62. TRACKMOUSEEVENT tme;
  63. tme.cbSize = sizeof(tme);
  64. tme.hwndTrack = m_hWnd;
  65. tme.dwFlags = TME_LEAVE | TME_HOVER;
  66. tme.dwHoverTime = 1;
  67. m_bTracking = _TrackMouseEvent(&tme);
  68. }
  69. CButton::OnMouseMove(nFlags, point);
  70. }
  71. LRESULT CXPButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
  72. {
  73. m_bOver = FALSE;
  74. m_bTracking = FALSE;
  75. InvalidateRect(NULL, FALSE);
  76. return 0;
  77. }
  78. LRESULT CXPButton::OnMouseHover(WPARAM wParam, LPARAM lParam)
  79. {
  80. m_bOver = TRUE;
  81. InvalidateRect(NULL);
  82. return 0;
  83. }
  84. void CXPButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  85. {
  86. //从lpDrawItemStruct获取控件的相关信息
  87. CRect rect =  lpDrawItemStruct->rcItem;
  88. CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
  89. int nSaveDC=pDC->SaveDC();
  90. UINT state = lpDrawItemStruct->itemState;
  91. POINT pt ;
  92. TCHAR strText[MAX_PATH + 1];
  93. ::GetWindowText(m_hWnd, strText, MAX_PATH);
  94. //画按钮的外边框,它是一个半径为5的圆角矩形
  95. pt.x = 5;
  96. pt.y = 5;
  97. CPen* hOldPen = pDC->SelectObject(&m_BoundryPen);
  98. pDC->RoundRect(&rect, pt);
  99. //获取按钮的状态
  100. if (state & ODS_FOCUS)
  101. {
  102. m_bFocus = TRUE;
  103. m_bSelected = TRUE;
  104. }
  105. else
  106. {
  107. m_bFocus = FALSE;
  108. m_bSelected = FALSE;
  109. }
  110. if (state & ODS_SELECTED || state & ODS_DEFAULT)
  111. {
  112. m_bFocus = TRUE;
  113. }
  114. pDC->SelectObject(hOldPen);
  115. rect.DeflateRect(CSize(GetSystemMetrics(SM_CXEDGE), GetSystemMetrics(SM_CYEDGE)));
  116. //根据按钮的状态填充按钮的底色
  117. CBrush* pOldBrush;
  118. if (m_bOver)
  119. {
  120. pOldBrush = pDC->SelectObject(&m_FillActive);
  121. DoGradientFill(pDC, &rect);
  122. }
  123. else
  124. {
  125. pOldBrush = pDC->SelectObject(&m_FillInactive);
  126. DoGradientFill(pDC, &rect);
  127. }
  128. //根据按钮的状态绘制内边框
  129. if (m_bOver || m_bSelected)
  130. DrawInsideBorder(pDC, &rect);
  131. pDC->SelectObject(pOldBrush);
  132. //显示按钮的文本
  133. if (strText!=NULL)
  134. {
  135. CFont* hFont = GetFont();
  136. CFont* hOldFont = pDC->SelectObject(hFont);
  137. CSize szExtent = pDC->GetTextExtent(strText, lstrlen(strText));
  138. CPoint pt( rect.CenterPoint().x - szExtent.cx / 2, rect.CenterPoint().y - szExtent.cy / 2);
  139. if (state & ODS_SELECTED) 
  140. pt.Offset(1, 1);
  141. int nMode = pDC->SetBkMode(TRANSPARENT);
  142. if (state & ODS_DISABLED)
  143. pDC->DrawState(pt, szExtent, strText, DSS_DISABLED, TRUE, 0, (HBRUSH)NULL);
  144. else
  145. pDC->DrawState(pt, szExtent, strText, DSS_NORMAL, TRUE, 0, (HBRUSH)NULL);
  146. pDC->SelectObject(hOldFont);
  147. pDC->SetBkMode(nMode);
  148. }
  149. pDC->RestoreDC(nSaveDC);
  150. }
  151. //绘制按钮的底色
  152. void CXPButton::DoGradientFill(CDC *pDC, CRect* rect)
  153. {
  154. CBrush brBk[64];
  155. int nWidth = rect->Width();
  156. int nHeight = rect->Height();
  157. CRect rct;
  158. for (int i = 0; i < 64; i ++)
  159. {
  160. if (m_bOver)
  161. {
  162. if (m_bFocus)
  163. brBk[i].CreateSolidBrush(RGB(255 - (i / 4), 255 - (i / 4), 255 - (i / 3)));
  164. else
  165. brBk[i].CreateSolidBrush(RGB(255 - (i / 4), 255 - (i / 4), 255 - (i / 5)));
  166. }
  167. else
  168. {
  169. if (m_bFocus)
  170. brBk[i].CreateSolidBrush(RGB(255 - (i / 3), 255 - (i / 3), 255 - (i / 4)));
  171. else
  172. brBk[i].CreateSolidBrush(RGB(255 - (i / 3), 255 - (i / 3), 255 - (i / 5)));
  173. }
  174. }
  175. for (i = rect->top; i <= nHeight + 2; i ++) 
  176. {
  177. rct.SetRect(rect->left, i, nWidth + 2, i + 1);
  178. pDC->FillRect(&rct, &brBk[((i * 63) / nHeight)]);
  179. }
  180. for (i = 0; i < 64; i ++)
  181. brBk[i].DeleteObject();
  182. }
  183. //绘制按钮的内边框
  184. void CXPButton::DrawInsideBorder(CDC *pDC, CRect* rect)
  185. {
  186. CPen *pLeft, *pRight, *pTop, *pBottom;
  187. if (m_bSelected && !m_bOver)
  188. {
  189. pLeft = & m_InsideBoundryPenLeftSel;
  190. pRight = &m_InsideBoundryPenRightSel;
  191. pTop = &m_InsideBoundryPenTopSel;
  192. pBottom = &m_InsideBoundryPenBottomSel;
  193. }
  194. else
  195. {
  196. pLeft = &m_InsideBoundryPenLeft;
  197. pRight = &m_InsideBoundryPenRight;
  198. pTop = &m_InsideBoundryPenTop;
  199. pBottom = &m_InsideBoundryPenBottom;
  200. }
  201. CPoint oldPoint = pDC->MoveTo(rect->left, rect->bottom - 1);
  202. CPen* pOldPen = pDC->SelectObject(pLeft);
  203. pDC->LineTo(rect->left, rect->top + 1);
  204. pDC->SelectObject(pRight);
  205. pDC->MoveTo(rect->right - 1, rect->bottom - 1);
  206. pDC->LineTo(rect->right - 1, rect->top);
  207. pDC->SelectObject(pTop);
  208. pDC->MoveTo(rect->left - 1, rect->top);
  209. pDC->LineTo(rect->right - 1, rect->top);
  210. pDC->SelectObject(pBottom);
  211. pDC->MoveTo(rect->left, rect->bottom);
  212. pDC->LineTo(rect->right - 1, rect->bottom);
  213. pDC->SelectObject(pOldPen);
  214. pDC->MoveTo(oldPoint);
  215. if (m_bSelected && !m_bOver)
  216. DrawFocusRect(pDC->m_hDC,rect);
  217. }