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 == Interop.DwmRegisterThumbnail(target.Handle, source, out this.thumb)) { Interop.ThumbnailProperties props = new Interop.ThumbnailProperties(); props.Visible = false; props.SourceClientAreaOnly = this.ClientAreaOnly; props.Opacity = (byte)(255 * this.Opacity); props.Flags = Interop.ThumbnailFlags.Visible | Interop.ThumbnailFlags.SourceClientAreaOnly | Interop.ThumbnailFlags.Opacity; Interop.DwmUpdateThumbnailProperties(thumb, ref props); } } }
private void UpdateThumbnail() { if (IntPtr.Zero != thumb) { Interop.ThumbnailProperties props = new Interop.ThumbnailProperties(); props.SourceClientAreaOnly = this.ClientAreaOnly; props.Opacity = (byte)(255 * this.Opacity); props.Flags = Interop.ThumbnailFlags.SourceClientAreaOnly | Interop.ThumbnailFlags.Opacity; Interop.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 == null || !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)); if (double.IsNaN(a.X)) { this.InvalidateArrange(); } else { Point b = transform.Transform(new Point(this.ActualWidth, this.ActualHeight)); Interop.ThumbnailProperties props = new Interop.ThumbnailProperties(); props.Visible = true; props.Destination = new Interop.Rect( (int)Math.Ceiling(a.X), (int)Math.Ceiling(a.Y), (int)Math.Ceiling(b.X), (int)Math.Ceiling(b.Y)); props.Flags = Interop.ThumbnailFlags.Visible | Interop.ThumbnailFlags.RectDestination; Interop.DwmUpdateThumbnailProperties(thumb, ref props); } } }