private void InitializeThumbnail() { if (IntPtr.Zero != _thumb) { // release the old thumbnail ReleaseThumbnail(); } if (IntPtr.Zero == Handle) { return; } // find our parent hwnd _target = (HwndSource)HwndSource.FromVisual(this); // if we have one, we can attempt to register the thumbnail if (_target == null || 0 != DwmUtils.DwmRegisterThumbnail(_target.Handle, Handle, out this._thumb)) { return; } DwmThumbnailProperties props = new DwmThumbnailProperties(); props.Visible = false; props.SourceClientAreaOnly = false; props.Opacity = (byte)(255 * this.Opacity); props.Flags = ThumbnailFlags.Visible | ThumbnailFlags.SourceClientAreaOnly | ThumbnailFlags.Opacity; DwmUtils.DwmUpdateThumbnailProperties(_thumb, ref props); }
// this is where the magic happens private void Thumbnail_LayoutUpdated(object sender, EventArgs e) { if (IntPtr.Zero == thumb) { InitialiseThumbnail(this.Source); } if (IntPtr.Zero != thumb) { if (!target.RootVisual.IsAncestorOf(this)) { //we are no longer in the visual tree ReleaseThumbnail(); return; } GeneralTransform transform = TransformToAncestor(target.RootVisual); Point a = transform.Transform(new Point(0, 0)); Point b = transform.Transform(new Point(this.ActualWidth, this.ActualHeight)); DwmThumbnailProperties props = new DwmThumbnailProperties(); props.Visible = true; props.Destination = new Rect( (int)Math.Ceiling(a.X), (int)Math.Ceiling(a.Y), (int)Math.Ceiling(b.X), (int)Math.Ceiling(b.Y)); props.Flags = ThumbnailFlags.Visible | ThumbnailFlags.RectDetination; DwmUtils.DwmUpdateThumbnailProperties(thumb, ref props); } }
private void InitialiseThumbnail(IntPtr source) { if (IntPtr.Zero != thumb) { // release the old thumbnail ReleaseThumbnail(); } if (IntPtr.Zero != source) { // find our parent hwnd target = (HwndSource)HwndSource.FromVisual(this); // if we have one, we can attempt to register the thumbnail if (target != null && 0 == DwmUtils.DwmRegisterThumbnail(target.Handle, source, out this.thumb)) { DwmThumbnailProperties props = new DwmThumbnailProperties(); props.Visible = false; props.SourceClientAreaOnly = this.ClientAreaOnly; props.Opacity = (byte)(255 * this.Opacity); props.Flags = ThumbnailFlags.Visible | ThumbnailFlags.SourceClientAreaOnly | ThumbnailFlags.Opacity; DwmUtils.DwmUpdateThumbnailProperties(thumb, ref props); } } }
public void DrawRectForWindow() { if (IntPtr.Zero == _thumb) { InitializeThumbnail(); } if (IntPtr.Zero == _thumb) { return; } if (!_target.RootVisual.IsAncestorOf(this)) { //we are no longer in the visual tree ReleaseThumbnail(); return; } double scale = DwmUtils.GetSystemScale(); // keep original window aspect ratio double windowWidth = WindowRect.Right - WindowRect.Left; double windowHeight = WindowRect.Bottom - WindowRect.Top; Point origin = new Point(Canvas.GetLeft(this), Canvas.GetTop(this)); double scaleFactor = Height / windowHeight; windowHeight = scaleFactor * windowHeight; windowWidth = scaleFactor * windowWidth; // origins remain the same as provided var rect = new Rect( (int)(origin.X), (int)(origin.Y), (int)(origin.X + windowWidth), (int)(origin.Y + windowHeight)); var scaledRect = new Rect( (int)(origin.X * scale), (int)(origin.Y * scale), (int)((origin.X + windowWidth) * scale), (int)((origin.Y + windowHeight) * scale)); DwmThumbnailProperties props = new DwmThumbnailProperties(); props.Visible = true; props.Destination = scaledRect; // props.Destination = new DWM.Rect( // (int) Math.Ceiling(a.X), (int) Math.Ceiling(a.Y), // (int) Math.Ceiling(b.X), (int) Math.Ceiling(b.Y)); props.Flags = ThumbnailFlags.Visible | ThumbnailFlags.RectDetination; DwmUtils.DwmUpdateThumbnailProperties(_thumb, ref props); }
private void UpdateThumbnail() { if (IntPtr.Zero != thumb) { DwmThumbnailProperties props = new DwmThumbnailProperties(); props.SourceClientAreaOnly = this.ClientAreaOnly; props.Opacity = (byte)(255 * this.Opacity); props.Flags = ThumbnailFlags.SourceClientAreaOnly | ThumbnailFlags.Opacity; DwmUtils.DwmUpdateThumbnailProperties(thumb, ref props); } }