分类: C/C++
2008-08-04 09:31:00
class CAnimateIcon { protected: //创建一个列表变量,用于保存图像列 CImageList m_imgList; //当前图像序号 int m_iImageCounter; //图像总数 int m_iMaxNumOfImages; HICON hIcon; HICON hPrevIcon; public: CAnimateIcon(); ~CAnimateIcon(); bool SetImageList(int IDOfImageListResource,int numberOfImages,COLORREF transparentColor); bool ShowNextImage(); };b.AnimateIcon.cpp如下:
#include "StdAfx.h" #include "AnimateIcon.h" CAnimateIcon::CAnimateIcon() { m_iImageCounter=-1; m_iMaxNumOfImages=-100; m_imgList.m_hImageList=NULL; } CAnimateIcon::~CAnimateIcon() { DestroyIcon(hPrevIcon); } //初始化图像列表 bool CAnimateIcon::SetImageList(int IDOfImageListResource, int numberOfImages,COLORREF transparentColor) { if(numberOfImages<=0) return false; m_iMaxNumOfImages=numberOfImages; VERIFY(m_imgList.Create(IDOfImageListResource,16,1,transparentColor)); return true; } bool CAnimateIcon::ShowNextImage() { if(m_imgList.m_hImageList==NULL) return false; m_iImageCounter ; if(m_iImageCounter>=m_iMaxNumOfImages) m_iImageCounter=0; hIcon=m_imgList.ExtractIcon(m_iImageCounter); HICON hPrevIcon=(HICON)AfxGetMainWnd()->SetIcon(hIcon,false); DestroyIcon(hPrevIcon); return true; }
void CAnimateDlgIconDlg::OnTimer(UINT nIDEvent) { //调用成员函数显示图片 m_animIcon.ShowNextImage(); CDialog::OnTimer(nIDEvent); }在主对话框中的InitInstance中
//初始化图像列表 m_animIcon.SetImageList(IDB_BITMAP_ANIM,6,RGB(0,0,0)); //设置图像更换时间为500毫秒 m_timerID=this->SetTimer(99,500,NULL);在VC6.0+WIN2000 SP3编译通过!