实例介绍
【实例简介】
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace ListTrayBarInfo { public class SysTrayWnd { public struct TrayItemData { public int dwProcessID; public byte fsState; public byte fsStyle; public IntPtr hIcon; public IntPtr hProcess; public IntPtr hWnd; public int idBitmap; public int idCommand; public string lpProcImagePath; public string lpTrayToolTip; } public static IntPtr GetTrayWnd() { IntPtr handle = Win32.FindWindow("Shell_TrayWnd", null); handle = Win32.FindWindowEx(handle, IntPtr.Zero, "TrayNotifyWnd", null); handle = Win32.FindWindowEx(handle, IntPtr.Zero, "SysPager", null); handle = Win32.FindWindowEx(handle, IntPtr.Zero, "ToolbarWindow32", null); return handle; } public static TrayItemData[] GetTrayWndDetail() { SortedList<string, TrayItemData> stlTrayItems = new SortedList<string, TrayItemData>(); try { Win32.TBBUTTON tbButtonInfo = new Win32.TBBUTTON(); IntPtr hTrayWnd = IntPtr.Zero; IntPtr hTrayProcess = IntPtr.Zero; int iTrayProcessID = 0; int iAllocBaseAddress = 0; int iRet = 0; int iTrayItemCount = 0; hTrayWnd = GetTrayWnd(); Win32.GetWindowThreadProcessId(hTrayWnd, ref iTrayProcessID); hTrayProcess =Win32.OpenProcess( (uint)Win32.ProcessAccessFlags.All | (uint)Win32.ProcessAccessFlags.VMOperation | (uint)Win32.ProcessAccessFlags.VMRead | (uint)Win32.ProcessAccessFlags.VMWrite, 0, (uint)iTrayProcessID); iAllocBaseAddress = Win32.VirtualAllocEx(hTrayProcess, 0, Marshal.SizeOf(tbButtonInfo), Win32.MEM_COMMIT, Win32.PAGE_READWRITE); iTrayItemCount = Win32.SendMessage(hTrayWnd, Win32.TB_BUTTONCOUNT, 0, 0); for (int i = 0; i < iTrayItemCount; i ) { try { TrayItemData trayItem = new TrayItemData(); Win32.TRAYDATA trayData = new Win32.TRAYDATA(); int iOut = 0; int dwProcessID = 0; IntPtr hRelProcess = IntPtr.Zero; string strTrayToolTip = string.Empty; iRet = Win32.SendMessage(hTrayWnd, Win32.TB_GETBUTTON, i, iAllocBaseAddress); IntPtr hButtonInfo = Marshal.AllocHGlobal(Marshal.SizeOf(tbButtonInfo)); IntPtr hTrayData = Marshal.AllocHGlobal(Marshal.SizeOf(trayData)); iRet = Win32.ReadProcessMemory(hTrayProcess, iAllocBaseAddress, hButtonInfo, Marshal.SizeOf(tbButtonInfo), out iOut); Marshal.PtrToStructure(hButtonInfo, tbButtonInfo); iRet = Win32.ReadProcessMemory(hTrayProcess, tbButtonInfo.dwData, hTrayData, Marshal.SizeOf(trayData), out iOut); Marshal.PtrToStructure(hTrayData, trayData); byte[] bytTextData = new byte[1024]; iRet = Win32.ReadProcessMemory(hTrayProcess, tbButtonInfo.iString, bytTextData, 1024, out iOut); strTrayToolTip = Encoding.Unicode.GetString(bytTextData); if (!string.IsNullOrEmpty(strTrayToolTip)) { int iNullIndex = strTrayToolTip.IndexOf('\0'); strTrayToolTip = strTrayToolTip.Substring(0, iNullIndex); } Win32.GetWindowThreadProcessId(trayData.hwnd, ref dwProcessID); hRelProcess = Win32.OpenProcess((uint)Win32.ProcessAccessFlags.QueryInformation, 0, (uint)dwProcessID); StringBuilder sbProcImagePath = new StringBuilder(256); if (hRelProcess != IntPtr.Zero) { Win32.GetProcessImageFileName(hRelProcess, sbProcImagePath, sbProcImagePath.Capacity); } string strImageFilePath = string.Empty; if (sbProcImagePath.Length > 0) { int iDeviceIndex = sbProcImagePath.ToString().IndexOf("\\", "\\Device\\HarddiskVolume".Length); string strDevicePath = sbProcImagePath.ToString().Substring(0, iDeviceIndex); int iStartDisk = (int)'A'; while (iStartDisk <= (int)'Z') { StringBuilder sbWindowImagePath = new StringBuilder(256); iRet = Win32.QueryDosDevice(((char)iStartDisk).ToString() ":", sbWindowImagePath, sbWindowImagePath.Capacity); if (iRet != 0) { if (sbWindowImagePath.ToString() == strDevicePath) { strImageFilePath = ((char)iStartDisk).ToString() ":" sbProcImagePath.ToString().Replace(strDevicePath, ""); break; } } iStartDisk ; } } trayItem.dwProcessID = dwProcessID; trayItem.fsState = tbButtonInfo.fsState; trayItem.fsStyle = tbButtonInfo.fsStyle; trayItem.hIcon = trayData.hIcon; trayItem.hProcess = hRelProcess; trayItem.hWnd = trayData.hwnd; trayItem.idBitmap = tbButtonInfo.iBitmap; trayItem.idCommand = tbButtonInfo.idCommand; trayItem.lpProcImagePath = strImageFilePath; trayItem.lpTrayToolTip = strTrayToolTip; stlTrayItems[string.Format("{0:d8}", tbButtonInfo.idCommand)] = trayItem; } catch { continue; } } Win32.VirtualFreeEx(hTrayProcess, iAllocBaseAddress, Marshal.SizeOf(tbButtonInfo), Win32.MEM_RELEASE); Win32.CloseHandle(hTrayProcess); TrayItemData[] trayItems = new TrayItemData[stlTrayItems.Count]; stlTrayItems.Values.CopyTo(trayItems, 0); return trayItems; } catch (SEHException ex) { throw ex; } catch (Exception ex) { throw ex; } } public static void RefreshTrayWnd() { TrayItemData[] trayItems = GetTrayWndDetail(); IntPtr hTrayWnd=GetTrayWnd(); for (int i = trayItems.Length - 1; i >= 0; i--) { int iProcessExitCode = 0; Win32.GetExitCodeProcess(trayItems[i].hProcess, ref iProcessExitCode); if (iProcessExitCode != Win32.STILL_ACTIVE) { //通过隐藏图标来达到刷新的动作 int iRet = Win32.SendMessage(hTrayWnd, Win32.TB_HIDEBUTTON, trayItems[i].idCommand, 0); } } } } }
好例子网口号:伸出你的我的手 — 分享!
相关软件
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)