简体   繁体   English

C ++ Windows系统托盘不会显示消息

[英]C++ Windows System Tray wont display message

I have been stuck here for 4 days. 我已经在这里停留了4天。 I made a function that puts a program in the system tray but the problem here is that it wont show balloon title and message . 我做了一个将程序放入系统托盘的函数,但是这里的问题是它不会显示气球标题和消息 What Am I doing Wrong? 我究竟做错了什么? I even made a separate function to determine what windows os we are running on and initialize cbSize based on the Os detected. 我什至提供了一个单独的函数来确定我们正在运行哪些windows os ,并根据检测到的windows os来初始化cbSize Any help will be appreciated. 任何帮助将不胜感激。 Bellow is the function. 波纹管是功能。

EDIT: I am using Windows 7 and the Icon shows up in the system tray but wont show the message or title. 编辑:我正在使用Windows 7 ,并且图标显示在系统托盘中,但不会显示消息或标题。 I am also doing this Console Application right now as this will be used as a plugin in Unity3D . 我现在也在做这个控制台应用程序,因为它将用作Unity3D的插件。 I want a solution that uses windows api but not windows form as I don't want any new window to open from this. 我想要一个使用Windows API但不使用Windows窗体的解决方案,因为我不希望从中打开任何新窗口。

void createSystemTray()
{
    HWND wHandler = GetDesktopWindow();
    NOTIFYICONDATA iData;
    ZeroMemory(&iData,sizeof(iData));



    if(getOsVersion()=="Windows Vista" || getOsVersion()=="Windows 7" || getOsVersion()=="Windows 8" || getOsVersion()=="Windows 8.1")
    {
        iData.cbSize = sizeof(NOTIFYICONDATA);
    }

    else if (getOsVersion()=="Windows XP"||getOsVersion()=="Windows XP Professional x64 Edition")
    {
        iData.cbSize = NOTIFYICONDATA_V3_SIZE;
    }

    else if (getOsVersion()=="Windows 2000")
    {
        iData.cbSize = NOTIFYICONDATA_V2_SIZE;
    }

    else if (getOsVersion()=="UNKNOWN OS")
    {
//Assume we have old Windows Os such as Me,95....
        iData.cbSize = NOTIFYICONDATA_V1_SIZE;
    }
    iData.hWnd = wHandler;
    iData.uID = 100;
    iData.uVersion = NOTIFYICON_VERSION_4;
    iData.uCallbackMessage = WM_MESSAGE;
    iData.hIcon = LoadIcon(NULL,(LPCTSTR)IDI_WARNING);
    lstrcpy(iData.szTip,"My First Tray Icon");
    lstrcpy(iData.szInfo,"My App Info");
    lstrcpy(iData.szInfoTitle,"My Info Title");
    iData.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
    Shell_NotifyIcon(NIM_SETVERSION,&iData); //called only when usingNIM_ADD
    Shell_NotifyIcon(NIM_ADD,&iData);
}

I added NIF_INFO to the uFlags and the problem is gone. 我将NIF_INFO添加到uFlags ,问题消失了。 Now it displays everything including text, title and info title. 现在,它将显示所有内容,包括文本,标题和信息标题。 The code below is what solved it. 下面的代码解决了它。

iData.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP|NIF_SHOWTIP|NIF_INFO;

Your biggest problem with the code in the question is that you pass the wrong window handle. 问题中的代码最大的问题是传递了错误的窗口句柄。 You have to pass one of your window handles. 您必须通过您的窗口句柄之一。 But instead you pass the window handle of the desktop. 但是,您可以传递桌面的窗口句柄。

You will need to create a window and use its handle. 您将需要创建一个窗口并使用其句柄。 The window does not need to be visible. 该窗口不需要是可见的。 I believe that you can use a message only window. 我相信您可以使用“仅消息”窗口。

You must also call NIM_SETVERSION after NIM_ADD . 你还必须调用NIM_SETVERSIONNIM_ADD

I'm very sceptical of your version switching being based on string equality testing. 我非常怀疑您的版本切换基于字符串相等性测试。 Your code will break on Windows 9 for instance. 例如,您的代码将在Windows 9上中断。 Use the version helper functions . 使用版本帮助器功能

You also perform no error checking. 您也不会执行任何错误检查。 This isn't the easiest function to call but your failure to check for errors makes things even harder than they need to be. 这不是最容易调用的函数,但是如果您无法检查错误,则会使事情变得比原本更难做。 Please read the documentation and add error checking code. 请阅读文档并添加错误检查代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM