为windows应用程序提供托盘图标

时间:2022-01-13 02:57:44

1>包含头文件 #include "Shellapi.h"

2>相关结构体和函数:
    NOTIFYICONDATA
    WINSHELLAPI BOOL WINAPI Shell_NotifyIcon( DWORD dwMessage, PNOTIFYICONDATA pnid )
    细节就去查MSDN吧
3>代码部分:
     //在添加自定义消息宏resource.h
          #define WM_NOTIFYICONWM_USER + 1
     //填充NOTIFYICONDATA结构体
   notify.cbSize = sizeof(NOTIFYICONDATA);
   notify.hWnd = hWnd;
   notify.uID = IDR_MAINFRAME;
   notify.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
   notify.uCallbackMessage = WM_NOTIFYICON;
   notify.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_SMALL));
   strncpy(notify.szTip, "hello world", 64);
    //添加icon
   Shell_NotifyIcon(NIM_ADD, &notify);
    //消息响应部分
         case WM_NOTIFYICON: //一旦鼠标落在通知栏图标矩形区域就发送这个消息
    if(lParam == WM_LBUTTONDOWN) //从Param中获取附加消息
    {
      MessageBox(hWnd, "Hello", "Test", MB_OK);
            ShowWindow(hWnd, SW_SHOWNORMAL);
              }
           break;