如何在没有在通知区域中添加图标的情况下Shell_NotifyIcon

Win*_*Win 6 winapi system-tray systray tooltip

MSDN关于通知和通知区域的文档非常明确,要求在通知区域中显示一个图标以显示通知:

要显示通知,您必须在通知区域中有一个图标.在某些情况下,例如Microsoft Communicator或电池级别,该图标已经存在.但是,在许多其他情况下,只有在显示通知所需的时间内,您才会向通知区域添加图标.

因为我不希望任何图标添加到通知区域,我想也许是"重用"现有的最有可能是那里的典型桌面.一个好的候选者可能是系统时钟.

我的问题是:

  1. 如何查找/枚举系统时钟的NOTIFYICONDATA结构(恢复时的AKA"日期和时间属性")?
  2. 有没有更好的方法来实现这一点(没有添加图标)?

ixe*_*013 9

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;
 }
Run Code Online (Sandbox Code Playgroud)

  • 是的,很不幸的.通知按进程排队,因此多个进程将分别获取其图标.我通过使用信号量实现IQueryContinue来解决这个问题.正在运行的实例检查另一个(notifu)进程是否想要显示某些内容.如果是这样,它会解雇自己.用户将看到图标消失并再次快速返回.它或者是一个通知中心,它会排队并显示来自多个进程的消息. (2认同)