XPButton.cpp
上传用户:aymxxn
上传日期:2014-11-29
资源大小:135k
文件大小:6k
源码类别:

文件操作

开发平台:

Visual C++

  1. // XPButton.cpp : implementation file
  2. // Modified by jingzhou xu
  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. IMPLEMENT_DYNCREATE(CXPButton, CButton)
  13. CXPButton::CXPButton()
  14. {
  15. pBoundryPen.CreatePen(PS_INSIDEFRAME|PS_SOLID,1,RGB(0,0,0));
  16. pInsideBoundryPenLeft.CreatePen(PS_INSIDEFRAME|PS_SOLID,3,RGB(250,196,88)); 
  17. pInsideBoundryPenRight.CreatePen(PS_INSIDEFRAME|PS_SOLID,3,RGB(251,202,106)); 
  18. pInsideBoundryPenTop.CreatePen(PS_INSIDEFRAME|PS_SOLID,2,RGB(252,210,121)); 
  19. pInsideBoundryPenBottom.CreatePen(PS_INSIDEFRAME|PS_SOLID,2,RGB(229,151,0)); 
  20. pFillActive.CreateSolidBrush(RGB(222,223,236));
  21. pFillInactive.CreateSolidBrush(RGB(222,223,236));
  22. m_bOverControl = m_bTracking = 0;
  23. }
  24. CXPButton::~CXPButton()
  25. {
  26. // TODO: Cleanup your control's instance data here.
  27. pBoundryPen.DeleteObject();
  28. pFillActive.DeleteObject();
  29. pFillInactive.DeleteObject();
  30. pInsideBoundryPenLeft.DeleteObject();
  31. pInsideBoundryPenRight.DeleteObject();
  32. pInsideBoundryPenBottom.DeleteObject();
  33. pInsideBoundryPenTop.DeleteObject();
  34. }
  35. BEGIN_MESSAGE_MAP(CXPButton, CButton)
  36. //{{AFX_MSG_MAP(CXPButton)
  37. // NOTE - the ClassWizard will add and remove mapping macros here.
  38. //}}AFX_MSG_MAP
  39. ON_WM_MOUSEMOVE()
  40. ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
  41. ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
  42. END_MESSAGE_MAP()
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CXPButton message handlers
  45. void CXPButton::DoGradientFill(CDC *pDC, CRect rect)
  46. {
  47. CBrush pBrush[64];
  48. int nWidth = (rect.right) - (rect.left);
  49.     int nHeight = (rect.bottom) - (rect.top);
  50.     CRect rct;
  51.     for (int i=0; i<64; i++)
  52. {
  53. if(m_bOverControl)
  54. {
  55. if(Focus)
  56. {
  57. pBrush[i].CreateSolidBrush(RGB(255-(i/4), 255-(i/4), 255-(i/3)));
  58. }
  59. else
  60. {
  61. pBrush[i].CreateSolidBrush(RGB(255-(i/4), 255-(i/4), 255-(i/5)));
  62. }
  63. }
  64. else
  65. {
  66. if(Focus)
  67. {
  68. pBrush[i].CreateSolidBrush(RGB(255-(i/3), 255-(i/3), 255-(i/4)));
  69. }
  70. else
  71. {
  72. pBrush[i].CreateSolidBrush(RGB(255-(i/3), 255-(i/3), 255-(i/5)));
  73. }
  74. }
  75. }
  76.     for (i=rect.top; i<nHeight+2; i++) 
  77. {
  78.         rct.SetRect (rect.left, i, nWidth+2, i + 1);
  79.         pDC->FillRect (&rct, &pBrush[((i * 63) / nHeight)]);
  80.     }
  81.     for (i=0; i<64; i++)
  82. {
  83. pBrush[i].DeleteObject();
  84. }
  85. }
  86. void CXPButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  87. {
  88. // TODO: Add your code to draw the specified item
  89. CDC* pDC   = CDC::FromHandle(lpDrawItemStruct->hDC);
  90.     CRect rect = lpDrawItemStruct->rcItem;
  91.     UINT state = lpDrawItemStruct->itemState;
  92. CPoint pt;
  93. CString strText;
  94. CString &m_title = strText;
  95. GetWindowText(strText);
  96.     // draw the control edges (DrawFrameControl is handy!)
  97. pt.x = 10;
  98. pt.y = 10;
  99. pOldPen = pDC->SelectObject(&pBoundryPen);
  100. pDC->RoundRect(rect,pt);
  101. if(state & ODS_FOCUS)
  102. {
  103. Focus = TRUE;
  104. }
  105. else
  106. {
  107. Focus = FALSE;
  108. }
  109.     if (state & ODS_SELECTED)
  110. {
  111. //pDC->DrawFrameControl(rect, DFC_BUTTON, DFCS_BUTTONPUSH | DFCS_PUSHED);
  112. ;   
  113. }
  114.     else
  115. {
  116. // pDC->DrawFrameControl(rect, DFC_BUTTON, DFCS_BUTTONPUSH);
  117. ;
  118. }
  119.     
  120.     pDC->SelectObject(pOldPen);
  121. // Deflate the drawing rect by the size of the button's edges
  122.     rect.DeflateRect( CSize(GetSystemMetrics(SM_CXEDGE), GetSystemMetrics(SM_CYEDGE)));
  123.     // Fill the interior color if necessary
  124. if (m_bOverControl)
  125. {
  126. pOldBrush = pDC->SelectObject(&pFillActive);   
  127. //pDC->FloodFill(rect.left+5,rect.top+5,RGB(0,0,0));  //pDC->FillSolidRect(rect, RGB(255, 255, 0)); // yellow
  128. DoGradientFill(pDC,rect);
  129. DrawInsideBorder(pDC,rect);
  130. }
  131. else
  132. {
  133. pOldBrush = pDC->SelectObject(&pFillInactive);   
  134. //pDC->FloodFill(rect.left+5,rect.top+5,RGB(0,0,0));  //pDC->FillSolidRect(rect, RGB(0, 0, 255)); // yellow
  135. DoGradientFill(pDC,rect);
  136. }
  137. pDC->SelectObject(pOldBrush);
  138.     
  139. // Draw the text
  140.     if (!m_title.IsEmpty()/*!strText.IsEmpty()*/)
  141.     {
  142.         CSize Extent = pDC->GetTextExtent(m_title/*strText*/);
  143.         CPoint pt( rect.CenterPoint().x - Extent.cx/2, 
  144. rect.CenterPoint().y - Extent.cy/2 );
  145.         if (state & ODS_SELECTED) 
  146.             pt.Offset(1,1);
  147.         int nMode = pDC->SetBkMode(TRANSPARENT);
  148.         //pOldPen = pDC->SelectObject(pBoundryPen);
  149.         //CFont *pOldFont = SelectStockFont( pDC );
  150.         if (state & ODS_DISABLED)
  151.             pDC->DrawState(pt, Extent, m_title/*strCaption*/, DSS_DISABLED, TRUE, 0, (HBRUSH)NULL);
  152.         else
  153.             pDC->TextOut(pt.x, pt.y, m_title/*strCaption*/);
  154.         //pDC->SelectObject(pOldPen);
  155. //pDC->SelectObject(pOldFont);
  156.         pDC->SetBkMode(nMode);
  157.     }
  158. return;
  159. }
  160. void CXPButton::PreSubclassWindow() 
  161. {
  162. // TODO: Add your specialized code here and/or call the base class
  163. ModifyStyle(0, BS_OWNERDRAW);
  164. CButton::PreSubclassWindow();
  165. }
  166. void CXPButton::DrawInsideBorder(CDC *pDC, CRect rect)
  167. {
  168. CPoint oldpoint = pDC->MoveTo(rect.left,rect.bottom-3);
  169. pOldPen = pDC->SelectObject(&pInsideBoundryPenLeft);
  170. pDC->LineTo(rect.left,rect.top+2);
  171. pDC->SelectObject(&pInsideBoundryPenRight);
  172. pDC->MoveTo(rect.right-1,rect.bottom-3);
  173. pDC->LineTo(rect.right-1,rect.top+2);
  174. pDC->SelectObject(&pInsideBoundryPenTop);
  175. pDC->MoveTo(rect.left+2,rect.top);
  176. pDC->LineTo(rect.right-2,rect.top);
  177. pDC->SelectObject(&pInsideBoundryPenBottom);
  178. pDC->MoveTo(rect.left+2,rect.bottom);
  179. pDC->LineTo(rect.right-2,rect.bottom);
  180. pDC->SelectObject(pOldPen);
  181. pDC->MoveTo(oldpoint);
  182. }
  183. void CXPButton::OnMouseMove(UINT nFlags, CPoint point)
  184. {
  185. if (!m_bTracking)
  186. {
  187. TRACKMOUSEEVENT tme;
  188. tme.cbSize = sizeof(tme);
  189. tme.hwndTrack = m_hWnd;
  190. tme.dwFlags = TME_LEAVE|TME_HOVER;
  191. tme.dwHoverTime = 1;
  192. m_bTracking = _TrackMouseEvent(&tme);
  193. }
  194. CButton::OnMouseMove(nFlags, point);
  195. }
  196. void CXPButton::OnMouseHover(WPARAM wparam, LPARAM lparam)
  197. {
  198. m_bOverControl=TRUE;
  199. Invalidate();
  200. }
  201. LRESULT CXPButton::OnMouseLeave(WPARAM wparam, LPARAM lparam)
  202. {
  203. m_bTracking = FALSE;
  204. m_bOverControl = FALSE;
  205. Invalidate(FALSE);
  206. return 0;
  207. }