如何在通知区域不添加图标的情况下使用 Shell_NotifyIcon [英] How to Shell_NotifyIcon WITHOUT adding an icon in the notification area

查看:18
本文介绍了如何在通知区域不添加图标的情况下使用 Shell_NotifyIcon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN 关于通知和通知区域的文档是在通知区域中有一个图标以显示通知的要求非常清楚:

MSDN's documentation about Notifications and the Notification Area is pretty clear in the requirement for having an icon in the notification area in order to display a notification:

要显示通知,您必须在通知中有一个图标区域.在某些情况下,例如Microsoft Communicator 或电池级别,该图标将已经展示.然而,在许多其他情况下,您将添加一个图标到通知区域只要是需要显示通知.

To display a notification, you must have an icon in the notification area. In certain cases, such as Microsoft Communicator or battery level, that icon will already be present. In many other cases, however, you will add an icon to the notification area only as long as is needed to show the notification.

由于我不希望向通知区域添加任何图标,我正在考虑重用"一个最有可能出现在典型桌面上的现有图标.一个很好的候选者可能是系统时钟.

Since I do not wish to add any icon to the notification area, I was thinking of perhaps "reusing" an existing one that is most likely to be there on a typical desktop. A good candidate may be the system clock.

我的问题是:

  1. 如何查找/枚举NOTIFYICONDATA 结构用于系统时钟(又名日期和时间属性"恢复时)?
  2. 有没有更好的方法完成此操作(无需添加一个图标)?
  1. How do I find/enumerate the NOTIFYICONDATA structure for the system clock (AKA "Date and Time Properties" when restored)?
  2. Is there a better way of accomplishing this (without adding an icon)?

推荐答案

Shell_NotifyIcon 在幕后使用 IUserNotification.我玩弄了它并制作了一个实用程序.我听说有一位视障系统管理员使用它来使他的脚本屏幕阅读器兼容.它是命令行,它没有消息循环.

Shell_NotifyIcon uses IUserNotification under the hood. I played around with it and made a utility out of it. I heard of a visually impaired sysadmin who uses it to make his scripts screen reader compatible. It is command line, it does not have a message loop.

它是自我感知的,这意味着发送给它的通知将排队(您可以控制它).为此,我提供了一个 IQueryContinue 实现.该项目是在 C++ 并且是开源的,帮助你自己.

It is self aware, meaning that notifications sent to it will be queued (you have control over it). For that to work, I provided a IQueryContinue implementation. The project is in C++ and is open source, help yourself.

这是它的胆量:

 HRESULT NotifyUser(const NOTIFU_PARAM& params, IQueryContinue *querycontinue, IUserNotificationCallback *notifcallback)
 {
    HRESULT result = E_FAIL;

    IUserNotification *un = 0;
    IUserNotification2 *deux = 0; //French pun : "un" above stands for UserNotification but it also means 1 in French. deux means 2.

    //First try with the Vista/Windows 7 interface
    //(unless /xp flag is specified
    if (!params.mForceXP)
       result = CoCreateInstance(CLSID_UserNotification, 0, CLSCTX_ALL, IID_IUserNotification2, (void**)&deux);

    //Fall back to Windows XP
    if (!SUCCEEDED(result))
    {
       TRACE(eWARN, L"Using Windows XP interface IUserNotification\n");
       result = CoCreateInstance(CLSID_UserNotification, 0, CLSCTX_ALL, IID_IUserNotification, (void**)&un);
    }
    else
    {
       TRACE(eINFO, L"Using Vista interface IUserNotification2\n");
       un = (IUserNotification*)deux; //Rather ugly cast saves some code...
    }

    if (SUCCEEDED(result))
    {
       const std::basic_string<TCHAR> crlf_text(L"\\n");
       const std::basic_string<TCHAR> crlf(L"\n");
       std::basic_string<TCHAR> text(params.mText);
       size_t look = 0;
       size_t found;

       //Replace \n with actual CRLF pair
       while ((found = text.find(crlf_text, look)) != std::string::npos)
       {
          text.replace(found, crlf_text.size(), crlf);
          look = found+1;
       }

       result = un->SetIconInfo(params.mIcon, params.mTitle.c_str());
       result = un->SetBalloonInfo(params.mTitle.c_str(), text.c_str(), params.mType);

       //Looks like it controls what happends when the X button is
       //clicked on
       result = un->SetBalloonRetry(0, 250, 0);

       if (deux)
          result = deux->Show(querycontinue, 250, notifcallback);
       else
          result = un->Show(querycontinue, 250);

       un->Release();
    }

    return result;
 }

这篇关于如何在通知区域不添加图标的情况下使用 Shell_NotifyIcon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