Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to keep an icon in the tray after Explorer's crash

Programming

How to keep an icon in the tray after Explorer's crash

by  imagenetics  Posted    (Edited  )
Introduction:
If you wrote an application which displays an icon in Taskbar Notification Area (aka System Tray), you probably wonder how to restore this icon after Explorer's crash without restarting the application. The solution lies in responding to a message sent to the application when taskbar is created.

Note:
1) This FAQ will not cover the subject of displaying an icon in the tray. I will focus only on restoring this icon.

2) In the following example an icon is created by TrayIcon_Create() function.

Test:
To simulate an Explorer's crash and see how an icon of your application (don't forget to have it running, ofcourse) disappears from the tray, open Windows Task Manager (Ctrl+Alt+Del), go to Processes tab and in the list find explorer.exe. Terminate this process. The taskbar will disappear. Now from the File menu run Explorer again (execute "C:\WINDOWS\explorer.exe"). The taskbar is recreated but your application's icon is not in the tray, the application itself, however, is still running (check the processes list). Right?

Let's go:
Open your application's project and go to the header file of the form which holds the functions for creating a tray icon. Add this:
Code:
...
private:
    [color green]unsigned long WM_TASKBARCREATED;[/color]
    [color green]void __fastcall WndProc(TMessage &Msg);[/color]

Now in the *.cpp file add the following in the form's constructor:
Code:
...
//-----------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    [color green]WM_TASKBARCREATED = RegisterWindowMessage("TaskbarCreated");[/color]
}

Still in the *.cpp file, write the body of the function which will process messages sent to your application:
Code:
[color green]void __fastcall TForm1::WndProc(TMessage &Msg)
{
    if(Msg.Msg == WM_TASKBARCREATED)
    {
        TrayIcon_Create(); [color blue][i]// Add the icon[/i][/color]
    }

    TForm::WndProc(Msg);
}
[/color]

OK. Now compile and run the application. Perform the above test again and see that the icon is restored in the tray after the taskbar is recreated.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top