A few days ago saw a "how to display a dynamic icon in the title bar" article, the author of Chongqing Construction Group, a Town. Feel the use of settimer to achieve trouble, and occupy resources. An itch on hand changes it to a thread implementation and encapsulates the thread in the class. Simple to use, do not need to set settimer in the OnTimer function call. Simply call M_animicon.setimagelist (Idb_bitmap_anim,6,rgb (0,0,0) directly);
M_animicon.draw (this);
The other parts of the program are Town, and I just change the part of the OnTimer implementation to thread implementation.
class Canimateicon
{
Protected:
//Create a list variable for saving image columns
CImageList m_imglist;
Current image ordinal
int m_iimagecounter;
Total number of images
int m_nmaxnumofimages;
Closes the thread
bool m_bexit;
The window class pointer for the window that owns the icon
CWnd * M_PWND;
Icon Refresh interval
DWORD M_dwmilliseconds;
Public:
void Changeinterval (DWORD dwtimes);/change Icon refresh interval
BOOL Draw (C WND *pwnd,dword dwtimes=1000);
Canimateicon ();
~canimateicon ();
BOOL SetImageList (int idofimagelistresource,int numberofimages,colorref transparentcolor);
Protected:
Static UINT winapi _threadanimation (LPVOID pparam);
};
BOOL Canimateicon::D Raw (CWnd *pwnd,dword dwtimes)
{
//window class pointer for window with icon
M_pwnd=pwnd;
ASSERT (M_pwnd);
//Icon refresh interval
M_dwmilliseconds=dwtimes
unsigned int ndummy;
//Note: The pointer passed to the Cheng Ben instance of the line
HANDLE hthread = (HANDLE) _beginthreadex (null,0,_threadanimation,this,
create_s Uspended,&ndummy);
if (!hthread) {
TRACE (_t ("Draw:couldn" T start a ICON animation thread\n));
return false;
}
Else {
ResumeThread (hthread);
CloseHandle (hthread);//no longer requires a thread handle to turn it off
return true;
}
UINT winapi Canimateicon::_threadanimation (lpvoid pparam)
{
//From an instance pointer passed over, you can manipulate different instances,
//Note that thread functions are unique and static.
Canimateicon &p=* (Canimateicon *) Pparam;
while (!p.m_bexit) {
if (p.m_imglist.m_himagelist==null)
return 0;
p.m_iimagecounter++;
if (p.m_iimagecounter>=p.m_nmaxnumofimages)
p.m_iimagecounter=0;
Hicon Hicon=p.m_imglist.extracticon (P.m_iimagecounter);
Hicon hprevicon= (hicon) P.m_pwnd->seticon (Hicon,false);
DestroyIcon (hprevicon);
Sleep (p.m_dwmilliseconds);
}
return 1;
}
Compile through the VC6.0+WIN2000+SP3!
This article supporting source code