AVIButton.cpp
上传用户:szcysw
上传日期:2013-03-11
资源大小:6752k
文件大小:4k
- // AVIButton.cpp : implementation file
- //
- #include "stdafx.h"
- #include "AVIButonDemo.h"
- #include "AVIButton.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAVIButton
- CAVIButton::CAVIButton()
- {
- this->aviResource=0;
- }
- CAVIButton::~CAVIButton()
- {
- }
- BEGIN_MESSAGE_MAP(CAVIButton, CButton)
- //{{AFX_MSG_MAP(CAVIButton)
- ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- //
- BOOL CAVIButton::Create( LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID )
- {
- BOOL m_bSucess = CButton::Create( lpszCaption,dwStyle,rect, pParentWnd, nID );
- return m_bSucess;
- }
- void CAVIButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
- {
- // TODO: Add your code to draw the specified item
- CRect rect;
- GetClientRect(rect);
- //如果m_AnimateCtrl还没有建立
- if (!::IsWindow(m_AnimateCtrl))
- {
- //将按钮做为m_AnimateCtrl的父窗口
- m_AnimateCtrl.Create(WS_CHILD |WS_VISIBLE,rect,this,0);
- m_AnimateCtrl.Open(this->aviResource);
- m_AnimateCtrl.GetClientRect(rect);
- //将按钮大小设置成和m_AnimateCtrl一致
- SetWindowPos(NULL, -1, -1, rect.Width()+4, rect.Height()+4,
- SWP_NOMOVE|SWP_NOZORDER|SWP_NOREDRAW|SWP_NOACTIVATE);
- rect.OffsetRect(2,2);
- //开始播放
- m_AnimateCtrl.MoveWindow(rect);
- m_AnimateCtrl.Play(0,-1,-1);
- }
-
- //进行按钮本身的绘制
- CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
- UINT nState = lpDrawItemStruct->itemState;
- CRect buttonRect;
- GetClientRect(buttonRect);
- if (IsWindowEnabled())
- nState &= ~ODS_DISABLED;
- else
- nState |= ODS_DISABLED;
- COLORREF upCol,downCol,Frame;
- //边框色
- Frame=RGB(0,0,0);
- BOOL IsPressed = FALSE;
- //如果按钮已经被按下
- if ((nState & ODS_SELECTED) == ODS_SELECTED)
- {
-
- upCol=RGB(0,0,0);
- Frame=RGB(128,128,128);
- downCol=RGB(0,0,0);
- IsPressed = TRUE;
- }
- else
- if ((nState & ODS_DISABLED) == ODS_DISABLED)
- {
- // Disabled// in the future maybe
- }
- else
- { // Normal
- 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.DeleteObject();
- pen2Created = FALSE;
- if (pen1Created = pen1.CreatePen(PS_SOLID, 1, Frame))
- pOldPen = pDC->SelectObject( &pen1 );
- if (IsPressed)
- {
- pDC->MoveTo(1,rect.Height()-2);
- pDC->LineTo(1,1);
- pDC->LineTo(rect.Width()-2,1);
- }
- else
- {
- pDC->MoveTo(rect.Width()-1,0);
- pDC->LineTo(rect.Width()-1,rect.Height()-1);
- pDC->LineTo(-1,rect.Height()-1);
- }
- //恢复DC设置
- if (pen1Created) pen1.DeleteObject();
- pen1Created = FALSE;
- if (pOldPen != NULL)
- pDC->SelectObject( pOldPen );
-
- }
-
- void CAVIButton::OnClicked()
- {
- AfxMessageBox("测试信息");
- }
- //设置AVI的ID值
- void CAVIButton::SetAVIResource(DWORD resource)
- {
- this->aviResource=resource;
-
- }