跨进程控制SysTreeView32树状图控件的问题,啥也不说了,直接上代码:
public partial class Form1 : Form { // 定义常量 public const int WM_LBUTTONDBLCLK = 0x020B; // 左键双击消息 public const int WM_RBUTTONDOWN = 0x0204; // 右键按下消息 public const int WM_RBUTTONUP = 0x0205; // 右键松开消息 public const int TV_FIRST = 0x1100; public const int TVM_GETCOUNT = TV_FIRST + 5; public const int TVM_GETNEXTITEM = TV_FIRST + 10; public const int TVM_GETITEMA = TV_FIRST + 12; public const int TVM_GETITEMW = TV_FIRST + 62; private const int TVM_GETITEM = 0x0000110c; private const int TVM_SELECTITEM = 0x0000110b; private const int TVM_GETTEXTCOLOR = 0x0000110b; public const int TVM_EXPAND = 0x0131; public const int WM_GETTEXT = 0x000D; public const int TVGN_ROOT = 0x0000; public const int TVGN_NEXT = 0x0001; public const int TVGN_PREVIOUS = 0x0002; public const int TVGN_PARENT = 0x0003; public const int TVGN_CHILD = 0x0004; public const int TVGN_FIRSTVISIBLE = 0x0005; public const int TVGN_NEXTVISIBLE = 0x0006; public const int TVGN_PREVIOUSVISIBLE = 0x0007; public const int TVGN_DROPHILITE = 0x0008; public const int TVGN_CARET = 0x0009; public const int TVGN_LASTVISIBLE = 0x000A; public const int TVIF_TEXT = 0x0001; public const int TVIF_IMAGE = 0x0002; public const int TVIF_PARAM = 0x0004; public const int TVIF_STATE = 0x0008; public const int TVIF_HANDLE = 0x0010; public const int TVIF_SELECTEDIMAGE = 0x0020; public const int TVIF_CHILDREN = 0x0040; public const int TVIF_INTEGRAL = 0x0080; [DllImport("User32.DLL")] public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); [DllImport("user32.dll", EntryPoint = "SendMessageA")] private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, StringBuilder lParam); [DllImport("user32.dll", EntryPoint = "SendMessageA")] private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, ref Rectangle lParam); [DllImport("kernel32.dll")] public static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId); public const uint MEM_COMMIT = 0x1000; public const uint MEM_RELEASE = 0x8000; public const uint MEM_RESERVE = 0x2000; public const uint PAGE_READWRITE = 4; public const uint PROCESS_VM_OPERATION = 0x0008; public const uint PROCESS_VM_READ = 0x0010; public const uint PROCESS_VM_WRITE = 0x0020; [DllImport("kernel32.dll")] public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect); [DllImport("kernel32.dll")] public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint dwFreeType); [DllImport("kernel32.dll")] public static extern bool CloseHandle(IntPtr handle); [DllImport("kernel32.dll")] public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead); [DllImport("kernel32.dll")] public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead); [DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint dwProcessId); public Form1() { InitializeComponent(); } private List<string> strings = new List<string>(); private void button1_Click(object sender, EventArgs e) { WindowAPI.WindowInfo allDesktopWindowsFirst = WindowAPI.GetWindowsControlFirst("合区工具 1.1", null); //启动合区工具 WindowAPI.WindowInfo ChildWindowsInfo2 = WindowAPI.GetChildWindowsControlFirst (allDesktopWindowsFirst.intPtr, "添加合区服", "Button"); WindowAPI.PostMessage(ChildWindowsInfo2.intPtr, WindowAPI.CLICKINT, 0, 0); Thread.Sleep(300); WindowAPI.PostMessage(ChildWindowsInfo2.intPtr, WindowAPI.CLICKINT, 0, 0); Thread.Sleep(300); //设置SysTreeView32为D:\系统测试光芒231205SS WindowAPI.WindowInfo windowInfoByFuncAndTimeOut = WindowAPI.GetWindowsControlFirst("浏览文件夹", null); Thread.Sleep(300); WindowAPI.WindowInfo hwndTreeView = WindowAPI.GetChildWindowsControlFirst(windowInfoByFuncAndTimeOut.intPtr, "", "SysTreeView32"); string selectpath = @"E:\迅雷云盘\Access2003"; var pathstr = selectpath.Split('\\'); List<string> AOutput = new List<string>(); var lists = GetTreeViewText((IntPtr)hwndTreeView.intPtr, ref AOutput); //展开所有节点 foreach (var l in lists) { int result = WindowAPI.PostMessage(l, WM_LBUTTONDBLCLK, 0, 0); } //查询所有节点 lists = GetTreeViewText((IntPtr)hwndTreeView.intPtr, ref AOutput); foreach (var l in lists) { } var selnode = lists[40]; TreeView_SelectNode(hwndTreeView.intPtr, selnode); WindowAPI.WindowInfo WindowInfo2 = WindowAPI.GetChildWindowsControlFirst(windowInfoByFuncAndTimeOut.intPtr, "确定", "Button"); WindowAPI.PostMessage(WindowInfo2.intPtr, WindowAPI.CLICKINT, 0, 0); Thread.Sleep(300); WindowAPI.PostMessage(WindowInfo2.intPtr, WindowAPI.CLICKINT, 0, 0); Thread.Sleep(300); } public static bool TreeView_SelectNode(IntPtr TreeViewHwnd, IntPtr ItemHwnd) { int result = SendMessage(TreeViewHwnd, TVM_SELECTITEM, TVGN_CARET, (int)ItemHwnd); if (result == 0) { return false; } else { return true; } } public static uint TreeView_GetCount(IntPtr hwnd) { return (uint)SendMessage(hwnd, TVM_GETCOUNT, 0, 0); } public static IntPtr TreeView_GetNextItem(IntPtr hwnd, IntPtr hitem, int code) { return (IntPtr)SendMessage(hwnd, TVM_GETNEXTITEM, code, (int)hitem); } public static IntPtr TreeView_GetRoot(IntPtr hwnd) { return TreeView_GetNextItem(hwnd, IntPtr.Zero, TVGN_ROOT); } public static IntPtr TreeView_GetChild(IntPtr hwnd, IntPtr hitem) { return TreeView_GetNextItem(hwnd, hitem, TVGN_CHILD); } public static IntPtr TreeView_GetParent(IntPtr hwnd, IntPtr hitem) { return TreeView_GetNextItem(hwnd, hitem, TVGN_PARENT); } public static IntPtr TreeView_GetNextSibling(IntPtr hwnd, IntPtr hitem) { return TreeView_GetNextItem(hwnd, hitem, TVGN_NEXT); } public static IntPtr TreeNodeGetNext(IntPtr AHandle, IntPtr ATreeItem) { if (AHandle == IntPtr.Zero || ATreeItem == IntPtr.Zero) return IntPtr.Zero; IntPtr result = TreeView_GetChild(AHandle, ATreeItem); if (result == IntPtr.Zero) result = TreeView_GetNextSibling(AHandle, ATreeItem); else return result; IntPtr vParentID = ATreeItem; while (result == IntPtr.Zero && vParentID != IntPtr.Zero) { vParentID = TreeView_GetParent(AHandle, vParentID); result = TreeView_GetNextSibling(AHandle, vParentID); } return result; } public static List<IntPtr> GetTreeViewText(IntPtr AHandle,ref List<string> AOutput) { List<IntPtr> list = new List<IntPtr>(); if (AOutput == null) return list; uint vProcessId; GetWindowThreadProcessId(AHandle, out vProcessId); IntPtr vProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, false, vProcessId); IntPtr vPointer = VirtualAllocEx(vProcess, IntPtr.Zero, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); try { uint vItemCount = TreeView_GetCount(AHandle); IntPtr vTreeItem = TreeView_GetRoot(AHandle); for (int i = 0; i < vItemCount; i++) { list.Add(vTreeItem); byte[] vBuffer = new byte[256]; TVITEM[] vItem = new TVITEM[1]; vItem[0] = new TVITEM(); vItem[0].mask = TVIF_TEXT; vItem[0].hItem = vTreeItem; vItem[0].pszText = (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(TVITEM))); vItem[0].cchTextMax = vBuffer.Length; uint vNumberOfBytesRead = 0; WriteProcessMemory(vProcess, vPointer, Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0), Marshal.SizeOf(typeof(TVITEM)), ref vNumberOfBytesRead); SendMessage(vTreeItem, TVM_GETITEMA, 0, (int)vPointer); ReadProcessMemory(vProcess, (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(TVITEM))), Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0), vBuffer.Length, ref vNumberOfBytesRead); var str = Marshal.PtrToStringAnsi( Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0)); vTreeItem = TreeNodeGetNext(AHandle, vTreeItem); } return list; } finally { VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE); CloseHandle(vProcess); } } private void button2_Click(object sender, EventArgs e) { WindowAPI.WindowInfo allDesktopWindowsFirst = WindowAPI.GetWindowsControlFirst("合区工具 1.1", null); //启动合区工具 WindowAPI.WindowInfo ChildWindowsInfo2 = WindowAPI.GetChildWindowsControlFirst (allDesktopWindowsFirst.intPtr, "添加合区服", "Button"); WindowAPI.PostMessage(ChildWindowsInfo2.intPtr, WindowAPI.CLICKINT, 0, 0); Thread.Sleep(300); WindowAPI.PostMessage(ChildWindowsInfo2.intPtr, WindowAPI.CLICKINT, 0, 0); Thread.Sleep(300); //设置SysTreeView32为D:\系统测试光芒231205SS WindowAPI.WindowInfo windowInfoByFuncAndTimeOut = WindowAPI.GetWindowsControlFirst("浏览文件夹", null); Thread.Sleep(300); WindowAPI.WindowInfo hwndTreeView = WindowAPI.GetChildWindowsControlFirst(windowInfoByFuncAndTimeOut.intPtr, "", "SysTreeView32"); //获取TreeView的左上坐标点 } }