Restoring the System Tray Icon
Skywing
C/C++/ASM Programmer & Software Developer
Posted On 01/18/03 02:06PM

I'm sure many of you are familiar with programs annoyingly losing their system tray icons when Explorer is restarted.

Fortunately, there's a way for you to receive notifications of the shell restarting, so that you can recreate any tray icon(s) which you may have had up at the time... :

Call RegisterWindowMessage("TaskbarCreated") and store the return value - this is a message which the system will send to you when the shell restarts. The value of this message is not constant, so you'll need to call RegisterWindowMessage when your program loads in order to receive the current value (it will not change after that point). This message is sent to all top-level windows; when you receive it, you can assume that all system tray icons which you may have previously created are removed, and need to be re-added.

This feature is present with IE4 and later...

MSDN provides some sample code in C which demonstrates how to use this:


LRESULT CALLBACK WndProc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam) 
{ 
    static UINT s_uTaskbarRestart; 
    
    switch(uMessage) 
    { 
    case WM_CREATE: 
        s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated")); 
        break; 
    
    default: 
        if(uMessage == s_uTaskbarRestart) 
            AddTaskbarIcons(); 
        break; 
    } 
    return DefWindowProc(hWnd, uMessage, wParam, lParam); 
}