ANIBUTTON.CPP
上传用户:lvjun8202
上传日期:2013-04-30
资源大小:797k
文件大小:7k
源码类别:

SNMP编程

开发平台:

C/C++

  1.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               /*****************************************************************************
  2. *                                                                             
  3. *   AniButton.cpp                                                            
  4. *                                                                             
  5. *   Electrical Engineering Faculty - Software Lab                             
  6. *   Spring semester 1998                                                      
  7. *                                                                             
  8. *   Tanks game                                                                
  9. *                                                                             
  10. *   Module description: Implements the animated buttons used in the dialogs.
  11. *                       
  12. *                                                                             
  13. *   Authors: Eran Yariv - 28484475                                           
  14. *            Moshe Zur  - 24070856                                           
  15. *                                                                            
  16. *                                                                            
  17. *   Date: 23/09/98                                                           
  18. *                                                                            
  19. ******************************************************************************/
  20. ////////////////////////////////////////////////////////////////
  21. // CAniButton Animated button
  22. // (c) Oscar  oscarko@cyberus.ca
  23. // 
  24. #include "stdafx.h"
  25. #include "AniButton.h"
  26. #ifdef _DEBUG
  27. #define new DEBUG_NEW
  28. #undef THIS_FILE
  29. static char THIS_FILE[] = __FILE__;
  30. #endif
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CAniButton
  33. CAniButton::CAniButton()
  34. {
  35.     m_nAniID = 0;
  36.     m_bPlaying = FALSE;
  37. }
  38. CAniButton::~CAniButton()
  39. {
  40. }
  41. BEGIN_MESSAGE_MAP(CAniButton, CButton)
  42.     //{{AFX_MSG_MAP(CAniButton)
  43.     ON_WM_MOUSEMOVE()
  44. //}}AFX_MSG_MAP
  45. END_MESSAGE_MAP()
  46. BOOL CAniButton::Create( LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID )
  47. {
  48.     BOOL m_bSucess = CButton::Create( lpszCaption,dwStyle,rect, pParentWnd, nID );
  49.     return m_bSucess;
  50. }
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CAniButton message handlers
  53. void CAniButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  54. {
  55.     CRect rect;
  56.     GetClientRect(rect);
  57.     if (!::IsWindow(m_AnimateCtrl))
  58.     {
  59.        m_AnimateCtrl.Create(WS_CHILD |WS_VISIBLE,rect,this,0);
  60.        m_AnimateCtrl.Open(m_nAniID);
  61.        m_AnimateCtrl.GetClientRect(rect);
  62.        VERIFY(SetWindowPos(NULL, -1, -1, rect.Width()+4, rect.Height()+4,
  63.         SWP_NOMOVE|SWP_NOZORDER|SWP_NOREDRAW|SWP_NOACTIVATE));
  64.        rect.OffsetRect(2,2);
  65.        
  66.        m_AnimateCtrl.MoveWindow(rect); 
  67.        m_AnimateCtrl.Play(0,-1,1);
  68.     }
  69.     CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  70.     UINT nState = lpDrawItemStruct->itemState;
  71.     CRect buttonRect;
  72.     GetClientRect(buttonRect);
  73.     if (IsWindowEnabled())
  74.         nState &= ~ODS_DISABLED;
  75.     else
  76.         nState |= ODS_DISABLED;
  77.     DrawButton(pDC, nState, buttonRect);
  78. }
  79. void CAniButton::LoadAVI(UINT nAniID)
  80. {
  81.     m_nAniID =nAniID;
  82. }
  83. //Draw the button borders
  84. void CAniButton::DrawButton(CDC* pDC, UINT nState, CRect rect)
  85. {
  86. return;
  87.     COLORREF upCol,downCol,edgeCol;
  88.     BOOL bRevers = FALSE;
  89.     upCol = downCol = edgeCol = RGB(0,0,0);
  90.     if ((nState & ODS_SELECTED) == ODS_SELECTED)
  91.     {
  92.         //  Pressed 
  93.         upCol=  RGB(0,60,255);
  94.         downCol=RGB(0,120,255);
  95.         edgeCol=RGB(0,60,0);
  96.         bRevers = TRUE;
  97.     }
  98.     else if ((nState & ODS_FOCUS) == ODS_FOCUS)
  99.     {
  100.         // In focus
  101.         upCol=  RGB(0,20,80);
  102.         downCol=RGB(0,40,80);
  103.         edgeCol=RGB(0,20,0);
  104.     }
  105.     else if ((nState & ODS_DISABLED) == ODS_DISABLED)
  106.     {
  107.         // Disabled -  in the future maybe
  108.     }
  109.     else
  110.     {   // Normal
  111.         upCol=  RGB(0,0,0);
  112.         downCol=RGB (0,0,0);
  113.         edgeCol=RGB(0,0,0);
  114.     }
  115.     CPen* pOldPen = NULL;
  116.     BOOL pen1Created;
  117.     CPen pen1;
  118.     BOOL pen2Created;
  119.     CPen pen2;
  120.     pen1Created = pen1.CreatePen(PS_DOT, 1, upCol);
  121.     if (pen1Created)  
  122.         pOldPen = pDC->SelectObject( &pen1 );
  123.     pDC->MoveTo(1,rect.Height()-1);
  124.     pDC->LineTo(1,1);                                                             
  125.     pDC->LineTo(rect.Width()-1,1);
  126.     pDC->MoveTo(0,rect.Height()-1);
  127.     pDC->LineTo(0,0);                                                             
  128.     pDC->LineTo(rect.Width()-1,0);
  129.     pen2Created = pen2.CreatePen(PS_DASH, 1, downCol);
  130.     if (pen2Created)  
  131.     {
  132.         pDC->SelectObject( &pen2 );
  133.     }
  134.     
  135.     if (pen1Created) pen1.DeleteObject();
  136.         pen1Created = FALSE;
  137.     pDC->MoveTo(rect.Width()-1,0);
  138.     pDC->LineTo(rect.Width()-1,rect.Height()-1);
  139.     pDC->LineTo(0,rect.Height()-1);
  140.     pDC->MoveTo(rect.Width()-2,1);
  141.     pDC->LineTo(rect.Width()-2,rect.Height()-2);
  142.     pDC->LineTo(0,rect.Height()-2);
  143.     if (pen2Created) pen2.DeleteObject();
  144.         pen2Created = FALSE;
  145.     pen1Created = pen1.CreatePen(PS_SOLID, 1, edgeCol);
  146.     if (pen1Created) 
  147.         pOldPen = pDC->SelectObject( &pen1 );
  148.     if (bRevers)
  149.     {
  150.         pDC->MoveTo(1,rect.Height()-2);
  151.         pDC->LineTo(1,1);                                                             
  152.         pDC->LineTo(rect.Width()-2,1);
  153.     }
  154.     else
  155.     {
  156.         pDC->MoveTo(rect.Width()-1,0);
  157.         pDC->LineTo(rect.Width()-1,rect.Height()-1);
  158.         pDC->LineTo(-1,rect.Height()-1);
  159.     }
  160.     if (pen1Created) pen1.DeleteObject();
  161.         pen1Created = FALSE;
  162.     if (pOldPen != NULL) pDC->SelectObject( pOldPen );
  163. }
  164. void CAniButton::OnMouseMove(UINT nFlags, CPoint point) 
  165. {
  166.     ClientToScreen(&point);
  167.     CRect rcWindow;
  168.     GetWindowRect(rcWindow);
  169.     BOOL bNewMouseOverButton = rcWindow.PtInRect(point);
  170.     unsigned long nROnly = ES_READONLY;
  171.     BOOL bTest = (GetStyle() &  nROnly) != nROnly;
  172.     if (bNewMouseOverButton && IsWindowEnabled() && bTest)
  173.     {
  174.         if (::IsWindow(m_AnimateCtrl) && !m_bPlaying)
  175.         {
  176.             m_AnimateCtrl.Play(0,UINT(-1),1);
  177.             m_bPlaying = TRUE;
  178.             SetCapture();
  179.         }
  180.     }
  181.     else
  182.     {
  183.         m_bPlaying = FALSE;
  184.         ReleaseCapture();
  185.     }
  186.     CButton::OnMouseMove(nFlags, point);
  187. }