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

 
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中注册一个消息处理函数。
const UINT WM_TASKBARCREATED = ::RegisterWindowMessage(_T("TaskbarCreated"));
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
    ON_REGISTERED_MESSAGE(WM_TASKBARCREATED,OnTaskBarCreated)
END_MESSAGE_MAP(CMainFrame, CFrameWnd)
     The handler itself should reinstall whatever icons you need.
LRESULT CMainFrame::OnTaskBarCreated(WPARAM wp, LPARAM lp)
{
    VERIFY(InstallIcons());
    return 0;
}
BOOL CMainFrame::InstallIcons()
{
    NOTIFYICONDATA nid; 
    //
    // stuff nid with args
    //
    return Shell_NotifyIcon(NIM_ADD, &nid);
}
     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。
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值