Windows TrayIcon (Shell_NotifyIcon)

Add/Delete TrayIcon

BOOL AddNotificationIcon(HWND hwnd)
{
    NOTIFYICONDATA nid = {sizeof(nid)};
    nid.hWnd = hwnd;
    // add the icon, setting the icon, tooltip, and callback message.
    // the icon will be identified with the GUID
    nid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE | NIF_SHOWTIP | NIF_GUID;
    nid.guidItem = __uuidof(PrinterIcon);
    nid.uCallbackMessage = WMAPP_NOTIFYCALLBACK;
    LoadIconMetric(g_hInst, MAKEINTRESOURCE(IDI_NOTIFICATIONICON), LIM_SMALL, &nid.hIcon);
    LoadString(g_hInst, IDS_TOOLTIP, nid.szTip, ARRAYSIZE(nid.szTip));
    Shell_NotifyIcon(NIM_ADD, &nid);

    // NOTIFYICON_VERSION_4 is prefered
    nid.uVersion = NOTIFYICON_VERSION_4;
    return Shell_NotifyIcon(NIM_SETVERSION, &nid);
}

BOOL DeleteNotificationIcon()
{
    NOTIFYICONDATA nid = {sizeof(nid)};
    nid.uFlags = NIF_GUID;
    nid.guidItem = __uuidof(PrinterIcon);
    return Shell_NotifyIcon(NIM_DELETE, &nid);
}

WMAPP_NOTIFYCALLBACK为自定义的消息

UINT const WMAPP_NOTIFYCALLBACK = WM_APP + 1;

Show TrayIcon Infromation

BOOL ShowLowInkBalloon()
{
    // Display a low ink balloon message. This is a warning, so show the appropriate system icon.
    NOTIFYICONDATA nid = {sizeof(nid)};
    nid.uFlags = NIF_INFO | NIF_GUID;
    nid.guidItem = __uuidof(PrinterIcon);
    // respect quiet time since this balloon did not come from a direct user action.
    nid.dwInfoFlags = NIIF_WARNING | NIIF_RESPECT_QUIET_TIME;
    LoadString(g_hInst, IDS_LOWINK_TITLE, nid.szInfoTitle, ARRAYSIZE(nid.szInfoTitle));
    LoadString(g_hInst, IDS_LOWINK_TEXT, nid.szInfo, ARRAYSIZE(nid.szInfo));
    return Shell_NotifyIcon(NIM_MODIFY, &nid);
}

BOOL ShowNoInkBalloon()
{
    // Display an out of ink balloon message. This is a error, so show the appropriate system icon.
    NOTIFYICONDATA nid = {sizeof(nid)};
    nid.uFlags = NIF_INFO | NIF_GUID;
    nid.guidItem = __uuidof(PrinterIcon);
    nid.dwInfoFlags = NIIF_ERROR;
    LoadString(g_hInst, IDS_NOINK_TITLE, nid.szInfoTitle, ARRAYSIZE(nid.szInfoTitle));
    LoadString(g_hInst, IDS_NOINK_TEXT, nid.szInfo, ARRAYSIZE(nid.szInfo));
    return Shell_NotifyIcon(NIM_MODIFY, &nid);
}

BOOL ShowPrintJobBalloon()
{
    // Display a balloon message for a print job with a custom icon
    NOTIFYICONDATA nid = {sizeof(nid)};
    nid.uFlags = NIF_INFO | NIF_GUID;
    nid.guidItem = __uuidof(PrinterIcon);
    nid.dwInfoFlags = NIIF_USER | NIIF_LARGE_ICON;
    LoadString(g_hInst, IDS_PRINTJOB_TITLE, nid.szInfoTitle, ARRAYSIZE(nid.szInfoTitle));
    LoadString(g_hInst, IDS_PRINTJOB_TEXT, nid.szInfo, ARRAYSIZE(nid.szInfo));
    LoadIconMetric(g_hInst, MAKEINTRESOURCE(IDI_NOTIFICATIONICON), LIM_LARGE, &nid.hBalloonIcon);
    return Shell_NotifyIcon(NIM_MODIFY, &nid);
}

TrayIcon RightClick Popup Menu

