Chinaunix首页 | 论坛 | 博客
  • 博客访问: 103653198
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类: C/C++

2008-04-15 21:25:15

   来源:赛迪论坛    作者:three man

三、实现代码

/////////////////////////////// AviButton.h : header file
#if !defined(AFX_AVIBUTTON_H__5E20D4EF_864E_11D7_886E_F16C81CD642B__INCLUDED_)
#define AFX_AVIBUTTON_H__5E20D4EF_864E_11D7_886E_F16C81CD642B__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CAviButton : public CButton
{
 // Construction
 public:
  CAviButton();
 public:
  UINT m_nAviID;
  CAnimateCtrl AnimateCtrl;
  BOOL bPlaying;
  void LoadAvi(UINT nAviID);
  void DrawButton(CDC* pDC, UINT nState, CRect rect);
  // Overrides
  // ClassWizard generated virtual function overrides
  //{{AFX_VIRTUAL(CAviButton)
   public:
    virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
    virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD
dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID,
CCreateContext* pContext = NULL);
  //}}AFX_VIRTUAL
  // Implementation
 public:
  virtual ~CAviButton();
  // Generated message map functions
 protected:
  //{{AFX_MSG(CAviButton)
   afx_msg void OnMouseMove(UINT nFlags, CPoint point);
  //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};
#endif

////////////////////////////CAviButton.CPP文件;
#include "stdafx.h"
#include "TestAviButton.h"
#include "AviButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CAviButton::CAviButton()
{
 m_nAviID = 0;
 bPlaying = FALSE;
}

CAviButton::~CAviButton()
{}
void CAviButton::LoadAvi(UINT nAviID)
{
 m_nAviID =nAviID;
}

BEGIN_MESSAGE_MAP(CAviButton, CButton)
 //{{AFX_MSG_MAP(CAviButton)
  ON_WM_MOUSEMOVE()
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

///////////////////// CAviButton message handlers
void CAviButton::DrawButton(CDC *pDC, UINT nState, CRect rect)
{
 COLORREF upCol,downCol,edgeCol;
 edgeCol=RGB(0,0,0); //设置按钮边缘的初始化颜色;
 if ((nState & ODS_SELECTED) == ODS_SELECTED)
 {
  //设置按钮被按下时按钮的颜色
  upCol=RGB(0,0,0);
  edgeCol=RGB(128,128,128);
  downCol=RGB(0,0,0);
 }
 else
 {
  //设置按钮正常时按钮的颜色
  upCol=RGB(255,255,255);
  downCol=RGB(128,128,128);
 }
 CPen* pOldPen = NULL;
 BOOL pen1Created;
 CPen pen1;
 BOOL pen2Created;
 CPen pen2;
 if (pen1Created = pen1.CreatePen(PS_SOLID, 1, upCol))
  pOldPen = pDC->SelectObject( &pen1 );
  //画左上边缘
 pDC->MoveTo(1,rect.Height()-1);
 pDC->LineTo(1,1);
 pDC->LineTo(rect.Width()-1,1);
 pDC->MoveTo(0,rect.Height()-1);
 pDC->LineTo(0,0);
 pDC->LineTo(rect.Width()-1,0);
 if (pen2Created = pen2.CreatePen(PS_SOLID, 1, downCol))
  pDC->SelectObject( &pen2 );
 if (pen1Created)
 {
  pen1.DeleteObject();
  pen1Created = FALSE;
 }
 //画右下边缘
 pDC->MoveTo(rect.Width()-1,0);
 pDC->LineTo(rect.Width()-1,rect.Height()-1);
 pDC->LineTo(0,rect.Height()-1);
 pDC->MoveTo(rect.Width()-2,1);
 pDC->LineTo(rect.Width()-2,rect.Height()-2);
 pDC->LineTo(0,rect.Height()-2);
 if (pen2Created)//删除"pen2"画笔对象
 {
  pen2.DeleteObject();
  pen2Created = FALSE;
 }
 if (pen1Created = pen1.CreatePen(PS_SOLID, 1, edgeCol))
  pOldPen = pDC->SelectObject( &pen1 );
 if (pen1Created)
 {
  pen1.DeleteObject();
  pen1Created = FALSE;
 }
 if (pOldPen != NULL)
  pDC->SelectObject( pOldPen );
}

void CAviButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
 // TODO: Add your code to draw the specified item
 CRect rect;
 GetClientRect(rect);
 if (!::IsWindow(AnimateCtrl))
 {
  //在按钮上生成一个动画控件
  AnimateCtrl.Create(WS_CHILD |WS_VISIBLE,rect,this,0);
  //打开avi文件并显示第一帧
  AnimateCtrl.Open(m_nAviID);
  AnimateCtrl.GetClientRect(rect);
 }
 CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
 UINT nState = lpDrawItemStruct->itemState;
 CRect buttonRect;
 GetClientRect(buttonRect);
 //绘制按钮
 DrawButton(pDC, nState, buttonRect);
}

BOOL CAviButton::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD
dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
 // TODO: Add your specialized code here and/or call the base class
 return CWnd::Create(lpszClassName, lpszWindowName, dwStyle,
 rect, pParentWnd, nID, pContext);
}

void CAviButton::OnMouseMove(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 ClientToScreen(&point);
 CRect rcWindow;
 GetWindowRect(rcWindow);
 //判断鼠标是否经过按钮
 BOOL bNewMouseOverButton = rcWindow.PtInRect(point);
 if (bNewMouseOverButton && IsWindowEnabled() )
 {
  if (::IsWindow(AnimateCtrl) && !bPlaying)
  {
   AnimateCtrl.Play(0,-1,1);
   bPlaying = TRUE;
   SetCapture();
  }
 }
 else
 {
  bPlaying = FALSE;
  ReleaseCapture();
 }
 CButton::OnMouseMove(nFlags, point);
}
/////////////////////////////////
BOOL CTestAviButtonDlg::OnInitDialog()
{
 CDialog::OnInitDialog();
 …………………//此处代码省略;
 m_AviButton.LoadAvi(IDR_AVI);
 return TRUE; // return TRUE unless you set the focus to a control
}

  四、小结

  通过CAnimateCtrl类和按钮控件的自画功能的结合,本实例实现了动画按钮,如果该类和工具条、状态条等控件结合,还可以实现在上述控件上播放动画视频流的效果。

阅读(222) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~