private void ResizeWindow(IntPtr hwnd, int x, int y, bool relativeOffsets) { System.Runtime.InteropServices.HandleRef href = new System.Runtime.InteropServices.HandleRef(null, User32.GetAncestor(hwnd, User32.GA_FlagEnum.GA_ROOT)); // Get current width & height Microsoft.Test.Win32.NativeStructs.RECT parentRect = GetWindowRect(href.Handle); Microsoft.Test.Win32.NativeStructs.RECT rect = GetWindowRect(hwnd); int dx = x + parentRect.Width - rect.Width; int dy = y + parentRect.Height - rect.Height; if (relativeOffsets) { dx = parentRect.right + x; dy = parentRect.bottom + y; } if (dx == rect.left && dy == rect.top) { return; } bool success = Microsoft.Test.Win32.NativeMethods.MoveWindow(href, parentRect.left, parentRect.top, dx, dy, true); Microsoft.Test.Threading.DispatcherHelper.DoEvents(100); if (!success) { throw new System.Runtime.InteropServices.ExternalException("Call to Win32 API ('MoveWindow') failed "); } }
private static Bitmap CreateBitmapFromWindowHandle(IntPtr windowHandle, Microsoft.Test.Win32.NativeStructs.RECT r) { WaitForCompleteRender(); //System.Diagnostics.Trace.WriteLine(String.Format( // "Capturing bitmap for window in rectangle [{0};{1} - {2};{3}]", // r.left, r.top, r.right, r.bottom)); GlobalLog.LogStatus(String.Format( "Capturing bitmap for window in rectangle [{0};{1} - {2};{3}]", r.left, r.top, r.right, r.bottom)); new SecurityPermission(PermissionState.Unrestricted).Assert(); int cx = r.Width; int cy = r.Height; if (cx <= 0) { throw new Exception("Window to create bitmap for has <= 0 width."); } if (cy <= 0) { throw new Exception("Window to create bitmap for has <= 0 height."); } IntPtr sourceDC = NativeMethods.GetDC(new HandleRef(new object(), (IntPtr)null)); Microsoft.Test.Win32.NativeStructs.POINT topLeft = new Microsoft.Test.Win32.NativeStructs.POINT(0, 0); //Get the screen co-ordinates of the client origin. NativeMethods.ClientToScreen(windowHandle, ref topLeft); if ((int)sourceDC == 0) { string message = String.Format( "Unable to retrieve device context for window handle {0}.", (int)windowHandle); throw new Exception(message); } try { Bitmap bitmap = new Bitmap(cx, cy, System.Drawing.Imaging.PixelFormat.Format24bppRgb); Graphics graphics = Graphics.FromImage(bitmap); IntPtr destination = graphics.GetHdc(); const int SRCCOPY = 0x00CC0020; NativeMethods.SafeBitBlt(destination, 0, 0, cx, cy, sourceDC, topLeft.x, topLeft.y, SRCCOPY); graphics.ReleaseHdc(destination); graphics.Dispose(); return(bitmap); } finally { // Win32.ReleaseDC(windowHandle, sourceDC); NativeMethods.ReleaseDC(new HandleRef(new object(), windowHandle), new HandleRef(new object(), sourceDC)); } }
private Microsoft.Test.Win32.NativeStructs.RECT GetWindowRect(IntPtr hwnd) { System.Runtime.InteropServices.HandleRef href = new System.Runtime.InteropServices.HandleRef(null, hwnd); Microsoft.Test.Win32.NativeStructs.RECT retval = new Microsoft.Test.Win32.NativeStructs.RECT(); bool success = Microsoft.Test.Win32.NativeMethods.GetWindowRect(href, ref retval); if (!success) { throw new System.Runtime.InteropServices.ExternalException("Call to Win32 API ('GetWindowRect') failed "); } return(retval); }
/// <summary>Creates a bitmap with the image a user sees for a window.</summary> /// <param name="windowHandle">Handle to the window.</param> /// <returns>The created bitmap.</returns> public static Bitmap CreateBitmapFromWindowHandle(IntPtr windowHandle) { new SecurityPermission(PermissionState.Unrestricted).Assert(); Microsoft.Test.Win32.NativeStructs.RECT r = new Microsoft.Test.Win32.NativeStructs.RECT(0, 0, 0, 0); //Win32.GetClientRect(windowHandle, ref r); NativeMethods.GetClientRect(new HandleRef(new object(), windowHandle), ref r); if (r.Width == 0) { GlobalLog.LogDebug("Unable to retrieve client rectangle for window."); GlobalLog.LogDebug("The whole window will be used instead."); //Win32.GetWindowRect(windowHandle, ref r); NativeMethods.GetWindowRect(new HandleRef(new object(), windowHandle), ref r); if (r.Width == 0) { throw new Exception("The window refuses to provide width."); } } return(CreateBitmapFromWindowHandle(windowHandle, r)); }