Jose Jose - 3 months ago 26
C++ Question

NOTIFY ICON Click Win32 Api in c++

I've this code that set an icon in the taskbar, but I can't when someone do Right/Left click on it. Is there a way to do that?

//Notification
NOTIFYICONDATA nid = {};
nid.hWnd = hwnd;
nid.cbSize = sizeof(nid);
nid.uFlags = NIF_ICON | NIF_TIP | NIF_GUID;

// Note: This is an example GUID only and should not be used.
// Normally, you should use a GUID-generating tool to provide the value to
// assign to guidItem.
HICON hIcon = static_cast<HICON>(LoadImage(NULL,
TEXT("gui\\sample.ico"),
IMAGE_ICON,
0, 0,
LR_DEFAULTCOLOR | LR_SHARED | LR_DEFAULTSIZE | LR_LOADFROMFILE));
static const GUID myGUID =
{ 0x23977b55, 0x10e0, 0x4041,{ 0xb8, 0x62, 0xb1, 0x95, 0x41, 0x96, 0x36, 0x68 } };
nid.guidItem = myGUID;
nid.hIcon = hIcon;
// This text will be shown as the icon's tooltip.
StringCchCopy(nid.szTip, ARRAYSIZE(nid.szTip), title);
SendMessage(nid.hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
SendMessage(nid.hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
//TaskBar
// Show the notification.
Shell_NotifyIcon(NIM_ADD, &nid) ? S_OK : E_FAIL;


Could somebody help me please? Thanks

Answer

What you are asking for is covered in MSDN's documentation for Shell_NotifyIcon():

NOTIFYICONDATA structure

NIF_MESSAGE (0x00000001)
The uCallbackMessage member is valid

uCallbackMessage
Type: UINT

An application-defined message identifier. The system uses this identifier to send notification messages to the window identified in hWnd. These notification messages are sent when a mouse event or hover occurs in the bounding rectangle of the icon, when the icon is selected or activated with the keyboard, or when those actions occur in the balloon notification.

When the uVersion member is either 0 or NOTIFYICON_VERSION, the wParam parameter of the message contains the identifier of the taskbar icon in which the event occurred. This identifier can be 32 bits in length. The lParam parameter holds the mouse or keyboard message associated with the event. For example, when the pointer moves over a taskbar icon, lParam is set to WM_MOUSEMOVE.

When the uVersion member is NOTIFYICON_VERSION_4, applications continue to receive notification events in the form of application-defined messages through the uCallbackMessage member, but the interpretation of the lParam and wParam parameters of that message is changed as follows:

  • LOWORD(lParam) contains notification events, such as NIN_BALLOONSHOW, NIN_POPUPOPEN, or WM_CONTEXTMENU.

  • HIWORD(lParam) contains the icon ID. Icon IDs are restricted to a length of 16 bits.

  • GET_X_LPARAM(wParam) returns the X anchor coordinate for notification events NIN_POPUPOPEN, NIN_SELECT, NIN_KEYSELECT, and all mouse messages between WM_MOUSEFIRST and WM_MOUSELAST. If any of those messages are generated by the keyboard, wParam is set to the upper-left corner of the target icon. For all other messages, wParam is undefined.

  • GET_Y_LPARAM(wParam) returns the Y anchor coordinate for notification events and messages as defined for the X anchor.

When you add your notification icon, you need to:

  1. Specify an hWnd that will receive notifications from the icon.

  2. Specify an nID or guidItem to identify the icon. If you use guidItem and display multiple icons, notifications will not be able to tell you which icon is notifying you, so you would have to use a separate HWND for each icon. Also, guidItem is actually more restrictive in functionality than nID, and causes more problems than it solves, so I strongly recommend staying away from guidItem altogether and always use nID only.

  3. enable the NIF_MESSAGE flag, and provide a custom uCallbackMessage message ID.

The HWND's window procedure will then receive the message ID whenever the user interacts with the icon. The message's WPARAM and LPARAM values will describe the action.

For example:

#define APPWM_ICONNOTIFY (WM_APP + 1)

...

HICON hIcon = static_cast<HICON>(LoadImage(NULL,
    TEXT("gui\\sample.ico"),
    IMAGE_ICON,
    0, 0,
    LR_DEFAULTCOLOR | LR_SHARED | LR_DEFAULTSIZE | LR_LOADFROMFILE));

SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);

//Notification
NOTIFYICONDATA nid = {};
nid.cbSize = sizeof(nid);
nid.hWnd = hwnd;
nid.uID = 1;
nid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
nid.uCallbackMessage = APPWM_ICONNOTIFY;
nid.hIcon = hIcon;
// This text will be shown as the icon's tooltip.
StringCchCopy(nid.szTip, ARRAYSIZE(nid.szTip), title);

// Show the notification.
Shell_NotifyIcon(NIM_ADD, &nid);

...

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case APPWM_ICONNOTIFY:
        {
            switch (lParam)
            {
                case WM_LBUTTONUP:
                    //...
                    break;
                case WM_RBUTTONUP:
                    //...
                    break;
            }

            return 0;
        }

        //...
    }

    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
Comments