如何遍历系统托盘区的图标

 我来答
huanglenzhi
2015-04-25 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
采纳数:117538 获赞数:517183
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。

向TA提问 私信TA
展开全部

推荐你看下这个,C#里获取系统托盘信息,希望对你有帮助。

public sealed class NotifyHelper
   {
       private static int WM_TASKBARCREATED = RegisterWindowMessage("TaskbarCreated");
       private const int WM_USER = 0x400;
       private const int WM_TRAYMOUSEMESSAGE = 2048;
       private const int WM_MOUSEMOVE = 0x200;
       private const int WM_LBUTTONDOWN = 0x201;
       private const int WM_LBUTTONUP = 0x202;
       private const int WM_LBUTTONDBLCLK = 0x203;
       private const int WM_RBUTTONDOWN = 0x204;
       private const int WM_RBUTTONUP = 0x205;
       private const int WM_RBUTTONDBLCLK = 0x206;
       private const int WM_MBUTTONDOWN = 0x207;
       private const int WM_MBUTTONUP = 0x208;
       private const int WM_MBUTTONDBLCLK = 0x209;
       private const int NIN_BALLOONSHOW = 0x402;
       private const int NIN_BALLOONHIDE = 0x403;
       private const int NIN_BALLOONTIMEOUT = 0x404;
       private const int NIN_BALLOONUSERCLICK = 0x405;

       private const int READ_CONTROL = 0x20000;
       private const int STANDARD_RIGHTS_REQUIRED = 0xF0000;
       private const int STANDARD_RIGHTS_READ = READ_CONTROL;
       private const int STANDARD_RIGHTS_EXECUTE = READ_CONTROL;
       private const int STANDARD_RIGHTS_ALL = 0x1F0000;
       private const int STANDARD_RIGHTS_WRITE = READ_CONTROL;
       private const int SYNCHRONIZE = 0x100000;
       private const int PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xFFF;
       private const int PROCESS_TERMINATE = 0x1;

       private const int PROCESS_VM_OPERATION = 0x8;
       private const int PROCESS_VM_READ = 0x10;
       private const int PROCESS_VM_WRITE = 0x20;
       private const int MEM_RESERVE = 0x2000;
       private const int MEM_COMMIT = 0x1000;
       private const int MEM_RELEASE = 0x8000;
       private const int PAGE_READWRITE = 0x4;

       private const int TB_BUTTONCOUNT = (WM_USER + 24);
       private const int TB_HIDEBUTTON = (WM_USER + 4);
       private const int TB_GETBUTTON = (WM_USER + 23);
       private const int TB_GETBITMAP = (WM_USER + 44);
       private const int TB_DELETEBUTTON = (WM_USER + 22);
       private const int TB_ADDBUTTONS = (WM_USER + 20);
       private const int TB_INSERTBUTTON = (WM_USER + 21);
       private const int TB_ISBUTTONHIDDEN = (WM_USER + 12);
       private const int ILD_NORMAL = 0x0;

       private const int TPM_NONOTIFY = 0x80;

       #region Win32 API 引用

       [DllImport("user32.dll", CharSet = CharSet.Auto)]
       public static extern IntPtr PostMessage(HandleRef hwnd, int msg, int wparam, int lparam);

       [DllImport("user32.dll", CharSet = CharSet.Auto)]
       public static extern bool PostMessage(HandleRef hwnd, int msg, IntPtr wparam, IntPtr lparam);

       [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
       private static extern bool SetForegroundWindow(HandleRef hWnd);

       [DllImport("user32.dll", CharSet = CharSet.Auto)]
       private static extern int RegisterWindowMessage(string msg);

       [DllImport("kernel32", EntryPoint = "OpenProcess")]
       private static extern IntPtr OpenProcess(
           int dwDesiredAccess,
           IntPtr bInheritHandle,
           IntPtr dwProcessId
           );
       [DllImport("kernel32", EntryPoint = "CloseHandle")]
       private static extern int CloseHandle(
           IntPtr hObject
           );
       [DllImport("user32", EntryPoint = "GetWindowThreadProcessId")]
       private static extern IntPtr GetWindowThreadProcessId(
           IntPtr hwnd,
           ref IntPtr lpdwProcessId
           );
       [DllImport("user32", EntryPoint = "FindWindow")]
       private static extern IntPtr FindWindow(
           string lpClassName,
           string lpWindowName
           );
       [DllImport("user32", EntryPoint = "FindWindowEx")]
       private static extern IntPtr FindWindowEx(
           IntPtr hWnd1,
           IntPtr hWnd2,
           string lpsz1,
           string lpsz2
           );
       [DllImport("user32", EntryPoint = "SendMessage")]
       private static extern int SendMessage(
           IntPtr hwnd,
           int wMsg,
           int wParam,
           int lParam
           );
       [DllImport("user32", EntryPoint = "SendMessage")]
       private static extern int SendMessage(
           IntPtr hwnd,
           int wMsg,
           int wParam,
           IntPtr lParam
           );
       [DllImport("kernel32", EntryPoint = "ReadProcessMemory")]
       private static extern int ReadProcessMemory(
           IntPtr hProcess,
           IntPtr lpBaseAddress,
           ref IntPtr lpBuffer,
           int nSize,
           int lpNumberOfBytesWritten
           );
       [DllImport("kernel32", EntryPoint = "ReadProcessMemory")]
       private static extern int ReadProcessMemory(
           IntPtr hProcess,
           IntPtr lpBaseAddress,
           byte[] lpBuffer,
           int nSize,
           int lpNumberOfBytesWritten
           );
       [DllImport("kernel32", EntryPoint = "WriteProcessMemory")]
       private static extern int WriteProcessMemory(
           IntPtr hProcess,
           ref int lpBaseAddress,
           ref int lpBuffer,
           int nSize,
           ref int lpNumberOfBytesWritten
           );
       [DllImport("kernel32", EntryPoint = "VirtualAllocEx")]
       private static extern IntPtr VirtualAllocEx(
           IntPtr hProcess,
           int lpAddress,
           int dwSize,
           int flAllocationType,
           int flProtect
           );
       [DllImport("kernel32", EntryPoint = "VirtualFreeEx")]
       private static extern int VirtualFreeEx(
           IntPtr hProcess,
           IntPtr lpAddress,
           int dwSize,
           int dwFreeType
           );
       #endregion

       public static IntPtr FindNotifyIcon(string TipTitle)
       {
           if (TipTitle.Length == 0) return IntPtr.Zero;
           IntPtr pid = IntPtr.Zero;
           IntPtr ipHandle = IntPtr.Zero; //图标句柄
           IntPtr lTextAdr = IntPtr.Zero; //文本内存地址
           IntPtr ipTemp = FindWindow("Shell_TrayWnd", null);
           //找到托盤
           ipTemp = FindWindowEx(ipTemp, IntPtr.Zero, "TrayNotifyWnd", null);
           ipTemp = FindWindowEx(ipTemp, IntPtr.Zero, "SysPager", null);
           IntPtr ipTray = FindWindowEx(ipTemp, IntPtr.Zero, "ToolbarWindow32", null);

           GetWindowThreadProcessId(ipTray, ref pid);
           if (pid.Equals(0)) return ipHandle;

           IntPtr hProcess = OpenProcess(PROCESS_ALL_ACCESS | PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, IntPtr.Zero, pid);
           IntPtr lAddress = VirtualAllocEx(hProcess, 0, 4096, MEM_COMMIT, PAGE_READWRITE);

           //得到图标个数
           int lButton = SendMessage(ipTray, TB_BUTTONCOUNT, 0, 0);
           for (int i = 0; i < lButton; i++)
           {
               SendMessage(ipTray, TB_GETBUTTON, i, lAddress);
               //读文本地址
               ReadProcessMemory(hProcess, (IntPtr)(lAddress.ToInt32() + 16), ref lTextAdr, 4, 0);
               if (!lTextAdr.Equals(-1))
               {
                   byte[] buff = new byte[1024];
                   //读文本
                   ReadProcessMemory(hProcess, lTextAdr, buff, 1024, 0);
                   string title = System.Text.ASCIIEncoding.Unicode.GetString(buff);
                   // 从字符0处截断
                   int nullindex = title.IndexOf("\0");
                   if (nullindex > 0)
                   {
                       title = title.Substring(0, nullindex);
                   }
                   //ReadProcessMemory(hProcess, lAddress, ref ipButtonID, 4, 0);
                   //判断是不是要找的图标
                   if (title.Equals(TipTitle))
                   {
                       IntPtr ipHandleAdr = IntPtr.Zero;
                       //读句柄地址
                       ReadProcessMemory(hProcess, (IntPtr)(lAddress.ToInt32() + 12), ref ipHandleAdr, 4, 0);
                       ReadProcessMemory(hProcess, ipHandleAdr, ref ipHandle, 4, 0);
                       break;
                   }
               }
           }
           VirtualFreeEx(hProcess, lAddress, 4096, MEM_RELEASE);
           CloseHandle(hProcess);
           return ipHandle;
       }

       public static void UpdateNotify()
       {
           IntPtr pid = IntPtr.Zero;
           IntPtr ipHandle = IntPtr.Zero; //图标句柄
           IntPtr lTextAdr = IntPtr.Zero; //文本内存地址
           IntPtr ipTemp = FindWindow("Shell_TrayWnd", null);
           //找到托盤

本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
AiPPT
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图... 点击进入详情页
本回答由AiPPT提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式