⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 animatectrl.cpp

📁 animate gdiplus animete
💻 CPP
字号:
#include "stdafx.h"
#include "animatectrl.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNCREATE(CAnimate_Ctrl,CStatic)

CAnimate_Ctrl::CAnimate_Ctrl()
{
	m_pictureStyle = PSTYLE_CENTER;
	m_bAnimate = false;
	m_nFMS = 300; //approx 3 pics per second
	m_backCol = RGB(255,255,255);;
}

CAnimate_Ctrl::~CAnimate_Ctrl()
{
	POSITION pos = m_iList.GetHeadPosition();
	for (int i=0;i<m_iList.GetCount();i++) {
		Image* pImage = m_iList.GetNext(pos);
		if(pImage!=NULL) {
			delete pImage;
			pImage = NULL;
		}
	}
}


BEGIN_MESSAGE_MAP(CAnimate_Ctrl, CStatic)
	//{{AFX_MSG_MAP(CAnimate_Ctrl)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CAnimate_Ctrl::addFrame(int nPos, const CString& strFileName)
{
	ASSERT(nPos >= -2 || nPos < m_iList.GetCount());
	
	wchar_t* wstrFName = new wchar_t[strFileName.GetLength()+1];
	ascii2unicode(strFileName, wstrFName);
	ASSERT(wstrFName!=NULL);
	Image* pImage = Image::FromFile(wstrFName);	
	delete [] wstrFName; wstrFName = NULL;
	
	if(nPos==LIST_HEAD)
		m_iList.AddHead(pImage);
	else if(nPos == LIST_TAIL)
		m_iList.AddTail(pImage);
	else {
		POSITION pos = m_iList.GetHeadPosition();
		for (int i=0;i<m_iList.GetCount();i++, m_iList.GetNext(pos))
			if(i==nPos)
				break;

		m_iList.InsertBefore(pos, pImage);
	}
}

void CAnimate_Ctrl::addFrame(int nPos, HBITMAP hbm)
{
	ASSERT(nPos >= -2 || nPos < m_iList.GetCount());

	CPalette pal;
	pal.CreateHalftonePalette(GetDC());
	Image* pImage = Bitmap::FromHBITMAP(hbm, (HPALETTE)pal);

	if(nPos==LIST_HEAD)
		m_iList.AddHead(pImage);
	else if(nPos == LIST_TAIL)
		m_iList.AddTail(pImage);
	else {
		POSITION pos = m_iList.GetHeadPosition();
		for (int i=0;i<m_iList.GetCount();i++, m_iList.GetNext(pos))
			if(i==nPos)
				break;

		m_iList.InsertBefore(pos, pImage);
	}
}

void CAnimate_Ctrl::removeFrame(int nPos)
{
	ASSERT(nPos >= -2 || nPos < m_iList.GetCount());

	Image* pImage;
	if(nPos==LIST_HEAD) {
		pImage = m_iList.GetHead();
		delete pImage;
		m_iList.RemoveHead();
	}
	else if(nPos == LIST_TAIL) {
		pImage = m_iList.GetTail();
		delete pImage;
		m_iList.RemoveTail();		
	}
	else {
		POSITION pos = m_iList.GetHeadPosition();
		for (int i=0;i<m_iList.GetCount();i++, m_iList.GetNext(pos))
			if(i==nPos)
				break;
		pImage = m_iList.GetAt(pos);
		delete pImage;
		m_iList.RemoveAt(pos);
	}
}

void CAnimate_Ctrl::ascii2unicode(LPCTSTR strin, wchar_t* strout)
{
	int len = strlen(strin);	
	for(int i=0; i<len; i++)
		strout[i]=(wchar_t)strin[i];
	strout[i] = 0;
}

UINT AnimateThreadProc( LPVOID pParam )
{
	CAnimate_Ctrl::transfer* pTrans = (CAnimate_Ctrl::transfer*)pParam;
	CWnd wnd;
	wnd.Attach(pTrans->hwnd);
	
	POSITION posCurrentFrame = pTrans->piList->GetHeadPosition();

	UINT nTicks = GetTickCount();

	CPaintDC dc(&wnd); // device context for painting

	CRect rect;
	wnd.GetWindowRect(rect);

	int top, left;
	top = left = 0;

	CRect rectClient;
	wnd.GetClientRect(rectClient);
	
	while(*(pTrans->bAnim)) {
		if(GetTickCount() - nTicks > pTrans->FMS) {
			Image* pImage = pTrans->piList->GetNext(posCurrentFrame);
			if(posCurrentFrame == pTrans->piList->GetTailPosition())
				posCurrentFrame = pTrans->piList->GetHeadPosition();
			if(pImage == NULL || pImage->GetLastStatus() != Ok)
				continue;
			
			dc.FillSolidRect(rectClient, pTrans->backcol);

			switch(pTrans->pstyle) {
				case PSTYLE_NORMAL:	
					Graphics(dc.GetSafeHdc()).DrawImage(pImage,left,top);
					break;
				case PSTYLE_CENTER: 
					top = rect.Height()/2 - (pImage->GetHeight())/2;
					left = rect.Width()/2 - (pImage->GetWidth())/2;
					Graphics(dc.GetSafeHdc()).DrawImage(pImage,left,top); 
					break;
				case PSTYLE_STRETCH:
					Graphics(dc.GetSafeHdc()).DrawImage(pImage,top,left,rect.Width(),rect.Height());
					break;
				default:
					ASSERT(false);
			}

			wnd.Invalidate();
			nTicks = GetTickCount();
		}
	}
	wnd.Detach();

	return 0;
}


void CAnimate_Ctrl::startAnim()
{
	if(m_bAnimate) return;
	m_bAnimate = true;
	
	//ModifyStyleEx(0, WS_EX_TRANSPARENT, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE);

	m_transfer.bAnim = &m_bAnimate;
	m_transfer.FMS = m_nFMS;
	m_transfer.piList = &m_iList;
	m_transfer.pstyle = m_pictureStyle;
	m_transfer.hwnd = m_hWnd;
	m_transfer.backcol = m_backCol;
	
	AfxBeginThread(AnimateThreadProc, &m_transfer);
}

void CAnimate_Ctrl::stopAnim()
{
	m_bAnimate = false;
}

void CAnimate_Ctrl::OnPaint() 
{
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -