windows下的托盘编程(Shell_NotifyIcon)
2011-08-31 23:46 Clingingboy 阅读(9866) 评论(0) 编辑 收藏 举报
添加和删除托盘图标
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;