ToolTipBitmapButton.cpp
Upload User: kairuinn
Upload Date: 2009-02-07
Package Size: 2922k
Code Size: 4k
Category:

Graph program

Development Platform:

Visual C++

  1. // ToolTipBitmapButton.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ToolTipBitmapButton.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CToolTipBitmapButton
  12. CToolTipBitmapButton::CToolTipBitmapButton()
  13. :m_bHover(FALSE)
  14. ,m_bTracking(FALSE)
  15. ,m_pressed(false)
  16. ,m_mouse_hover_style(false)
  17. {
  18. }
  19. CToolTipBitmapButton::~CToolTipBitmapButton()
  20. {
  21. }
  22. BEGIN_MESSAGE_MAP(CToolTipBitmapButton, baseCToolTipBitmapButton)
  23. //{{AFX_MSG_MAP(CToolTipBitmapButton)
  24. ON_WM_MOUSEMOVE()
  25. ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
  26. ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
  27. ON_WM_KEYUP()
  28. //}}AFX_MSG_MAP
  29. END_MESSAGE_MAP()
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CToolTipBitmapButton message handlers
  32. BOOL CToolTipBitmapButton::PreTranslateMessage(MSG* pMsg) 
  33. {
  34. // TODO: Add your specialized code here and/or call the base class
  35. return baseCToolTipBitmapButton::PreTranslateMessage(pMsg);
  36. }
  37. void CToolTipBitmapButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  38. {
  39. // TODO: Add your code to draw the specified item
  40. CDC * pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  41. CDC * pMemDC = new CDC;
  42. pMemDC->CreateCompatibleDC(pDC);
  43. CBitmap * pOldBitmap;
  44. pOldBitmap = pMemDC->SelectObject(&m_Bitmap);
  45. CPoint point(0,0);
  46. if (IsWindowEnabled()) {
  47. if ( (lpDrawItemStruct->itemState & ODS_SELECTED) || m_pressed)
  48. {
  49. //pDC->BitBlt(0, 0, m_ButtonSize.cx, m_ButtonSize.cy, pMemDC, m_ButtonSize.cx, 0, SRCCOPY);
  50. pDC->TransparentBlt(0, 0, m_ButtonSize.cx, m_ButtonSize.cy, pMemDC, 
  51. m_ButtonSize.cx, 0,m_ButtonSize.cx, m_ButtonSize.cy,RGB(255,255,0));
  52. } else {
  53. if (m_bHover) // focused ?
  54. {
  55. //pDC->BitBlt(0, 0, m_ButtonSize.cx, m_ButtonSize.cy, pMemDC, m_ButtonSize.cx*2, 0, SRCCOPY);
  56. pDC->TransparentBlt(0, 0, m_ButtonSize.cx, m_ButtonSize.cy, pMemDC, 
  57. m_ButtonSize.cx*2, 0,m_ButtonSize.cx, m_ButtonSize.cy,RGB(255,255,0));
  58. } else {
  59. //pDC->BitBlt(0,0,m_ButtonSize.cx,m_ButtonSize.cy,pMemDC,0,0,SRCCOPY);
  60. pDC->TransparentBlt(0, 0, m_ButtonSize.cx, m_ButtonSize.cy, pMemDC, 
  61. 0, 0,m_ButtonSize.cx, m_ButtonSize.cy,RGB(255,255,0));
  62. }
  63. }
  64. } else {
  65. //pDC->BitBlt(0, 0, m_ButtonSize.cx, m_ButtonSize.cy, pMemDC, m_ButtonSize.cx*3, 0, SRCCOPY);
  66. pDC->TransparentBlt(0, 0, m_ButtonSize.cx, m_ButtonSize.cy, pMemDC, 
  67. m_ButtonSize.cx*3, 0,m_ButtonSize.cx, m_ButtonSize.cy,RGB(255,255,0));
  68. }
  69. // clean up
  70. pMemDC -> SelectObject(pOldBitmap);
  71. delete pMemDC;
  72. }
  73. // Load a bitmap from the resources in the button, the bitmap has to have 3 buttonsstates next to each other: Up/Down/Hover
  74. BOOL CToolTipBitmapButton::LoadBitmap(HANDLE hbitmap)
  75. {
  76. m_Bitmap.Attach(hbitmap);
  77. BITMAP bitmapbits;
  78. m_Bitmap.GetBitmap(&bitmapbits);
  79. m_ButtonSize.cy=bitmapbits.bmHeight;
  80. m_ButtonSize.cx=bitmapbits.bmWidth/4; // up, down, focused, disabled
  81. OnSize(0,m_ButtonSize.cx,m_ButtonSize.cy);
  82. return TRUE;
  83. }
  84. void CToolTipBitmapButton::OnMouseMove(UINT nFlags, CPoint point) 
  85. {
  86. // TODO: Add your message handler code here and/or call default
  87. if (m_mouse_hover_style && !m_bTracking)
  88. {
  89. TRACKMOUSEEVENT tme;
  90. tme.cbSize = sizeof(tme);
  91. tme.hwndTrack = m_hWnd;
  92. tme.dwFlags = TME_LEAVE|TME_HOVER;
  93. tme.dwHoverTime = 1;
  94. m_bTracking = _TrackMouseEvent(&tme);
  95. }
  96. baseCToolTipBitmapButton::OnMouseMove(nFlags, point);
  97. }
  98. LRESULT CToolTipBitmapButton::OnMouseHover(WPARAM wparam, LPARAM lparam) 
  99. {
  100. // TODO: Add your message handler code here and/or call default
  101. m_bHover = TRUE;
  102. Invalidate();
  103. return 0;
  104. }
  105. LRESULT CToolTipBitmapButton::OnMouseLeave(WPARAM wparam, LPARAM lparam)
  106. {
  107. m_bTracking = FALSE;
  108. m_bHover=FALSE;
  109. Invalidate();
  110. return 0;
  111. }
  112. void CToolTipBitmapButton::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
  113. {
  114. if (nChar==VK_RETURN || nChar==VK_ESCAPE)
  115. GetParent()->SendMessage(WM_CHAR,nChar,0);
  116. else
  117. {
  118. GetParent()->SendMessage(WM_CHAR,nChar,0);
  119.         baseCToolTipBitmapButton::OnKeyUp(nChar, nRepCnt, nFlags);
  120. }
  121. }