void ShowContextMenu(HWND hwnd, POINT pt)
{
    HMENU hMenu = LoadMenu(g_hInst, MAKEINTRESOURCE(IDC_CONTEXTMENU));
    if (hMenu)
    {
        HMENU hSubMenu = GetSubMenu(hMenu, 0);
        if (hSubMenu)
        {
            // our window must be foreground before calling TrackPopupMenu or the menu will not disappear when the user clicks away
            SetForegroundWindow(hwnd);

            // respect menu drop alignment
            UINT uFlags = TPM_RIGHTBUTTON;
            if (GetSystemMetrics(SM_MENUDROPALIGNMENT) != 0)
            {
                uFlags |= TPM_RIGHTALIGN;
            }
            else
            {
                uFlags |= TPM_LEFTALIGN;
            }

            TrackPopupMenuEx(hSubMenu, uFlags, pt.x, pt.y, hwnd, NULL);
        }
        DestroyMenu(hMenu);
    }
}

This code should be put at the WndProc() the meaning of wParam and lParam  please refer to msdn.The uVersion field of NOTIFYICONDATA is different and the meaning of these two parameter is different.Please note this.

case WMAPP_NOTIFYCALLBACK:
    switch (LOWORD(lParam))
    {
    case NIN_SELECT:
        // for NOTIFYICON_VERSION_4 clients, NIN_SELECT is prerable to listening to mouse clicks and key presses
        // directly.
        if (IsWindowVisible(s_hwndFlyout))
        {
            HideFlyout(hwnd, s_hwndFlyout);
            s_hwndFlyout = NULL;
            s_fCanShowFlyout = FALSE;
        }
        else if (s_fCanShowFlyout)
        {
            s_hwndFlyout = ShowFlyout(hwnd);
        }
        break;

    case NIN_BALLOONTIMEOUT:
        RestoreTooltip();
        break;

    case NIN_BALLOONUSERCLICK:
        RestoreTooltip();
        // placeholder for the user clicking on the balloon.
        MessageBox(hwnd, L"The user clicked on the balloon.", L"User click", MB_OK);
        break;

    case WM_CONTEXTMENU:
        {
            POINT const pt = { LOWORD(wParam), HIWORD(wParam) };
            ShowContextMenu(hwnd, pt);
        }
        break;
    }
    break;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Windows 右下角弹窗通常是通过 Windows API 中的通知区域(Notification)实现的。来说,可以使用步骤来实现: . 使用 WinAPI 函数_NotifyIcon 来创建一个通知标,将其添加知区域中。 . 设置通知图标的属性,包括图标、提示文本等3. 监听通知图标的消息,鼠标点击、鼠标悬停等。 4 根据需要,显示弹窗或执行其他操作以下是一个简单的示例代码(使用 C/C++ 编写): ```cpp #include <Windows.h> // 定义唯一的消息ID #define WM_TRAYICON (WM_USER + 1) // 通知图标结构体 NOTIFYICONDATA nid; // 弹窗回调函数 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_CREATE: // 创建通知图标 nid.cbSize = sizeof(NOTIFYICONDATA); nid.hWnd = hwnd; nid.uID = 1; nid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE; nid.uCallbackMessage = WM_TRAYICON; nid.hIcon =Icon(NULL, IDI_APPLICATION); strcpy_s(nid.szTip, sizeof(nid.szTip), "提示本"); Shell_NotifyIcon(NIM_ADD, &nid); break; case WM_TRAYICON: // 处理通知图标的消息 if (wParam == 1 && lParam == WM_RBUTTONUP) { // 右键点击通知图标时显示弹窗 MessageBox(hwnd, "弹窗内容", "弹窗标题", MB_ICONINFORMATION); } break; case WM_DESTROY: // 移除通知图标 Shell_NotifyIcon(NIM_DELETE, &nid); PostQuitMessage(0); break; } return DefWindowProc(hwnd, msg, wParam, lParam); } int main() { // 注册窗口类 WNDCLASS wc = {0}; wc.lpfnWndProc = WndProc; wc.hInstance = GetModuleHandle(NULL); wc.lpszClassName = "MyWindowClass"; RegisterClass(&wc); // 创建窗口 HWND hwnd = CreateWindow(wc.lpszClassName, "My Window", 0, 0, 0, 0, 0, NULL, NULL, wc.hInstance, NULL); // 消息循环 MSG msg; while (GetMessage(&msg, NULL, 0, 0) > 0) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } ``` 请注意,这只是一个简单示例,实际使用时可能需要根据需求进行适当的修改和扩展。同时,需要注意遵循 Windows API 的使用规范和相关法律法规。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值