VC++制作播放AVI视频流的动画按钮
2007-10-04 20:11:30 来源:WEB开发网核心提示:二、编程步骤1、启动Visual C++6.0,生成一个基于对话框的应用程序,VC++制作播放AVI视频流的动画按钮(2),取其项目名为 TestAviButton, 然后按下 Finish 按钮来完成工程的创建;2、使用Class Wizard菜单命令以CButton 为基类创建CAviButton 类,生成类的头文
二、编程步骤
1、启动Visual C++6.0,生成一个基于对话框的应用程序,取其项目名为 TestAviButton, 然后按下 Finish 按钮来完成工程的创建;
2、使用Class Wizard菜单命令以CButton 为基类创建CAviButton 类,生成类的头文件 AviButton.h 和实现文件 AviButton.cpp,同时在类中重载 Create()、DrawItem()函数和 WM_MOUSEMOVE的消息映射;
3、打开工程中ID值为IDD_TESTAVIBUTTON_DIALOG 的对话框进行编辑,该对话框的提示文本为"将鼠标移至按钮上:"删除"取消"按钮,将"确定"按钮的属性 Styles 改为 OwnerDraw ,并去掉其提示文本 "确定";另外执行 Insert | Resource… 命令,在弹出的对话框中按下 Custom… 按钮,然后输入"AVI",向程序中添加"AVI"格式的资源,然后选择AVI类型的文件向项目中添加"AVI"格式的资源IDR_AVI;
4、添加代码,编译运行程序,此时每当我们的鼠标经过按钮时,一个漂亮的AVI按钮就产生了。
三、实现代码
/////////////////////////////// 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类和按钮控件的自画功能的结合,本实例实现了动画按钮,如果该类和工具条、状态条等控件结合,还可以实现在上述控件上播放动画视频流的效果。
更多精彩
赞助商链接