如何在 explorer.exe 重新启动时恢复 "missing" 通知图标?

How to restore a "missing" notification icon when explorer.exe is restarted?

我有一个 Win32 应用程序,它在启动时添加了一个任务栏图标。

它运行完美,除了 Explorer 崩溃然后重新启动时,我的应用程序保持 运行,但任务栏图标消失了。

我怀疑我应该处理一些事件,但是哪个?

你说的其实就是通知图标。正确使用此术语非常重要,这样我们才能了解您的意思。不管怎样,我觉得我的猜测是准确的。

您需要侦听创建任务栏时向所有顶级 windows 广播的 window 消息。这是 documented 像这样:

With Microsoft Internet Explorer 4.0 and later, the Shell notifies applications that the taskbar has been created. When the taskbar is created, it registers a message with the TaskbarCreated string and then broadcasts this message to all top-level windows. When your taskbar application receives this message, it should assume that any taskbar icons it added have been removed and add them again. This feature generally applies only to services that are already running when the Shell launches. The following example shows a very simplified method for handling this case.

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);
}