public static Rectangle GetWindowRect(this IntPtr hWnd) { var rect = new User32Api.RectApi(); User32Api.GetWindowRect(hWnd, ref rect); return(rect.ToRectangle()); }
public static void ItemClick(this IntPtr treeViewHwnd, IntPtr itemHwnd, Size offSet) { uint vProcessId; User32Api.GetWindowThreadProcessId(treeViewHwnd, out vProcessId); var vProcess = Kernel32Api.OpenProcess( WindowsMessageApi.PROCESS_ALL_ACCESS, false, vProcessId ); var remoteBuffer = Kernel32Api.VirtualAllocEx(vProcess, IntPtr.Zero, (uint)Marshal.SizeOf(typeof(User32Api.RectApi)), WindowsMessageApi.MEM_COMMIT, WindowsMessageApi.PAGE_EXECUTE_READWRITE); try { var rc = new User32Api.RectApi { left = itemHwnd.ToInt32() }; var localBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(rc)); Marshal.StructureToPtr(rc, localBuffer, false); int vNumberOfBytes; Kernel32Api.WriteProcessMemory(vProcess, remoteBuffer, localBuffer, Marshal.SizeOf(typeof(User32Api.RectApi)), out vNumberOfBytes); User32Api.SendMessage(treeViewHwnd, WindowsMessageApi.TVM_SELECTITEM, WindowsMessageApi.TVGN_CARET, itemHwnd.ToInt32()); User32Api.SendMessage(treeViewHwnd, WindowsMessageApi.TVM_ENSUREVISIBLE, 0, itemHwnd.ToInt32()); User32Api.SendMessage(treeViewHwnd, WindowsMessageApi.TVM_GETITEMRECT, 1, remoteBuffer.ToInt32()); Kernel32Api.ReadProcessMemory(vProcess, remoteBuffer, localBuffer, Marshal.SizeOf(typeof(User32Api.RectApi)), out vNumberOfBytes); rc = (User32Api.RectApi)Marshal.PtrToStructure(localBuffer, typeof(User32Api.RectApi)); var pt = rc.ToRectangle().Center() + offSet; User32Api.SendMessage(treeViewHwnd, WindowsMessageApi.WM_LBUTTONDBLCLK, 0, (int)MAKELPARAM((uint)pt.X, (uint)pt.Y)); } finally { Kernel32Api.VirtualFreeEx(vProcess, remoteBuffer, (uint)Marshal.SizeOf(typeof(User32Api.RectApi)), WindowsMessageApi.MEM_FREE); Kernel32Api.CloseHandle(vProcess); } }
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 Rectangle ToRectangle(this User32Api.RectApi rect) { return(new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top)); }