下面是显示托盘图标的代码。
我检测不到托盘图标的时候,会调用下面的代码。
但是返回错误
Shell_NotifyIcon(NIM_ADD, &_nid),的错误码是1008
Shell_NotifyIcon(NIM_MODIFY, &_nid),的错误码是1460
void TerminalInfo::ShowTrayIcon()
{
memset(&_nid, 0, sizeof(_nid));
_tcscpy_s(_nid.szTip, _T("test"));
_nid.cbSize = sizeof(NOTIFYICONDATA);
_nid.uID = 0;
_nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
_nid.hWnd = m_hWnd;
_nid.uCallbackMessage = WM_TRAY;
_nid.hIcon = _currentIcon; if (_bShowWindow)
{
if (_trayIconChange == 1)
{
if(!Shell_NotifyIcon(NIM_MODIFY, &_nid))
{
_logService->Error(L"改变托盘图标失败,改变参数:%d 错误码:%X", _trayIconChange, GetLastError());
}
}
else
{
if (!Shell_NotifyIcon(NIM_ADD, &_nid))
{
_logService->Error(L"显示托盘图标失败,改变参数:%d 错误码:%X", _trayIconChange, GetLastError());
}
}
}
else
{
_currentIcon = NULL;
RemoveTrayIcon();
}
}

解决方案 »

  1.   

    隐藏不会导致窗口不见
    参考// ShellNotifyIconTest.cpp : Defines the entry point for the application.
    //#include "stdafx.h"
    #include "resource.h"
    #include <crtdbg.h>
    #include <windows.h>
    #include <shellapi.h>// Global Variables:
    HINSTANCE hInst; // current instance
    LPCTSTR szWindowClass = _T("ShllNotifyTest"); // The title bar text// Foward declarations of functions included in this code module:
    ATOM MyRegisterClass(HINSTANCE hInstance);
    BOOL InitInstance(HINSTANCE, int);
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);NOTIFYICONDATA m_NFData={0};
    #define WM_NOFIY_MSG (WM_USER + 1000)int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
      // TODO: Place code here.
      MSG msg; 
      // Initialize global strings
      MyRegisterClass(hInstance);
      
      // Perform application initialization:
      if (!InitInstance (hInstance, nCmdShow)) 
      {
        return FALSE;
      }
      
      // Main message loop:
      while (GetMessage(&msg, NULL, 0, 0)) 
      {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
      }
      
      //
      Shell_NotifyIcon(NIM_DELETE, &m_NFData);
      
      return msg.wParam;
    }ATOM MyRegisterClass(HINSTANCE hInstance)
    {
      WNDCLASSEX wcex;
      
      wcex.cbSize = sizeof(WNDCLASSEX); 
      
      wcex.style = CS_HREDRAW | CS_VREDRAW;
      wcex.lpfnWndProc = (WNDPROC)WndProc;
      wcex.cbClsExtra = 0;
      wcex.cbWndExtra = 0;
      wcex.hInstance = hInstance;
      wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_SHELLNOTIFYICONTEST);
      wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
      wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
      wcex.lpszMenuName = (LPCSTR)IDC_SHELLNOTIFYICONTEST;
      wcex.lpszClassName = szWindowClass;
      wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
      
      return RegisterClassEx(&wcex);
    }BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
      HWND hWnd;
      
      hInst = hInstance; // Store instance handle in our global variable
      
      hWnd = CreateWindow(szWindowClass, _T("TEST"), WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
      
      if (!hWnd)
      {
        return FALSE;
      }
      
      ShowWindow(hWnd, nCmdShow);
      UpdateWindow(hWnd);
      
      memset(&m_NFData, 0, sizeof(m_NFData));
      m_NFData.cbSize = sizeof(m_NFData);
      m_NFData.hWnd = hWnd;
      m_NFData.uFlags  = NIF_ICON | NIF_MESSAGE | NIF_TIP;
      m_NFData.uCallbackMessage = WM_NOFIY_MSG;
      m_NFData.hIcon = LoadIcon(NULL, IDI_APPLICATION);
      _tcscpy_s(m_NFData.szTip, _T("Shell_Notify Test"));
      if(!Shell_NotifyIcon(NIM_ADD, &m_NFData))
      {
        _ASSERT(0);
      }
      
      
      return TRUE;
    }LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
      switch (message) 
      {
      case WM_NOFIY_MSG:
        {
          switch(LOWORD(lParam))
          {
          case(WM_LBUTTONDBLCLK): //双击左键
            {
              ShowWindow(hWnd, SW_SHOW);
              break;
            }
          case(WM_RBUTTONDOWN): //右键弹出菜单
            {
              POINT point;
              GetCursorPos(&point);
              SetForegroundWindow(hWnd);
              HMENU hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDC_SHELLNOTIFYICONTEST));
              if(hMenu)
              {
                UINT uCmd = TrackPopupMenu(GetSubMenu(hMenu, 0), 
                  TPM_RETURNCMD | TPM_NONOTIFY,
                  point.x, point.y, 0, hWnd, NULL);
                if(uCmd)
                {
                  PostMessage(hWnd, WM_COMMAND, uCmd, 0);
                }
                DestroyMenu(hMenu);
              }
              break;
            }
          }
          
          break;
        }
        
      case WM_COMMAND:
        {
          UINT wmId    = LOWORD(wParam); 
          UINT wmEvent = HIWORD(wParam); 
          // Parse the menu selections:
          switch (wmId)
          {
          case IDM_SHOW:
            {
              ShowWindow(hWnd, SW_SHOW);
              break;
            }
          case IDM_HIDE:
            {
              ShowWindow(hWnd, SW_HIDE);
              break;
            }
          case IDM_EXIT:
            {
              DestroyWindow(hWnd);
              break;
            }
          default:
            return DefWindowProc(hWnd, message, wParam, lParam);
          }
          break;
        }
      case WM_PAINT:
        {
          PAINTSTRUCT ps;
          HDC hdc = BeginPaint(hWnd, &ps);
          // TODO: Add any drawing code here...
          RECT rt;
          GetClientRect(hWnd, &rt);
          DrawText(hdc, _T("Hello"), -1, &rt, DT_CENTER);
          EndPaint(hWnd, &ps);
          break;
        }
      case WM_DESTROY:
        {
          PostQuitMessage(0);
          break;
        }
      default:
        {
          return DefWindowProc(hWnd, message, wParam, lParam);
        }
      }
      return 0;
    }