public static IntPtr GetThumbnail(IntPtr hWnd, System.Windows.UIElement targetElement) { Debug.WriteLine($"{hWnd} {targetElement}"); DwmRegisterThumbnail(new WindowInteropHelper(Window.GetWindow(targetElement)).Handle, hWnd, out var thumbHandle); var targetElementPoint = MainTools.GetDpiScaledGlobalControlPosition(targetElement); Point targetElementOppositePoint; if (targetElement.GetType().IsAssignableFrom(typeof(System.Windows.Controls.Control))) { var targetControl = (targetElement as System.Windows.Controls.Control); targetElementOppositePoint = new Point(targetElementPoint.X + targetControl.ActualWidth, targetElementPoint.Y + targetControl.ActualHeight); } else { targetElementOppositePoint = new Point(targetElementPoint.X, targetElementPoint.Y); } var targetRect = new Rect(targetElementPoint.X, targetElementPoint.Y, (int)(targetElementOppositePoint.X), (int)(targetElementOppositePoint.Y)); DwmQueryThumbnailSourceSize(thumbHandle, out var size); var props = new DwmThumbnailProperties { fVisible = true, dwFlags = DwmTnpVisible | DwmTnpRectdestination | DwmTnpOpacity, opacity = 255, rcDestination = new WinApi.Rect(0, 0, 100, 100) }; DwmUpdateThumbnailProperties(thumbHandle, ref props); return(thumbHandle); }
/// <summary> /// Updates the thumbnail's display settings. /// </summary> /// <param name="destination">Drawing region on destination window.</param> /// <param name="source">Origin region from source window.</param> /// <param name="opacity">Opacity. 0 is transparent, 255 opaque.</param> /// <param name="visible">Visibility flag.</param> /// <param name="onlyClientArea"> /// If true, only the client area of the window will be rendered. Otherwise, the /// borders will be be rendered as well. /// </param> public void Update(Rectangle destination, Rectangle source, byte opacity, bool visible, bool onlyClientArea) { if (source.Width < 1 || source.Height < 1) { throw new ArgumentException(ExceptionMessages.DwmThumbnailSourceInvalid); } var prop = new DwmThumbnailProperties { rcDestination = new Rect(destination), rcSource = new Rect(source), opacity = opacity, fVisible = visible, fSourceClientAreaOnly = onlyClientArea, dwFlags = DwmThumbnailFlags.RectDestination | DwmThumbnailFlags.RectSource | DwmThumbnailFlags.Opacity | DwmThumbnailFlags.Visible | DwmThumbnailFlags.SourceClientAreaOnly }; if (DwmMethods.DwmUpdateThumbnailProperties(handle, ref prop) != 0) { throw new DwmCompositionException(ExceptionMessages.DwmThumbnailUpdateFailure); } _destination = destination; _opacity = opacity; _visible = visible; _clientArea = ShowOnlyClientArea; }
private void UpdateThumbnail() { DwmThumbnailProperties props = new DwmThumbnailProperties(); props.Flags = DwmThumbnailFlags.RectDestination | DwmThumbnailFlags.Opacity | DwmThumbnailFlags.Visible | DwmThumbnailFlags.SourceClientAreaOnly; props.SourceClientAreaOnly = this.ClientAreaOnly; props.Opacity = (byte)(this.Opacity * (double)byte.MaxValue); props.Visible = this.IsVisible; System.Windows.Point point1 = this.Destination.TranslatePoint(new System.Windows.Point(0.0, 0.0), (UIElement)this.window); System.Windows.Point point2 = this.Destination.TranslatePoint(new System.Windows.Point(this.Destination.ActualWidth, this.Destination.ActualHeight), (UIElement)this.window); props.rcDestination = new Advent.Common.Interop.RECT((int)point1.X, (int)point1.Y, (int)point2.X, (int)point2.Y); Marshal.ThrowExceptionForHR(Advent.Common.Interop.NativeMethods.DwmUpdateThumbnailProperties(this.thumbnailHandle, ref props)); }
/// <summary> /// Change the thumbnail settings /// </summary> private void UpdateThumbnail() { Opacity = Math.Max((byte)0x01, _pipConfiguration.Opacity) / 255d; // Prepare the displaying of the Thumbnail var props = new DwmThumbnailProperties { Opacity = _pipConfiguration.Opacity, Visible = true, SourceClientAreaOnly = _pipConfiguration.SourceClientAreaOnly, Destination = new NativeRect(0, 0, Width, Height) }; Dwm.DwmUpdateThumbnailProperties(_phThumbnail, ref props); }
public void Update(Rectangle destination, byte opacity, bool visible, bool onlyClientArea) { DwmThumbnailProperties ptnProperties = new DwmThumbnailProperties(); ptnProperties.dwFlags = DwmThumbnailFlags.SourceClientAreaOnly | DwmThumbnailFlags.Visible | DwmThumbnailFlags.Opacity | DwmThumbnailFlags.RectDestination; ptnProperties.rcDestination = new RECT(destination); ptnProperties.opacity = opacity; ptnProperties.fVisible = visible; ptnProperties.fSourceClientAreaOnly = onlyClientArea; if (Win32API.DwmUpdateThumbnailProperties(this, ref ptnProperties) != 0) { throw new DwmCompositionException("DWMThumbnailUpdateFailure"); } }
/// <summary> /// Show the thumbnail of the supplied window above (or under) the parent Control /// </summary> /// <param name="window">WindowDetails</param> /// <param name="parentControl">Control</param> public void ShowThumbnail(IInteropWindow window, Control parentControl) { UnregisterThumbnail(); Dwm.DwmRegisterThumbnail(Handle, window.Handle, out _thumbnailHandle); if (_thumbnailHandle == IntPtr.Zero) { return; } Dwm.DwmQueryThumbnailSourceSize(_thumbnailHandle, out var sourceSize); var thumbnailHeight = 200; var thumbnailWidth = (int)(thumbnailHeight * (sourceSize.Width / (float)sourceSize.Height)); if (parentControl != null && thumbnailWidth > parentControl.Width) { thumbnailWidth = parentControl.Width; thumbnailHeight = (int)(thumbnailWidth * (sourceSize.Height / (float)sourceSize.Width)); } Width = thumbnailWidth; Height = thumbnailHeight; // Prepare the displaying of the Thumbnail var props = new DwmThumbnailProperties { Opacity = 255, Visible = true, SourceClientAreaOnly = false, Destination = new NativeRect(0, 0, thumbnailWidth, thumbnailHeight) }; Dwm.DwmUpdateThumbnailProperties(_thumbnailHandle, ref props); if (parentControl != null) { AlignToControl(parentControl); } if (!Visible) { Show(); } // Make sure it's on "top"! if (parentControl != null) { User32Api.SetWindowPos(Handle, parentControl.Handle, 0, 0, 0, 0, WindowPos.SWP_NOMOVE | WindowPos.SWP_NOSIZE | WindowPos.SWP_NOACTIVATE); } }
/// <summary> /// Change the thumbnail settings /// </summary> private void UpdateThumbnail() { // Retrieve the current information about the window, this could changed var interopWindow = InteropWindowFactory.CreateFor(_hWnd).Fill(); var sourceBounds = interopWindow.Info.Value.Bounds; Opacity = Math.Max((byte)0x01, _pipConfiguration.Opacity) / 255d; // Prepare the displaying of the Thumbnail var props = new DwmThumbnailProperties { Opacity = _pipConfiguration.Opacity, Visible = true, SourceClientAreaOnly = _pipConfiguration.SourceClientAreaOnly, // This is the size of the DMW Thumbnail Destination = new NativeRect(0, 0, Width, Height), // Here it would be possible to select only a part of the window, but this is slightly tricky of someone resizes the window Source = new NativeRect(0, 0, sourceBounds.Width, sourceBounds.Height) }; Dwm.DwmUpdateThumbnailProperties(_phThumbnail, ref props); }
internal static IntPtr ShowThumb(IntPtr targetWindow, Window thumbWindow, Rect rect) { IntPtr thumb; int i = DwmRegisterThumbnail(targetWindow, thumbWindow.WindowPointer, out thumb); if (i == 0) { if (thumb != IntPtr.Zero) { Psize size; DwmQueryThumbnailSourceSize(thumb, out size); var props = new DwmThumbnailProperties { fVisible = true, dwFlags = DwmTnpVisible | DwmTnpRectdestination | DwmTnpOpacity, opacity = 255, rcDestination = rect }; DwmUpdateThumbnailProperties(thumb, ref props); } } return(thumb); }
/// <summary> /// Capture DWM Window /// </summary> /// <param name="interopWindow">IInteropWindow</param> /// <param name="capture">Capture to fill</param> /// <param name="windowCaptureMode">Wanted WindowCaptureModes</param> /// <param name="autoMode">True if auto modus is used</param> /// <returns>ICapture with the capture</returns> public static ICapture CaptureDwmWindow(this IInteropWindow interopWindow, ICapture capture, WindowCaptureModes windowCaptureMode, bool autoMode) { var thumbnailHandle = IntPtr.Zero; Form tempForm = null; var tempFormShown = false; try { tempForm = new Form { ShowInTaskbar = false, FormBorderStyle = FormBorderStyle.None, TopMost = true }; // Register the Thumbnail Dwm.DwmRegisterThumbnail(tempForm.Handle, interopWindow.Handle, out thumbnailHandle); // Get the original size Dwm.DwmQueryThumbnailSourceSize(thumbnailHandle, out var sourceSize); if (sourceSize.Width <= 0 || sourceSize.Height <= 0) { return(null); } // Calculate the location of the temp form var windowRectangle = interopWindow.GetInfo().Bounds; var formLocation = windowRectangle.Location; var borderSize = new Size(); var doesCaptureFit = false; if (!interopWindow.IsMaximized()) { // Assume using it's own location formLocation = windowRectangle.Location; using (var workingArea = new Region(Screen.PrimaryScreen.Bounds)) { // Find the screen where the window is and check if it fits foreach (var screen in Screen.AllScreens) { if (!Equals(screen, Screen.PrimaryScreen)) { workingArea.Union(screen.Bounds); } } // If the formLocation is not inside the visible area if (!workingArea.AreRectangleCornersVisisble(windowRectangle)) { // If none found we find the biggest screen foreach (var screen in Screen.AllScreens) { var newWindowRectangle = new NativeRect(screen.WorkingArea.Location, windowRectangle.Size); if (workingArea.AreRectangleCornersVisisble(newWindowRectangle)) { formLocation = screen.Bounds.Location; doesCaptureFit = true; break; } } } else { doesCaptureFit = true; } } } else if (!WindowsVersion.IsWindows8OrLater) { //GetClientRect(out windowRectangle); borderSize = interopWindow.GetInfo().BorderSize; formLocation = new NativePoint(windowRectangle.X - borderSize.Width, windowRectangle.Y - borderSize.Height); } tempForm.Location = formLocation; tempForm.Size = sourceSize; // Prepare rectangle to capture from the screen. var captureRectangle = new NativeRect(formLocation.X, formLocation.Y, sourceSize.Width, sourceSize.Height); if (interopWindow.IsMaximized()) { // Correct capture size for maximized window by offsetting the X,Y with the border size // and subtracting the border from the size (2 times, as we move right/down for the capture without resizing) captureRectangle = captureRectangle.Inflate(borderSize.Width, borderSize.Height); } else { // TODO: Also 8.x? if (WindowsVersion.IsWindows10) { captureRectangle = captureRectangle.Inflate(CoreConfiguration.Win10BorderCrop.Width, CoreConfiguration.Win10BorderCrop.Height); } if (autoMode) { // check if the capture fits if (!doesCaptureFit) { // if GDI is allowed.. (a screenshot won't be better than we comes if we continue) using (var thisWindowProcess = Process.GetProcessById(interopWindow.GetProcessId())) { if (!interopWindow.IsApp() && WindowCapture.IsGdiAllowed(thisWindowProcess)) { // we return null which causes the capturing code to try another method. return(null); } } } } } // Prepare the displaying of the Thumbnail var props = new DwmThumbnailProperties { Opacity = 255, Visible = true, Destination = new NativeRect(0, 0, sourceSize.Width, sourceSize.Height) }; Dwm.DwmUpdateThumbnailProperties(thumbnailHandle, ref props); tempForm.Show(); tempFormShown = true; // Intersect with screen captureRectangle = captureRectangle.Intersect(capture.ScreenBounds); // Destination bitmap for the capture Bitmap capturedBitmap = null; // Check if we make a transparent capture if (windowCaptureMode == WindowCaptureModes.AeroTransparent) { // Use white, later black to capture transparent tempForm.BackColor = Color.White; // Make sure everything is visible tempForm.Refresh(); Application.DoEvents(); try { using (var whiteBitmap = WindowCapture.CaptureRectangle(captureRectangle)) { // Apply a white color tempForm.BackColor = Color.Black; // Make sure everything is visible tempForm.Refresh(); if (!interopWindow.IsApp()) { // Make sure the application window is active, so the colors & buttons are right // TODO: Await? interopWindow.ToForegroundAsync(); } // Make sure all changes are processed and visible Application.DoEvents(); using (var blackBitmap = WindowCapture.CaptureRectangle(captureRectangle)) { capturedBitmap = ApplyTransparency(blackBitmap, whiteBitmap); } } } catch (Exception e) { Log.Warn().WriteLine(e, "Exception: "); // Some problem occurred, cleanup and make a normal capture if (capturedBitmap != null) { capturedBitmap.Dispose(); capturedBitmap = null; } } } // If no capture up till now, create a normal capture. if (capturedBitmap == null) { // Remove transparency, this will break the capturing if (!autoMode) { tempForm.BackColor = Color.FromArgb(255, CoreConfiguration.DWMBackgroundColor.R, CoreConfiguration.DWMBackgroundColor.G, CoreConfiguration.DWMBackgroundColor.B); } else { var colorizationColor = Dwm.ColorizationSystemDrawingColor; // Modify by losing the transparency and increasing the intensity (as if the background color is white) colorizationColor = Color.FromArgb(255, (colorizationColor.R + 255) >> 1, (colorizationColor.G + 255) >> 1, (colorizationColor.B + 255) >> 1); tempForm.BackColor = colorizationColor; } // Make sure everything is visible tempForm.Refresh(); if (!interopWindow.IsApp()) { // Make sure the application window is active, so the colors & buttons are right interopWindow.ToForegroundAsync(); } // Make sure all changes are processed and visible Application.DoEvents(); // Capture from the screen capturedBitmap = WindowCapture.CaptureRectangle(captureRectangle); } if (capturedBitmap != null) { // Not needed for Windows 8 if (!WindowsVersion.IsWindows8OrLater) { // Only if the Inivalue is set, not maximized and it's not a tool window. if (CoreConfiguration.WindowCaptureRemoveCorners && !interopWindow.IsMaximized() && !interopWindow.GetInfo().ExtendedStyle.HasFlag(ExtendedWindowStyleFlags.WS_EX_TOOLWINDOW)) { // Remove corners if (!Image.IsAlphaPixelFormat(capturedBitmap.PixelFormat)) { Log.Debug().WriteLine("Changing pixelformat to Alpha for the RemoveCorners"); var tmpBitmap = capturedBitmap.CloneBitmap(PixelFormat.Format32bppArgb) as Bitmap; capturedBitmap.Dispose(); capturedBitmap = tmpBitmap; } RemoveCorners(capturedBitmap); } } } capture.Bitmap = capturedBitmap; // Make sure the capture location is the location of the window, not the copy capture.Location = interopWindow.GetInfo().Bounds.Location; } finally { if (thumbnailHandle != IntPtr.Zero) { // Unregister (cleanup), as we are finished we don't need the form or the thumbnail anymore Dwm.DwmUnregisterThumbnail(thumbnailHandle); } if (tempForm != null) { if (tempFormShown) { tempForm.Close(); } tempForm.Dispose(); tempForm = null; } } return(capture); }
public static extern int DwmUpdateThumbnailProperties(Thumbnail hThumbnailId, ref DwmThumbnailProperties ptnProperties);
public static extern int DwmUpdateThumbnailProperties(Thumbnail hThumbnailId, ref DwmThumbnailProperties ptnProperties);
public static extern int DwmUpdateThumbnailProperties(IntPtr hThumb, ref DwmThumbnailProperties props);
private void UpdateThumb() { if (_currentThumbHandle == IntPtr.Zero) return; PSIZE size; DwmQueryThumbnailSourceSize(_currentThumbHandle, out size); var props = new DwmThumbnailProperties { dwFlags = DWM_TNP_VISIBLE | DWM_TNP_RECTDESTINATION | DWM_TNP_OPACITY, fVisible = true, opacity = byte.MaxValue, rcDestination = new Rect(0, 0, _defaultWidth, _defaultWidth*size.Height/size.Width) }; Rect lpRect; if (GetWindowRect(_targetHandle, out lpRect)) MoveWindow(_targetHandle, lpRect.Left, lpRect.Top, props.rcDestination.Width, props.rcDestination.Height, true); DwmUpdateThumbnailProperties(_currentThumbHandle, ref props); }
private static extern int DwmUpdateThumbnailProperties(IntPtr hThumb, ref DwmThumbnailProperties props);