6

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

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

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

我的问题是:

  1. 如何查找/枚举系统时钟的 NOTIFYICONDATA 结构(恢复时也称为“日期和时间属性”)?
  2. 有没有更好的方法来完成这个(添加图标)?
4

1 回答 1

10

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

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

这是它的胆量:

 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;
 }
于 2011-07-05T12:20:22.517 回答