AdvButton.cpp
上传用户:shcaka8
上传日期:2013-04-06
资源大小:103k
文件大小:4k
源码类别:

家庭/个人应用

开发平台:

Visual C++

  1. // AdvButton.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MyAssistant.h"
  5. #include "AdvButton.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. #define MAXCAPTIONLEN 64
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CAdvButton
  14. CAdvButton::CAdvButton()
  15. {
  16. //initialize member variable
  17. m_ClientRect.left  = 0;
  18. m_ClientRect.top   = 0;
  19. m_ClientRect.right = 0;
  20. m_ClientRect.bottom= 0;
  21. m_ClientRgn.DeleteObject();
  22. m_ClientRgn.CreateEllipticRgnIndirect(&m_ClientRect);
  23. m_State = 0;
  24. m_Point.x = m_Point.y = 0;
  25. m_IsTimerOn = FALSE;
  26. m_BtnType = 0;
  27. m_IsPushed = FALSE;
  28. }
  29. CAdvButton::~CAdvButton()
  30. {
  31. }
  32. BEGIN_MESSAGE_MAP(CAdvButton, CButton)
  33. //{{AFX_MSG_MAP(CAdvButton)
  34. ON_WM_LBUTTONDOWN()
  35. ON_WM_LBUTTONUP()
  36. ON_WM_MOUSEMOVE()
  37. ON_WM_TIMER()
  38. ON_WM_CREATE()
  39. //}}AFX_MSG_MAP
  40. END_MESSAGE_MAP()
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CAdvButton message handlers
  43. BOOL CAdvButton::Create(LPCTSTR lpszCaption,DWORD dwStyle,const RECT& rect,CWnd *pParentWnd,UINT nID)
  44. {
  45. return CButton::Create(lpszCaption, dwStyle, rect, pParentWnd, nID);
  46. }
  47. void CAdvButton::PreSubclassWindow() 
  48. {
  49. //modify style
  50. ModifyStyle(0, BS_FLAT|BS_OWNERDRAW|BS_PUSHBUTTON);
  51. CButton::PreSubclassWindow();
  52. }
  53. int CAdvButton::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  54. {
  55. if (CButton::OnCreate(lpCreateStruct) == -1)
  56. return -1;
  57. return 0;
  58. }
  59. void CAdvButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  60. {
  61. //get client rect
  62. GetClientRect(&m_ClientRect);
  63. CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  64. if ( m_BtnType == 0) {
  65. CRect rc0(m_ClientRect.left+1,
  66.   m_ClientRect.top+1,
  67.   m_ClientRect.right-1,
  68.   m_ClientRect.bottom-1);
  69. CRect rc1(m_ClientRect.left+2,
  70.   m_ClientRect.top+2,
  71.   m_ClientRect.right-2,
  72.   m_ClientRect.bottom-2);
  73. CRect rc2(m_ClientRect.left+8,
  74.   m_ClientRect.top+8,
  75.   m_ClientRect.right-8,
  76.   m_ClientRect.bottom-8);
  77. if(m_IsPushed)
  78. {
  79. pDC->Rectangle(&rc0);
  80. pDC->FillRect(&rc1,new CBrush(WHITE));
  81. if(m_State == 1) {
  82. pDC->FillRect(&rc2,new CBrush(SYSCOLOR));
  83. }
  84. }
  85. else
  86. {
  87. pDC->Rectangle(&m_ClientRect);
  88. pDC->FillRect(&rc1,new CBrush(SYSCOLOR));
  89. if(m_State == 1) {
  90. pDC->FillRect(&rc2,new CBrush(WHITE));
  91. }
  92. }
  93. LPTSTR pCaption = new char[MAXCAPTIONLEN]; //
  94. int iLen = GetWindowText(pCaption,MAXCAPTIONLEN);
  95. pDC->SetBkMode(TRANSPARENT);
  96. pDC->SetTextColor(RGB(0,0,0));
  97. pDC->DrawText(pCaption,iLen,&m_ClientRect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
  98. }
  99. else if ( m_BtnType == 1 ) {
  100. }
  101. else if ( m_BtnType == 2 ) {
  102. }
  103. }
  104. void CAdvButton::OnLButtonDown(UINT nFlags, CPoint point) 
  105. {
  106. // TODO: Add your message handler code here and/or call default
  107. CRect rect;
  108. GetWindowRect(&rect);
  109. GetCursorPos(&m_Point);
  110. if((rect.PtInRect(m_Point))&&(m_State != 2))
  111. {
  112. m_State = 2; //2:select state
  113. Invalidate();
  114. }
  115. CButton::OnLButtonDown(nFlags, point);
  116. }
  117. void CAdvButton::OnLButtonUp(UINT nFlags, CPoint point) 
  118. {
  119. // TODO: Add your message handler code here and/or call default
  120. CRect rect;
  121. GetWindowRect(&rect);
  122. GetCursorPos(&m_Point);
  123. if((rect.PtInRect(m_Point))&&(m_State != 1))
  124. {
  125. m_State = 1; //1:focus state
  126. Invalidate();
  127. }
  128. CButton::OnLButtonUp(nFlags, point);
  129. }
  130. void CAdvButton::OnMouseMove(UINT nFlags, CPoint point) 
  131. {
  132. // TODO: Add your message handler code here and/or call default
  133. if(!m_IsTimerOn)
  134. {
  135. SetTimer(1000,100,NULL);
  136. m_IsTimerOn = TRUE;
  137. }
  138. CButton::OnMouseMove(nFlags, point);
  139. }
  140. void CAdvButton::OnTimer(UINT nIDEvent) 
  141. {
  142. // TODO: Add your message handler code here and/or call default
  143. CRect rect;
  144. GetWindowRect(&rect);
  145. GetCursorPos(&m_Point);
  146. if(rect.PtInRect(m_Point))
  147. {
  148. if((m_State != 1)&&(m_State != 2)) //
  149. {
  150. m_State = 1;
  151. Invalidate();
  152. }
  153. }
  154. else
  155. {
  156. if(m_State != 0)
  157. {
  158. m_State = 0;
  159. Invalidate();
  160. }
  161. KillTimer(nIDEvent);
  162. m_IsTimerOn = FALSE;
  163. }
  164. CButton::OnTimer(nIDEvent);
  165. }
  166. void CAdvButton::SetType(UINT piType)
  167. {
  168. m_BtnType = piType;
  169. }