VC如何实现电脑任意一个托盘图标的隐藏

比如强杀了进程后销毁相应的托盘图标。

方式一:

/****************************************************************/
/*  RefurbishTray函数主要是实现对系统托盘的图标刷新的问题           */
/*  由于托盘程序如果被强行杀掉进程等操作后,在托盘中的图标不能自动删除   */
/*  运用以下的方法,实现模拟鼠标点击托盘图标,使得图标刷新             */
/*****************************************************************/
void __fastcall RemoveDeadIcons()
{
    RECT rctTrayIcon;
    // Get tray window handle and bounding rectangle
    HWND hTrayWindow = FindWindowEx(
        FindWindow("Shell_TrayWnd", NULL), 0, "TrayNotifyWnd", NULL);
    if(!GetWindowRect(hTrayWindow, &rctTrayIcon))
        return;
    // Get small icon metrics
    int nIconWidth = GetSystemMetrics(SM_CXSMICON);
    int nIconHeight = GetSystemMetrics(SM_CYSMICON);
    // Save current mouse position }
    CPoint CursorPos;
    GetCursorPos(&CursorPos);
    // Sweep the mouse cursor over each icon in the tray in both dimensions
    for(int nRow=0; nRow<(rctTrayIcon.bottom-rctTrayIcon.top)/nIconHeight; nRow++)
    {
        for(int nCol=0; nCol<(rctTrayIcon.right-rctTrayIcon.left)/nIconWidth; nCol++)
        {
            SetCursorPos(rctTrayIcon.left + nCol * nIconWidth + 5,
                rctTrayIcon.top + nRow * nIconHeight + 5);
            Sleep(0);
        }
    }
    // Restore mouse position
    SetCursorPos(CursorPos.x, CursorPos.x);
    // Redraw tray window (to fix bug in multi-line tray area)
    RedrawWindow(hTrayWindow, NULL, 0, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW);
}

方式二:

#define _WIN32_IE 0x0500

#include <windows.h>
#include <CommCtrl.h>

struct TRAYDATA
{
  HWND hwnd;               
  UINT uID;               
  UINT uCallbackMessage;   
  DWORD Reserved[2];       
  HICON hIcon;               
};

void ShowTrayIcon(LPCWSTR lpwszIcon, BOOL bShow)
{
  HWND hWnd,hWndPaper;
  DWORD dwProcessId;
  DWORD dwButtonCount;
  HANDLE hProcess;
  LPVOID lpAddress;
  TBBUTTON tb;

  hWnd = FindWindow(TEXT("Shell_TrayWnd"), NULL);
  hWnd = FindWindowEx(hWnd, 0, TEXT("TrayNotifyWnd"), NULL);
  hWndPaper = FindWindowEx(hWnd, 0, TEXT("SysPager"), NULL);
  if(!hWndPaper)
    hWnd = FindWindowEx(hWnd, 0, TEXT("ToolbarWindow32"), NULL);
  else
    hWnd = FindWindowEx(hWndPaper, 0, TEXT("ToolbarWindow32"), NULL);

  GetWindowThreadProcessId(hWnd, &dwProcessId);
  hProcess = OpenProcess(PROCESS_ALL_ACCESS
    |PROCESS_VM_OPERATION
    |PROCESS_VM_READ
    |PROCESS_VM_WRITE,
    0,
    dwProcessId);
  lpAddress = VirtualAllocEx(hProcess,0, 0x4096, MEM_COMMIT, PAGE_READWRITE);
  dwButtonCount = SendMessage(hWnd, TB_BUTTONCOUNT, 0, 0);

  for(UINT i = 0 ; i < dwButtonCount - 1; i++)
  {
    SendMessage(hWnd, TB_GETBUTTON, i, (LPARAM)lpAddress);
    ReadProcessMemory(hProcess, lpAddress, &tb, sizeof(TBBUTTON), 0);

    if(tb.iString != -1)
    {
      TRAYDATA trayData;
      WCHAR wszBuff[MAX_PATH] = {0};
      WCHAR *pwsz = NULL;
      ReadProcessMemory(hProcess, (LPVOID)tb.iString, wszBuff, MAX_PATH, 0);
      ReadProcessMemory(hProcess, (LPVOID)tb.dwData, &trayData, sizeof(TRAYDATA), 0);
      pwsz = wcsstr(wszBuff, lpwszIcon);
      if (pwsz)
      {
        NOTIFYICONDATA nid;
        nid.cbSize = sizeof(NOTIFYICONDATA);
        nid.hWnd = trayData.hwnd;
        nid.uID = trayData.uID;
        nid.uFlags = NIF_STATE;
        nid.dwState = bShow?NIS_SHAREDICON:NIS_HIDDEN;
        nid.dwStateMask = NIS_HIDDEN;
        Shell_NotifyIcon(NIM_MODIFY, &nid);
      }
    }
  }
  VirtualFreeEx( hProcess, lpAddress, 0x4096, MEM_RELEASE);
  CloseHandle(hProcess);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
  ShowTrayIcon(L"音量", FALSE);  //隐藏音量图标
  //ShowTrayIcon(L"音量", TRUE);  //显示音量图标
  return 0;
}

转自:http://bbs.csdn.net/topics/390224391

其他:
XP/WIN7系统中删除已结束进程托盘图标的方法
http://blog.csdn.net/bzhxuexi/article/details/25238523

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值