windows下的托盘编程(Shell_NotifyIcon)
来源:互联网 发布:excel两列数据相同合并 编辑:程序博客网 时间:2024/06/14 23:05
添加和删除托盘图标
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;
显示托盘提示信息
指定NOTIFYICONDATA的wInfoFlags,szInfoTitle,szInfo信息,再次调用Shell_NotifyIcon函数指定flag为NIM_MODIFY
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);}
托盘右键弹出菜单
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); }}
NIN_BALLOONTIMEOUT为提示消失时触发,NIN_BALLOONUSERCLICK为提示出现点击托盘时触发,NIN_SELECT为单击托盘时触发
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
- windows下的托盘编程(Shell_NotifyIcon)
- windows下的托盘编程(Shell_NotifyIcon)
- Shell_NotifyIcon 修改托盘图标失败的问题
- windows程序托盘图标以及消息提示--基本知识(Shell_NotifyIcon)
- Shell_NotifyIcon实现任意托盘图标的隐藏与再现
- 关于调用Shell_NotifyIcon操作托盘图标失败的说明
- windows 7 Shell_NotifyIcon 使用的问题
- Windows托盘编程!!!
- Windows托盘编程
- Windows TrayIcon (Shell_NotifyIcon)
- Shell_NotifyIcon
- Shell_NotifyIcon
- Shell_NotifyIcon
- Shell_NotifyIcon
- 关于系统响应函数OnSysCommand及托盘Shell_NotifyIcon()等
- 【VS开发】最小化到托盘 shell_notifyicon和NOTIFYICONDATA
- C#托盘程序的编程
- windows 托盘
- Android中的Service的使用详解
- Animating Views Using Scenes and Transitions ? - Creating Custom Transitions
- js获取网页高度
- c++ 链表
- 使用 jQuery Mobile 与 HTML5 开发 Web App(四) —— jQuery Mobile 表单上
- windows下的托盘编程(Shell_NotifyIcon)
- 大数据技术
- Scaling CloudFlare’s Massive WAF
- 利用 GDB & Core Dump 来定位程序出错的位置
- log4j2 使用详解
- width:100vh有感而发
- 【jqgrid】 隐藏、显示行
- 对docume,准确获取网页客户区的宽高、滚动条宽高、滚动条Left和Top
- 搜索引擎七:总结