AVIButton.cpp
上传用户:szcysw
上传日期:2013-03-11
资源大小:6752k
文件大小:4k
源码类别:

界面编程

开发平台:

Visual C++

  1. // AVIButton.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "AVIButonDemo.h"
  5. #include "AVIButton.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CAVIButton
  13. CAVIButton::CAVIButton()
  14. {
  15. this->aviResource=0;
  16. }
  17. CAVIButton::~CAVIButton()
  18. {
  19. }
  20. BEGIN_MESSAGE_MAP(CAVIButton, CButton)
  21. //{{AFX_MSG_MAP(CAVIButton)
  22. ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
  23. //}}AFX_MSG_MAP
  24. END_MESSAGE_MAP()
  25. /////////////////////////////////////////////////////////////////////////////
  26. //
  27. BOOL CAVIButton::Create( LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID )
  28. {
  29.     BOOL m_bSucess = CButton::Create( lpszCaption,dwStyle,rect, pParentWnd, nID );
  30.     return m_bSucess;
  31. }
  32. void CAVIButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  33. {
  34.      // TODO: Add your code to draw the specified item
  35. CRect rect;
  36.     GetClientRect(rect);
  37. //如果m_AnimateCtrl还没有建立
  38. if (!::IsWindow(m_AnimateCtrl))
  39.     {
  40.  //将按钮做为m_AnimateCtrl的父窗口
  41.        m_AnimateCtrl.Create(WS_CHILD |WS_VISIBLE,rect,this,0);
  42.        m_AnimateCtrl.Open(this->aviResource);
  43.        m_AnimateCtrl.GetClientRect(rect);
  44. //将按钮大小设置成和m_AnimateCtrl一致
  45.     SetWindowPos(NULL, -1, -1, rect.Width()+4, rect.Height()+4,
  46. SWP_NOMOVE|SWP_NOZORDER|SWP_NOREDRAW|SWP_NOACTIVATE);
  47.        rect.OffsetRect(2,2);
  48.         //开始播放
  49.        m_AnimateCtrl.MoveWindow(rect); 
  50.        m_AnimateCtrl.Play(0,-1,-1);
  51.     }
  52. //进行按钮本身的绘制
  53. CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  54. UINT nState = lpDrawItemStruct->itemState;
  55.     CRect buttonRect;
  56.     GetClientRect(buttonRect);
  57.    if (IsWindowEnabled())
  58. nState &= ~ODS_DISABLED;
  59. else
  60. nState |= ODS_DISABLED;
  61.     COLORREF upCol,downCol,Frame;
  62.     //边框色
  63. Frame=RGB(0,0,0);
  64.     BOOL IsPressed = FALSE;
  65. //如果按钮已经被按下
  66. if ((nState & ODS_SELECTED) == ODS_SELECTED)
  67. {
  68.         upCol=RGB(0,0,0);
  69.         Frame=RGB(128,128,128);
  70.         downCol=RGB(0,0,0);
  71.         IsPressed = TRUE;
  72.     }
  73. else 
  74. if ((nState & ODS_DISABLED) == ODS_DISABLED)
  75. {
  76. // Disabled// in the future maybe
  77. }
  78. else
  79.     {   // Normal
  80.         upCol=RGB(255,255,255);
  81.         downCol=RGB(128,128,128);
  82. }
  83. CPen* pOldPen = NULL;
  84. BOOL pen1Created;
  85.     CPen pen1;
  86.     BOOL pen2Created;
  87.     CPen pen2;
  88.     if (pen1Created = pen1.CreatePen(PS_SOLID, 1, upCol))  
  89.         pOldPen = pDC->SelectObject( &pen1 );
  90. //分别绘制按钮边框(立体)
  91.     pDC->MoveTo(1,rect.Height()-1);
  92.     pDC->LineTo(1,1);                                                             
  93.     pDC->LineTo(rect.Width()-1,1);
  94.     pDC->MoveTo(0,rect.Height()-1);
  95.     pDC->LineTo(0,0);                                                             
  96.     pDC->LineTo(rect.Width()-1,0);
  97.     if (pen2Created = pen2.CreatePen(PS_SOLID, 1, downCol))  
  98.     {
  99.         pDC->SelectObject( &pen2 );
  100.     }
  101.     
  102.     if (pen1Created) pen1.DeleteObject();
  103.         pen1Created = FALSE;
  104.     pDC->MoveTo(rect.Width()-1,0);
  105.     pDC->LineTo(rect.Width()-1,rect.Height()-1);
  106.     pDC->LineTo(0,rect.Height()-1);
  107.     pDC->MoveTo(rect.Width()-2,1);
  108.     pDC->LineTo(rect.Width()-2,rect.Height()-2);
  109.     pDC->LineTo(0,rect.Height()-2);
  110.     if (pen2Created) pen2.DeleteObject();
  111.         pen2Created = FALSE;
  112.     if (pen1Created = pen1.CreatePen(PS_SOLID, 1, Frame)) 
  113.         pOldPen = pDC->SelectObject( &pen1 );
  114.     if (IsPressed)
  115.     {
  116.         pDC->MoveTo(1,rect.Height()-2);
  117.         pDC->LineTo(1,1);                                                             
  118.         pDC->LineTo(rect.Width()-2,1);
  119.     }
  120.     else
  121.     {
  122.         pDC->MoveTo(rect.Width()-1,0);
  123.         pDC->LineTo(rect.Width()-1,rect.Height()-1);
  124.         pDC->LineTo(-1,rect.Height()-1);
  125.     }
  126.     //恢复DC设置
  127. if (pen1Created) pen1.DeleteObject();
  128.         pen1Created = FALSE;
  129.     if (pOldPen != NULL) 
  130. pDC->SelectObject( pOldPen );
  131.      
  132. }
  133.  
  134. void CAVIButton::OnClicked() 
  135. {
  136. AfxMessageBox("测试信息");
  137. }
  138. //设置AVI的ID值
  139. void CAVIButton::SetAVIResource(DWORD resource)
  140. {
  141. this->aviResource=resource;
  142.  
  143. }