To prevent missing taskbar icons from system crashes

Source: Internet
Author: User

This article refers to "the program spring and Autumn" 2003/1 P66 "in Visual Basic to find the" vanishing "system tray icon," the Author: Zhong Lanfang, thank the original author.

Because the original program with VB, so Ccrun will change it to C++builder version, hehe. Please correct me.

A lot of software in the system Tray area added icon, can sometimes when Explorer.exe inexplicable crash, the program in the taskbar icon disappeared, for some in the taskbar hidden software, there is no control. The reason is that Explorer rebuilds the taskbar later, but most software does not know that the taskbar has been rebuilt, so there is no time to repaint its own icon in the taskbar.

When the taskbar is established, a message is sent to all top-level windows in the system: wm_taskbarcreated, we just need to capture this message and recreate the taskbar icon. However, this is a custom message for the taskbar, so you need to register the message with RegisterWindowMessage in your own program.

First define two global variables

long wm_taskbarcreated = 0;


unsigned int liconmessage = 0;


declares the following function in. h:


private://User declarations


void Tform1::removetrayicon ();


void Tform1::addtrayicon ();


void __fastcall tform1::wndproc (messages::tmessage& message);


 


registers the wm_taskbarcreated message in the form's OnCreate event and adds an icon to the pallet area


//---------------------------------------------------------------------------


void __fastcall tform1::formcreate (tobject *sender)


{


wm_taskbarcreated = RegisterWindowMessage ("taskbarcreated");


liconmessage=registerwindowmessage ("iconnotify");


Addtrayicon ();


}


deletes the icon in the taskbar in the OnClose event of the form


//---------------------------------------------------------------------------


void __fastcall tform1::formclose (tobject *sender, tcloseaction &action)


{


Removetrayicon ();


}


overload WndProc, responding to system messages


//---------------------------------------------------------------------------


void __fastcall tform1::wndproc (messages::tmessage& message)


{


if (message.msg==liconmessage)


   {


if (MESSAGE.LPARAM==WM_LBUTTONDBLCLK)//double-click the icon event


     //....


   }


if (message.msg==wm_taskbarcreated)//taskbar rebuild message


Addtrayicon (); Re-add icon


   


tform::wndproc (message)//For other messages, the WNDPROC function of the underlying class is invoked to let Windows default processing.


}


the following are custom functions that add and remove icons in the taskbar


//---------------------------------------------------------------------------


void Tform1::addtrayicon ()


{


Notifyicondata Icondata;


memset (&icondata,0,sizeof (icondata));


//Initializes the domain of the structure icondata to 0


icondata.cbsize=sizeof (Icondata);


Icondata.hwnd=handle;


strncpy (icondata.sztip,application->title.c_str,sizeof (Icondata.sztip));


icondata.hicon=application->icon->handle;


Icondata.ucallbackmessage=liconmessage;


icondata.uflags=nif_message| nif_icon| Nif_tip;


Shell_NotifyIcon (Nim_add,&icondata);


}


//---------------------------------------------------------------------------


void Tform1::removetrayicon ()


{


Notifyicondata Icondata;


memset (&icondata,0,sizeof (icondata));


icondata.cbsize=sizeof (Icondata);


Icondata.hwnd=handle;


Shell_NotifyIcon (Nim_delete,&icondata);


}

Compile this program, and then use Task Manager or other tools to abort Explorer.exe, you can see in the Explorer Reload, this program in the taskbar icon again, hey.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.