AniButton.cpp
上传用户:royluo
上传日期:2007-01-05
资源大小:1584k
文件大小:6k
源码类别:

游戏

开发平台:

Visual 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.     COLORREF upCol,downCol,edgeCol;
  87.     BOOL bRevers = FALSE;
  88.     upCol = downCol = edgeCol = RGB(0,0,0);
  89.     if ((nState & ODS_SELECTED) == ODS_SELECTED)
  90.     {
  91.         //  Pressed 
  92.         upCol=  RGB(0,60,255);
  93.         downCol=RGB(0,120,255);
  94.         edgeCol=RGB(0,60,0);
  95.         bRevers = TRUE;
  96.     }
  97.     else if ((nState & ODS_FOCUS) == ODS_FOCUS)
  98.     {
  99.         // In focus
  100.         upCol=  RGB(0,20,80);
  101.         downCol=RGB(0,40,80);
  102.         edgeCol=RGB(0,20,0);
  103.     }
  104.     else if ((nState & ODS_DISABLED) == ODS_DISABLED)
  105.     {
  106.         // Disabled -  in the future maybe
  107.     }
  108.     else
  109.     {   // Normal
  110.         upCol=  RGB(0,0,0);
  111.         downCol=RGB (0,0,0);
  112.         edgeCol=RGB(0,0,0);
  113.     }
  114.     CPen* pOldPen = NULL;
  115.     BOOL pen1Created;
  116.     CPen pen1;
  117.     BOOL pen2Created;
  118.     CPen pen2;
  119.     pen1Created = pen1.CreatePen(PS_SOLID, 1, upCol);
  120.     if (pen1Created)  
  121.         pOldPen = pDC->SelectObject( &pen1 );
  122.     pDC->MoveTo(1,rect.Height()-1);
  123.     pDC->LineTo(1,1);                                                             
  124.     pDC->LineTo(rect.Width()-1,1);
  125.     pDC->MoveTo(0,rect.Height()-1);
  126.     pDC->LineTo(0,0);                                                             
  127.     pDC->LineTo(rect.Width()-1,0);
  128.     pen2Created = pen2.CreatePen(PS_SOLID, 1, downCol);
  129.     if (pen2Created)  
  130.     {
  131.         pDC->SelectObject( &pen2 );
  132.     }
  133.     
  134.     if (pen1Created) pen1.DeleteObject();
  135.         pen1Created = FALSE;
  136.     pDC->MoveTo(rect.Width()-1,0);
  137.     pDC->LineTo(rect.Width()-1,rect.Height()-1);
  138.     pDC->LineTo(0,rect.Height()-1);
  139.     pDC->MoveTo(rect.Width()-2,1);
  140.     pDC->LineTo(rect.Width()-2,rect.Height()-2);
  141.     pDC->LineTo(0,rect.Height()-2);
  142.     if (pen2Created) pen2.DeleteObject();
  143.         pen2Created = FALSE;
  144.     pen1Created = pen1.CreatePen(PS_SOLID, 1, edgeCol);
  145.     if (pen1Created) 
  146.         pOldPen = pDC->SelectObject( &pen1 );
  147.     if (bRevers)
  148.     {
  149.         pDC->MoveTo(1,rect.Height()-2);
  150.         pDC->LineTo(1,1);                                                             
  151.         pDC->LineTo(rect.Width()-2,1);
  152.     }
  153.     else
  154.     {
  155.         pDC->MoveTo(rect.Width()-1,0);
  156.         pDC->LineTo(rect.Width()-1,rect.Height()-1);
  157.         pDC->LineTo(-1,rect.Height()-1);
  158.     }
  159.     if (pen1Created) pen1.DeleteObject();
  160.         pen1Created = FALSE;
  161.     if (pOldPen != NULL) pDC->SelectObject( pOldPen );
  162. }
  163. void CAniButton::OnMouseMove(UINT nFlags, CPoint point) 
  164. {
  165.     ClientToScreen(&point);
  166.     CRect rcWindow;
  167.     GetWindowRect(rcWindow);
  168.     BOOL bNewMouseOverButton = rcWindow.PtInRect(point);
  169.     unsigned long nROnly = ES_READONLY;
  170.     BOOL bTest = (GetStyle() &  nROnly) != nROnly;
  171.     if (bNewMouseOverButton && IsWindowEnabled() && bTest)
  172.     {
  173.         if (::IsWindow(m_AnimateCtrl) && !m_bPlaying)
  174.         {
  175.             m_AnimateCtrl.Play(0,UINT(-1),1);
  176.             m_bPlaying = TRUE;
  177.             SetCapture();
  178.         }
  179.     }
  180.     else
  181.     {
  182.         m_bPlaying = FALSE;
  183.         ReleaseCapture();
  184.     }
  185.     CButton::OnMouseMove(nFlags, point);
  186. }