XPButton.cpp 6.8 KB

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