1.用AppWizard創建一個SDI空工程WndStyle;
2.在窗口的標題欄增加動畫圖標;
- 創建位圖資源,并按序放置所有位圖文件(16*16);
- 在工程工作區選擇ResourceView面板,右擊任一資源項。在彈出的快捷菜單中選擇“Insert...”命令,打開“Insert Source”對話框。
- 在“Resurece Type”列表框中選擇“Bitmap”項,然后單擊右邊的“Import”按鈕。
- 打開“Import Resource”對話框,在文件列表框中選擇要插入的位圖文件,單擊“Improt”按鈕,插入選定的位圖,然后將位圖的ID值修改為需要的值“IDB_ANIM_IMGLIST”。
3.增加管理位圖動畫的文件;這里采用CImageList管理動畫需要的多幅位圖,因此需將AnimateIcon.cpp和AnimateIcon.h添加到工程文件中,步驟如下:
- 將這兩個文件復制到工程所在的文件夾中。
- 在工程工作區選擇“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_
- 在mainframe.h開始添加如下的語句:#include "AnimateIcon.h"
- 在MainFrm.h中增加兩個變量:
protected:
CAnimateIcon m_animIcon;
UINT m_timerID;
- 在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);
- 利用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);
}
編譯、鏈接后運行上述代碼,可以看到在應用程序的標題欄中已有了動畫圖標。
標簽:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:開發者在線