Exemple #1
0
        //获取托盘图标列表
        public static List <WindowInfo> GetIconList()
        {
            List <WindowInfo> iconList = new List <WindowInfo>();

            try
            {
                IntPtr pid      = IntPtr.Zero;
                IntPtr ipHandle = IntPtr.Zero; //图标句柄
                IntPtr lTextAdr = IntPtr.Zero; //文本内存地址

                IntPtr ipTray = TrayToolbarWindow32();

                Win32API.GetWindowThreadProcessId(ipTray, ref pid);
                if (pid.Equals(0))
                {
                    return(iconList);
                }

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

                //得到图标个数
                int lButton = Win32API.SendMessage(ipTray, Win32API.TB_BUTTONCOUNT, 0, 0);

                for (int i = 0; i < lButton; i++)
                {
                    Win32API.SendMessage(ipTray, Win32API.TB_GETBUTTON, i, lAddress);

                    //读文本地址
                    Win32API.ReadProcessMemory(hProcess, (IntPtr)(lAddress.ToInt32() + 16), ref lTextAdr, 4, 0);

                    if (!lTextAdr.Equals(-1))
                    {
                        byte[] buff = new byte[1024];

                        Win32API.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);
                        }

                        IntPtr ipHandleAdr = IntPtr.Zero;

                        //读句柄地址
                        Win32API.ReadProcessMemory(hProcess, (IntPtr)(lAddress.ToInt32() + 12), ref ipHandleAdr, 4, 0);
                        Win32API.ReadProcessMemory(hProcess, ipHandleAdr, ref ipHandle, 4, 0);

                        if (title.Replace("\0", "") == "")
                        {
                            continue;                               //不加载空项
                        }
                        iconList.Add(new WindowInfo(title, ipHandle));
                    }
                }
                Win32API.VirtualFreeEx(hProcess, lAddress, 4096, Win32API.MEM_RELEASE);
                Win32API.CloseHandle(hProcess);
            }
            catch (Exception ex)
            {
                LogManage.WriteLog(typeof(WindowHide), ex);
            }
            return(iconList);
        }
Exemple #2
0
        //显示/隐藏单个系统托盘图标,由参数caption指定图标
        public static void SetTrayIconVisible(string caption, bool isShow)
        {
            IntPtr vHandle    = TrayToolbarWindow32();
            int    vCount     = Win32API.SendMessage(vHandle, Win32API.TB_BUTTONCOUNT, 0, 0);
            IntPtr vProcessId = IntPtr.Zero;

            Win32API.GetWindowThreadProcessId(vHandle, ref vProcessId);
            IntPtr vProcess = Win32API.OpenProcess(Win32API.PROCESS_VM_OPERATION | Win32API.PROCESS_VM_READ |
                                                   Win32API.PROCESS_VM_WRITE, IntPtr.Zero, vProcessId);
            IntPtr vPointer = Win32API.VirtualAllocEx(vProcess, (int)IntPtr.Zero, 0x1000,
                                                      Win32API.MEM_RESERVE | Win32API.MEM_COMMIT, Win32API.PAGE_READWRITE);

            char[] vBuffer            = new char[256];
            IntPtr pp                 = Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0);
            uint   vNumberOfBytesRead = 0;

            try
            {
                for (int i = 0; i < vCount; i++)
                {
                    Win32API.SendMessage(vHandle, Win32API.TB_GETBUTTONTEXT, i, vPointer.ToInt32());

                    Win32API.ReadProcessMemoryEx(vProcess, vPointer,
                                                 Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0),
                                                 vBuffer.Length * sizeof(char), ref vNumberOfBytesRead);

                    int l = 0;
                    for (int j = 0; j < vBuffer.Length; j++)
                    {
                        if (vBuffer[j] == (char)0)
                        {
                            l = j;
                            break;
                        }
                    }
                    string s = new string(vBuffer, 0, l);

                    var realC = s.IndexOf(caption);
                    if (realC >= 0)
                    {
                        if (isShow)
                        {
                            Win32API.SendMessage(vHandle, Win32API.TB_HIDEBUTTON, i, 0);
                        }
                        else
                        {
                            Win32API.SendMessage(vHandle, Win32API.TB_HIDEBUTTON, i, 1);
                            //MainWindowIsShow = true;
                        }
                    }
                    list.Add(s);
                }
            }
            catch (Exception ex)
            {
                LogManage.WriteLog(typeof(WindowHide), ex);
            }
            finally
            {
                Win32API.VirtualFreeEx(vProcess, vPointer, 0, Win32API.MEM_RELEASE);
                Win32API.CloseHandle(vProcess);
            }
        }