Programmer
Programmer

Reputation: 125275

C++ Windows System Tray wont display message

I have been stuck here for 4 days. 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. 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. I am also doing this Console Application right now as this will be used as a plugin in 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.

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);
}

Upvotes: 1

Views: 1560

Answers (2)

Programmer
Programmer

Reputation: 125275

I added NIF_INFO to the uFlags and the problem is gone. 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;

Upvotes: 3

David Heffernan
David Heffernan

Reputation: 612954

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.

I'm very sceptical of your version switching being based on string equality testing. Your code will break on Windows 9 for instance. 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.

Upvotes: 2

Related Questions