首页 > 代码库 > VC++界面编程之--托盘图标无缘无故被隐藏的原因

VC++界面编程之--托盘图标无缘无故被隐藏的原因

有时我们创建的托盘图标会无缘无故的消失了,搜索了下谷歌,发现是explorer.exe有时会记不住增加的托盘图标,当它记不住时,会发送消息询问窗口:有没有需要增加托盘图标的?然后你的窗口重新做一次增加托盘图标的动作即可。以下是原文:

When you’re a programmer, it seems like you learn something new every day. One of the things I’ve been working on in the next version of TopDesk is making it survive Explorer crashes. TopDesk has quite a few hooks into explorer.exe, so when Explorer goes down, TopDesk goes with it. After banging my head against the wall several times I managed to fix the problem, but I noticed that even with the fix in place the TopDesk system tray (sorry, taskbar notification area) icon failed to re-appear when Explorer came back to life.

What’s happening is that when Explorer crashes, the taskbar notification area crashes too, forgetting any icons it had. When Explorer starts back up, it re-creates the taskbar notification area and sends out a registered window message called (strangely enough) TaskbarCreated to all top-level windows. It’s up to an application to check for this message and re-initialize any taskbar notification area icons it has. Here’s how you do it in C/C++:

1. Register your application so it can receive TaskbarCreated messages:

UINT wmTaskbarCreated = RegisterWindowMessage(_T("TaskbarCreated"));

2. Handle the message in your window procedure:

LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT nMessage, WPARAM wParam, LPARAM lParam)
{
	switch(nMessage)
	{
	default:
		{
			if(nMessage == wmTaskbarCreated)
			{
				// Call your system tray icon initialization function/method here
			}
		}
		break;
	}

	return DefWindowProc(hWnd, uMessage, wParam, lParam);
}

VC++界面编程之--托盘图标无缘无故被隐藏的原因