public static Bitmap Capture(this IntPtr hWnd) { var hscrdc = User32Api.GetWindowDC(hWnd); var windowRect = new User32Api.RectApi(); User32Api.GetWindowRect(hWnd, ref windowRect); var width = windowRect.right - windowRect.left; var height = windowRect.bottom - windowRect.top; var hbitmap = Gdi32Api.CreateCompatibleBitmap(hscrdc, width, height); var hmemdc = Gdi32Api.CreateCompatibleDC(hscrdc); Gdi32Api.SelectObject(hmemdc, hbitmap); User32Api.PrintWindow(hWnd, hmemdc, 0); var bmp = Image.FromHbitmap(hbitmap); Gdi32Api.DeleteDC(hscrdc); //删除用过的对象 Gdi32Api.DeleteDC(hmemdc); //删除用过的对象 GC.Collect(); return(bmp); }
public static void MouseLClick(this Point pt) { var myMinput = new User32Api.MouseInputApi { dx = pt.X * (65355 / Screen.PrimaryScreen.Bounds.Width), dy = pt.Y * (65355 / Screen.PrimaryScreen.Bounds.Height), Mousedata = 0, dwFlag = User32Api.MouseEventAbsolute | User32Api.MouseEventMove | User32Api.MouseEventLeftDown | User32Api.MouseEventLeftUp, time = 0 }; var myInput = new[] { new User32Api.Input { type = 0, mi = myMinput } }; if (User32Api.SendInput((uint)myInput.Length, myInput, Marshal.SizeOf(myInput[0].GetType())) == 0) { throw new Exception(string.Format("模拟鼠标点击失败!X:{0} Y: {1}", pt.X, pt.Y)); } }
//获取根节点 public static IntPtr GetRootItem(this IntPtr treeViewHwnd) { return(User32Api.SendMessage(treeViewHwnd, WindowsMessageApi.TVM_GETNEXTITEM, WindowsMessageApi.TVGN_ROOT, 0)); }
//获取下个子节点 public static IntPtr GetNextItem(this IntPtr treeViewHwnd, IntPtr prevItemHwnd) { return(User32Api.SendMessage(treeViewHwnd, WindowsMessageApi.TVM_GETNEXTITEM, WindowsMessageApi.TVGN_NEXT, prevItemHwnd.ToInt32())); }
public static string GetItemText(this IntPtr treeViewHwnd, IntPtr itemHwnd) { var result = new StringBuilder(1024); uint vProcessId; User32Api.GetWindowThreadProcessId(treeViewHwnd, out vProcessId); var vProcess = Kernel32Api.OpenProcess( WindowsMessageApi.PROCESS_VM_OPERATION | WindowsMessageApi.PROCESS_VM_READ | WindowsMessageApi.PROCESS_VM_WRITE, false, vProcessId ); var pStrBufferMemory = Kernel32Api.VirtualAllocEx(vProcess, IntPtr.Zero, 1024, WindowsMessageApi.MEM_COMMIT, WindowsMessageApi.PAGE_READWRITE); var remoteBuffer = Kernel32Api.VirtualAllocEx(vProcess, IntPtr.Zero, (uint)Marshal.SizeOf(typeof(User32Api.TVITEM)), WindowsMessageApi.MEM_COMMIT, WindowsMessageApi.PAGE_EXECUTE_READWRITE); try { var tvItem = new User32Api.TVITEM { mask = WindowsMessageApi.TVIF_TEXT, hItem = itemHwnd, pszText = pStrBufferMemory, cchTextMax = 1024 }; var localBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(tvItem)); Marshal.StructureToPtr(tvItem, localBuffer, false); int vNumberOfBytesWrite; Kernel32Api.WriteProcessMemory( vProcess, remoteBuffer, localBuffer, Marshal.SizeOf(typeof(User32Api.TVITEM)), out vNumberOfBytesWrite); User32Api.SendMessage(treeViewHwnd, WindowsMessageApi.TVM_GETITEM, 0, remoteBuffer.ToInt32()); int vNumberOfBytesRead; Kernel32Api.ReadProcessMemory( vProcess, pStrBufferMemory, result, 1024, out vNumberOfBytesRead ); } finally { Kernel32Api.VirtualFreeEx(vProcess, pStrBufferMemory, 0, WindowsMessageApi.MEM_RELEASE); Kernel32Api.VirtualFreeEx(vProcess, remoteBuffer, 0, WindowsMessageApi.MEM_RELEASE); Kernel32Api.CloseHandle(vProcess); } return(result.ToString()); }
public static int GetWindowTextLength(this IntPtr hWnd) { return(User32Api.GetWindowTextLength(hWnd)); }
public static IntPtr SetWindowText(this IntPtr hWnd, string text) { User32Api.SendMessage(hWnd, WindowsMessageApi.WM_SETTEXT, 0, text); return(hWnd); }
public static bool SetForeground(this IntPtr hWnd) { return(User32Api.SetForegroundWindow(hWnd)); }
public static IntPtr FindWindow(this string titleWnd, string classWnd) { return(User32Api.FindWindow(classWnd, titleWnd)); }
public static IntPtr FindWindow(this string titleWnd) { return(User32Api.FindWindow(null, titleWnd)); }