您的位置:首页 > 其它

如何重建丢失的系统托盘图标(MSDN节选翻译)

2008-04-03 09:50 435 查看
Q:I have an application that adds an icon to the system tray. When Windows® Explorer goes down, many times it restarts automatically, but has lost its tray icons. Since my application is still running, do you know of a way I can detect that I need to re-add my tray icon to get my application UI back without requiring a reboot?
Jeff Multhaup
Boise, Idaho
Q:我有一个应用程序可以把图标加到系统托盘里。当explorer.exe崩溃重启以后,系统托盘里的图标也会丢失。因为我的应用程序仍在运行,所以有什么方法可以让我在需要的时候重新加入系统托盘图标而无需重新启动系统?
A: Sure, it's easy-provided you have Windows 98 or the Microsoft® Internet Explorer 4.0 desktop installed. Whenever Internet Explorer 4.0 starts the taskbar, it broadcasts a registered message TaskbarCreated to all top-level parent windows. This is your cue to recreate the icons. If you're using MFC, all you have to do is define a global variable to hold the registered message and implement an ON_REGISTERED_MESSAGE handler for it.
A:有的,在安装了Windows98或者Microsoft® Internet Explorer 4.0的系统上很容易办到。当IE4.0启动任务栏,它会向所有注册了“TaskBarCreated”消息的顶级窗口进行广播,用来提醒你重建图标。如果你使用MFC,你要做的就是定义一个全局变量保存该注册消息并在ON_REGISTERED_MESSAGE中注册一个消息处理函数。
[code]const UINT WM_TASKBARCREATED = ::RegisterWindowMessage(_T("TaskbarCreated"));

[code]BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)

[code]    ON_REGISTERED_MESSAGE(WM_TASKBARCREATED,OnTaskBarCreated)

[code]END_MESSAGE_MAP(CMainFrame, CFrameWnd)

The handler itself should reinstall whatever icons you need.
[code]LRESULT CMainFrame::OnTaskBarCreated(WPARAM wp, LPARAM lp)

[code]{

[code]    VERIFY(InstallIcons());

[code]    return 0;

[code]}

[code]BOOL CMainFrame::InstallIcons()

[code]{

[code]    NOTIFYICONDATA nid;

[code]    //

[code]    // stuff nid with args

[code]    //

[code]    return Shell_NotifyIcon(NIM_ADD, &nid);

[code]}

What could be easier? You should implement InstallIcons as a separate function instead of calling Shell_NotifyIcon directly from OnTaskBarCreated since presumably you will want to also call it when your app starts up.
还有比这更简单的吗?你应该把InstallIcons作为一个独立的函数而不是在OnTaskBarCreated里直接调用Shell_NotifyIcon。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: