NOTIFY ICON 在 C++ 中单击 Win32 Api

NOTIFY ICON Click Win32 Api in c++

我有这段代码可以在任务栏中设置一个图标,但是当有人 Right/Left 单击它时我不能。有没有办法做到这一点?

//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;

有人可以帮我吗?谢谢

MSDN 的 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.

添加通知图标时,您需要:

  1. 指定一个 hWnd 将接收来自图标的通知。

  2. 指定 nIDguidItem 来标识图标。如果您使用 guidItem 并显示多个图标,通知将无法告诉您哪个图标在通知您,因此您必须为每个图标使用单独的 HWND。此外,guidItem 实际上在功能上比 nID 更受限制,并且导致的问题多于它解决的问题,因此我强烈建议完全远离 guidItem 并始终只使用 nID .

  3. 启用 NIF_MESSAGE 标志,并提供自定义 uCallbackMessage 消息 ID。

HWND 的 window 过程将在用户与图标交互时接收消息 ID。消息的 WPARAMLPARAM 值将描述操作。

例如:

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