HoverButton.cpp
上传用户:jsjyd88
上传日期:2007-01-01
资源大小:17k
文件大小:6k
源码类别:

按钮控件

开发平台:

Visual C++

  1. // HoverButton.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "HoverDlg.h"
  5. #include "HoverButton.h"
  6. #include "HoverButtonGroup.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CHoverButton
  14. BEGIN_MESSAGE_MAP(CHoverButton, CBitmapButton)
  15. //{{AFX_MSG_MAP(CHoverButton)
  16. ON_WM_MOUSEMOVE()
  17. ON_WM_LBUTTONUP()
  18. //}}AFX_MSG_MAP
  19. END_MESSAGE_MAP()
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CHoverButton construction / destruction
  22. CHoverButton::CHoverButton()
  23. {
  24. // To start with, the button is switched off and we are NOT tracking the mouse
  25. m_ButtonState = BUTTON_OFF;
  26. m_bMouseTracking = FALSE;
  27. m_pOwnerGroup = NULL;
  28. }
  29. CHoverButton::~CHoverButton()
  30. {
  31. }
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CHoverButton message handlers
  34. void CHoverButton::OnMouseMove(UINT nFlags, CPoint point) 
  35. {
  36. CBitmapButton::OnMouseMove(nFlags, point);
  37. // 1. Mouse has moved and we are not tracking this button, or 
  38. // 2. mouse has moved and the cursor was not above this window
  39. // == Is equivalent to WM_MOUSEENTER (for which there is no message)
  40. if((!m_bMouseTracking || GetCapture()!=this) && (m_ButtonState == BUTTON_OFF))
  41. {
  42. OnMouseEnter();
  43. }
  44. else 
  45. {
  46. if(m_ButtonState == BUTTON_OVER)
  47. {
  48. CRect rc;
  49. GetClientRect(&rc);
  50. if(!rc.PtInRect(point)) // The mouse cursor is no longer above this button
  51. OnMouseLeave();
  52. }
  53. }
  54. }
  55. void CHoverButton::OnMouseEnter(void)
  56. {
  57. // We are now tracking the mouse, OVER this button
  58. m_bMouseTracking = TRUE;
  59. m_ButtonState = BUTTON_OVER;
  60. // Ensure that mouse input is sent to the button
  61. SetCapture();
  62. Invalidate();
  63. UpdateWindow();
  64. }
  65. void CHoverButton::OnMouseLeave(void)
  66. {
  67. // We are not tracking the mouse, this button is OFF.
  68. m_ButtonState = BUTTON_OFF;
  69. m_bMouseTracking = FALSE;
  70. // Release mouse capture from the button and restore normal mouse input
  71. Invalidate();
  72. UpdateWindow();
  73. ReleaseCapture();
  74. }
  75. void CHoverButton::OnLButtonUp(UINT nFlags, CPoint point) 
  76. {
  77. SetButtonState(BUTTON_ON); // Highlight button
  78. CBitmapButton::OnLButtonUp(nFlags, point);
  79. if (m_pOwnerGroup)
  80. {
  81. m_pOwnerGroup->ButtonClicked(this);
  82. }
  83. }
  84. // Purpose: Set the new state of the button 
  85. // Return: Returns the old state of the button
  86. // Parameters: nState = Either ON or OFF. The default is OFF. This is NOT tri-state button!
  87. BUTTON_STATE CHoverButton::SetButtonState(BUTTON_STATE nState) 
  88. {
  89. BUTTON_STATE nOldState = (BUTTON_STATE)GetCheck();
  90. m_ButtonState = nState;
  91. switch(m_ButtonState)
  92. {
  93. case BUTTON_ON:
  94. EnableWindow(TRUE);
  95. SetState(BUTTON_ON);
  96. if (m_pOwnerGroup)
  97. {
  98. m_pOwnerGroup->ButtonClicked(this);
  99. }
  100. break;
  101. case BUTTON_GREYED:
  102. EnableWindow(FALSE);
  103. break;
  104. case BUTTON_OVER:
  105. EnableWindow(TRUE);
  106. SetState(BUTTON_OVER);
  107. break;
  108. default:
  109. EnableWindow(TRUE);
  110. SetState(BUTTON_OFF);
  111. m_ButtonState = BUTTON_OFF;
  112. break;
  113. }
  114. return(nOldState);
  115. }
  116. // Draws the buttons in their relevant state, and text labels
  117. void CHoverButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  118. {
  119. CDC memDC;
  120. CBitmap* pOld=NULL;
  121. CBitmap* pBitmap=NULL;
  122. CDC* pDC;
  123. CRect rc;
  124. int iSaveDC;
  125. pDC= CDC::FromHandle(lpDrawItemStruct->hDC);
  126. memDC.CreateCompatibleDC(pDC);
  127. VERIFY(pDC);
  128. iSaveDC=pDC->SaveDC();
  129. rc.CopyRect(&lpDrawItemStruct->rcItem);
  130. pDC->SetBkMode(TRANSPARENT);
  131. pDC->SetTextColor(GetSysColor(COLOR_WINDOWFRAME));// Black text color
  132. switch(m_ButtonState)
  133. {
  134. case BUTTON_ON:
  135. pBitmap=&m_bmpButtonDown;
  136. break;
  137. case BUTTON_OVER:
  138. pBitmap=&m_bmpButtonFocussed;
  139. break;
  140. case BUTTON_GREYED:
  141. pBitmap=&m_bmpButtonDisabled;
  142. break;
  143. default:
  144. pBitmap=&m_bmpButtonUp;
  145. break;
  146. }
  147. CString strTitle;
  148. GetWindowText(strTitle);
  149. if (pBitmap->m_hObject)
  150. {
  151. CRect rcBitmap(rc);
  152. BITMAP bmpInfo;
  153. CSize size;
  154. // Text
  155. size = pDC->GetTextExtent(strTitle);
  156. rcBitmap.OffsetRect(size.cx+5,0);
  157. // Draw bitmap
  158. if(!pBitmap->GetBitmap(&bmpInfo))
  159. return;
  160. pOld=memDC.SelectObject((CBitmap*) pBitmap);
  161. if (pOld==NULL) 
  162. return; //Destructors will clean up
  163. if(!pDC->BitBlt(0, 0, rc.Width(), rc.Height(), &memDC, 0, 0, SRCCOPY))
  164. return;
  165. memDC.SelectObject(pOld);
  166. if(memDC==NULL)
  167. return;
  168. }
  169. CRect rcText(rc);
  170. UINT nFormat = DT_CENTER;
  171. if(m_ButtonState == BUTTON_GREYED)
  172. {
  173. rcText.OffsetRect(1,1);
  174. pDC->SetTextColor(GetSysColor(COLOR_BTNHIGHLIGHT));
  175. pDC->DrawText(strTitle,rcText,nFormat);
  176. rcText.OffsetRect(-1,-1);
  177. pDC->SetTextColor(GetSysColor(COLOR_BTNSHADOW));
  178. pDC->DrawText(strTitle,rcText,nFormat);
  179. else
  180. pDC->DrawText(strTitle,rcText,nFormat);
  181. pDC->RestoreDC(iSaveDC);
  182. }
  183. BOOL CHoverButton::LoadBitmaps(UINT nBitmapUp, UINT nBitmapDown, 
  184.    UINT nBitmapFocus, UINT nBitmapDisabled)
  185. {
  186. return LoadBitmaps(MAKEINTRESOURCE(nBitmapUp),
  187. MAKEINTRESOURCE(nBitmapDown),
  188. MAKEINTRESOURCE(nBitmapFocus),
  189. MAKEINTRESOURCE(nBitmapDisabled));
  190. }
  191. BOOL CHoverButton::LoadBitmaps(LPCSTR lpszBitmapUp, LPCSTR lpszBitmapDown, 
  192.    LPCSTR lpszBitmapFocus, LPCSTR lpszBitmapDisabled)
  193. {
  194. BOOL bAllLoaded=TRUE;
  195. //Delete old ones
  196. m_bmpButtonDown.DeleteObject();
  197. m_bmpButtonFocussed.DeleteObject();
  198. m_bmpButtonUp.DeleteObject();
  199. m_bmpButtonDisabled.DeleteObject();
  200. if (!m_bmpButtonUp.LoadBitmap(lpszBitmapUp))
  201. {
  202. TRACE0("Failed to load up bitmap of bitmap buttonn");
  203. return FALSE;
  204. }
  205. if (lpszBitmapDown!=NULL)
  206. {
  207. if (!m_bmpButtonDown.LoadBitmap(lpszBitmapDown))
  208. {
  209. TRACE0("Failed to load down bitmap of bitmap buttonn");
  210. return bAllLoaded=FALSE;
  211. }
  212. }
  213. if (lpszBitmapFocus!=NULL)
  214. {
  215. if (!m_bmpButtonFocussed.LoadBitmap(lpszBitmapFocus))
  216. {
  217. TRACE0("Failed to load focussed bitmap of bitmap buttonn");
  218. return bAllLoaded=FALSE;
  219. }
  220. }
  221. if (lpszBitmapDisabled!=NULL)
  222. {
  223. if (!m_bmpButtonDisabled.LoadBitmap(lpszBitmapDisabled))
  224. {
  225. TRACE0("Failed to load disabled bitmap of bitmap buttonn");
  226. return bAllLoaded=FALSE;
  227. }
  228. }
  229. return bAllLoaded;
  230. }
  231. void CHoverButton::PreSubclassWindow() 
  232. {
  233. LoadBitmaps(IDB_RBU, IDB_RBD, IDB_RBF, IDB_RBX);
  234. CBitmapButton::PreSubclassWindow();
  235. }