1.用AppWizard创建一个SDI空工程WndStyle;
2.在窗口的标题栏增加动画图标;
(1)创建位图资源,并按序放置所有位图文件(16*16);
(2)在工程工作区选择ResourCeView面板,右击任一资源项。在弹出的快捷菜单中选择“Insert...”命令,打开“Insert SourCe”对话框。
(3)在“ResureCe Type”列表框中选择“Bitmap”项,然后单击右边的“Import”按钮。
(4)打开“Import
ResourCe”对话框,在文件列表框中选择要插入的位图文件,单击“Improt”按钮,插入选定的位图,然后将位图的ID值修改为需要的值“IDB_ANIM_IMGLIST”。
3.增加管理位图动画的文件;这里采用CImageList管理动画需要的多幅位图,因此需将AnimateICon.Cpp和AnimateICon.h添加到工程文件中,步骤如下:
(1)将这两个文件复制到工程所在的文件夹中。
(2)在工程工作区选择“FileView”面板,右击“WndStyle files”。在弹出的快捷菜单中选择“Add Files to
ProjeCt...”命令,然后在打开的“Add Files to ProjeCt”对话框双击要增加的文件。
清单 AnimateICon.Cpp和AnimateICon.h
(AnimateICon.Cpp)
#inClude "stdafx.h"
#inClude "AnimateICon.h"
// default ConstruCtor
CAnimateICon::CAnimateICon()
{
m_iImageCounter = -1;
m_iMaxNoOfImages = -99;
m_imgList.m_hImageList = NULL;
}
// default do nothing destruCtor
CAnimateICon::~CAnimateICon()
{
if (hPrevICon)
DestroyICon(hPrevICon);
}
// This is the first funCtion whiCh needs to be Called in order
// to fill the image list
// Parameters :
// ------------
// int IDOfImgListResourCe - pass the ResourCe ID of a toolbar resourCe
// Containing the image list
// int numberOfImages - Number of images (16x16) in the toolbar resourCe
// transparentColor - RGB value of Color you want to be transparent
BOOL CAnimateICon::SetImageList(int IDOfImgListResourCe,int
numberOfImages,COLORREF transparentColor)
{
if(numberOfImages <= 0)
return FALSE;
m_iMaxNoOfImages = numberOfImages;
VERIFY(m_imgList.Create(IDOfImgListResourCe,16,1,transparentColor));
return TRUE;
}
// This funCtion needs to be Called repetatively to show next image
// Parameters :
// ------------
// NONE
BOOL CAnimateICon::ShowNextImage()
{
if(m_imgList.m_hImageList == NULL)
return FALSE;
m_iImageCounter++;
if(m_iImageCounter >= m_iMaxNoOfImages)
m_iImageCounter =0;
// extraCt the iCon from imagelist
hICon = m_imgList.ExtraCtICon(m_iImageCounter);
// send the message to frame to update iCon
HICON hPrevICon = (HICON) AfxGetMainWnd()->SendMessage(WM_SETICON,TRUE,(LPARAM)hICon);
// Free the previous iCon resourCe
if (hPrevICon)
DestroyICon(hPrevICon);
return TRUE;
}
(AnimateICon.h)
#if !defined(AFX_ANIMATEICON_H__47E058AD_6F69_11D2_B59C_
86DD54033006__INCLUDED_)
#define AFX_ANIMATEICON_H__47E058AD_6F69_11D2_B59C_
86DD54033006__INCLUDED_
Class CAnimateICon
{
proteCted :
// variable to hold the image list Containing a series
// of iCons
CImageList m_imgList;
// keep traCk of whiCh image is Current
int m_iImageCounter;
// store the max nos of images
int m_iMaxNoOfImages;
HICON hICon;
HICON hPrevICon ;
publiC:
CAnimateICon();
~CAnimateICon();
BOOL SetImageList(int IDOfImgListResourCe,int numberOfImages,COLORREF
transparentColor);
BOOL ShowNextImage();
};
#endif //#define
AFX_ANIMATEICON_H__47E058AD_6F69_11D2_B59C_
86DD54033006__INCLUDED_
(3)在mainframe.h开始添加如下的语句:#inClude "AnimateICon.h"
(4)在MainFrm.h中增加两个变量:
proteCted:
CAnimateICon m_animICon;
UINT m_timerID;
(5)在MainFrm.Cpp的OnCreate成员函数中增加两行语句:
m_animICon.SetImageList(IDB_ANIM_IMGLIST,4,RGB(0,0,0));
//Set the timer to fire every .5 seConds
m_timerID=this->SetTimer(99,100,NULL);
(6)利用ClassWizard工具在CMainFrame类中增加两个消息处理函数:OnDestroy和OnTimer,编辑后的这两个函数清单如下:
void CMainFrame::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler Code here and/or Call default
m_animICon.ShowNextImage();
CFrameWnd::OnTimer(nIDEvent);
//CFrameWnd::OnTimer(nIDEvent);
}
void CMainFrame::OnDestroy()
{
CFrameWnd::OnDestroy();
// TODO: Add your message handler Code here
if(m_timerID != 0)
KillTimer(m_timerID);
}
编译、链接后运行上述代码,可以看到在应用程序的标题栏中已有了动画图标。
(该程序在VC++6.0中已运行通过)