用VC++在窗口的标题栏中增加动画图标

开发者在线 Builder.com.cn 更新时间:2001-08-14作者:陈晓强 林泉 来源:

本文关键词: C C++


转载自VCHelp

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中已运行通过)

 

用户评论

  • 用户名
  • 评论内容