public Image GetImage(RectangleF rect) { var adjustedRect = rect * Widget.LogicalPixelSize; //adjustedRect.Location += Control.Bounds.Location.ToEto(); var info = new Win32.MONITORINFOEX(); Win32.GetMonitorInfo(Control, ref info); adjustedRect.Location += info.rcMonitor.ToEto().Location; var oldDpiAwareness = Win32.SetThreadDpiAwarenessContextSafe(Win32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_v2); var realRect = Rectangle.Ceiling(adjustedRect); using (var screenBmp = new sd.Bitmap(realRect.Width, realRect.Height, sd.Imaging.PixelFormat.Format32bppRgb)) { using (var bmpGraphics = sd.Graphics.FromImage(screenBmp)) { bmpGraphics.CopyFromScreen(realRect.X, realRect.Y, 0, 0, realRect.Size.ToSD()); var bitmapSource = sw.Interop.Imaging.CreateBitmapSourceFromHBitmap( screenBmp.GetHbitmap(), IntPtr.Zero, sw.Int32Rect.Empty, sw.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); if (oldDpiAwareness != Win32.DPI_AWARENESS_CONTEXT.NONE) { Win32.SetThreadDpiAwarenessContextSafe(oldDpiAwareness); } return(new Bitmap(new BitmapHandler(bitmapSource))); } } }
public Image GetImage(RectangleF rect) { var bounds = Bounds; var realBounds = bounds; var hmonitor = Win32.MonitorFromPoint(bounds.Location.ToSDPoint(), 0); if (hmonitor != IntPtr.Zero) { // get actual monitor dimentions var oldDpiAwareness = Win32.SetThreadDpiAwarenessContextSafe(Win32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE); var info = new Win32.MONITORINFOEX(); Win32.GetMonitorInfo(new HandleRef(null, hmonitor), info); realBounds = info.rcMonitor.ToSD().ToEto(); if (oldDpiAwareness != Win32.DPI_AWARENESS_CONTEXT.NONE) { Win32.SetThreadDpiAwarenessContextSafe(oldDpiAwareness); } } var adjustedRect = rect; adjustedRect.Size *= (float)(realBounds.Width / bounds.Width); adjustedRect.Location += realBounds.Location; var realRect = Rectangle.Ceiling(adjustedRect); var screenBmp = new sd.Bitmap(realRect.Width, realRect.Height, sd.Imaging.PixelFormat.Format32bppRgb); using (var bmpGraphics = sd.Graphics.FromImage(screenBmp)) { bmpGraphics.CopyFromScreen(realRect.X, realRect.Y, 0, 0, realRect.Size.ToSD()); } return(new Bitmap(new BitmapHandler(screenBmp))); }
private void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam) { // MINMAXINFO structure Win32.MINMAXINFO mmi = (Win32.MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(Win32.MINMAXINFO)); // Get handle for nearest monitor to this window WindowInteropHelper wih = new WindowInteropHelper(this); IntPtr hMonitor = Win32.MonitorFromWindow(wih.Handle, Win32.MONITOR_DEFAULTTONEAREST); // Get monitor info Win32.MONITORINFOEX monitorInfo = new Win32.MONITORINFOEX(); monitorInfo.cbSize = Marshal.SizeOf(monitorInfo); Win32.GetMonitorInfo(new HandleRef(this, hMonitor), monitorInfo); // Get HwndSource HwndSource source = HwndSource.FromHwnd(wih.Handle); if (source == null) { // Should never be null throw new Exception("Cannot get HwndSource instance."); } if (source.CompositionTarget == null) { // Should never be null throw new Exception("Cannot get HwndTarget instance."); } // Get transformation matrix System.Windows.Media.Matrix matrix = source.CompositionTarget.TransformFromDevice; // Convert working area Win32.RECT workingArea = monitorInfo.rcWork; System.Windows.Point dpiIndependentSize = matrix.Transform(new System.Windows.Point( workingArea.Right - workingArea.Left, workingArea.Bottom - workingArea.Top )); // Convert minimum size System.Windows.Point dpiIndenpendentTrackingSize = matrix.Transform(new System.Windows.Point( this.MinWidth, this.MinHeight )); // Set the maximized size of the window mmi.ptMaxSize.x = (int)dpiIndependentSize.X; mmi.ptMaxSize.y = (int)dpiIndependentSize.Y; // Set the position of the maximized window mmi.ptMaxPosition.x = 0; mmi.ptMaxPosition.y = 0; // Set the minimum tracking size mmi.ptMinTrackSize.x = (int)dpiIndenpendentTrackingSize.X; mmi.ptMinTrackSize.y = (int)dpiIndenpendentTrackingSize.Y; Marshal.StructureToPtr(mmi, lParam, true); }
void adjust_maximized_client_rect(IntPtr window, Win32.RECT rect) { if (!maximized(window)) { return; } IntPtr monitor = Win32.MonitorFromWindow(window, Win32.MONITOR_DEFAULTTONULL); if (monitor == IntPtr.Zero) { return; } Win32.MONITORINFOEX monitor_info = new Win32.MONITORINFOEX(); if (!Win32.GetMonitorInfo(monitor, ref monitor_info)) { return; } // when maximized, make the client area fill just the monitor (without task bar) rect, // not the whole window rect which extends beyond the monitor. rect = monitor_info.WorkArea; }