2Created;
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类和按钮控件的自画功能的结合,本实例实现了动画按钮,如果该类和工具条、状态条等控件结合,还可以实现在上述控件上播放动画视