/// <summary>Handles the LayoutUpdated event of the Thumbnail image /// Actually, we really just ask Windows to paint us at our new size... /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void Thumbnail_LayoutUpdated(object sender, EventArgs e) { if (IntPtr.Zero.Equals(_thumb)) { InitialiseThumbnail(WindowSource); } else if (null != _target) { 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(ActualWidth, ActualHeight)); var props = new NativeMethods.ThumbnailProperties { Visible = true, Destination = new NativeMethods.Rect( 2 + (int) Math.Ceiling(a.X), 2 + (int) Math.Ceiling(a.Y), -2 + (int) Math.Ceiling(b.X), -2 + (int) Math.Ceiling(b.Y)), Flags = NativeMethods.ThumbnailFlags.Visible | NativeMethods.ThumbnailFlags.RectDestination }; NativeMethods.DwmUpdateThumbnailProperties(_thumb, ref props); } }
/// <summary>Updates the thumbnail /// </summary> private void UpdateThumbnail() { if (IntPtr.Zero != _thumb) { var props = new NativeMethods.ThumbnailProperties { ClientAreaOnly = ClientAreaOnly, Opacity = (byte) (255*Opacity), Flags = NativeMethods.ThumbnailFlags.SourceClientAreaOnly | NativeMethods.ThumbnailFlags.Opacity }; NativeMethods.DwmUpdateThumbnailProperties(_thumb, ref props); } }
/// <summary>Initialises the thumbnail image /// </summary> /// <param name="source">The source.</param> private void InitialiseThumbnail(IntPtr source) { if (IntPtr.Zero != _thumb) { // release the old thumbnail ReleaseThumbnail(); } if (IntPtr.Zero != source) { // find our parent hwnd // [System.Windows.Interop.HwndSource]::FromHwnd( [Diagnostics.Process]::GetCurrentProcess().MainWindowHandle ) //target = HwndSource.FromHwnd(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle); _target = (HwndSource)PresentationSource.FromVisual(this); // if we have one, we can attempt to register the thumbnail if (_target != null) { int result = NativeMethods.DwmRegisterThumbnail(_target.Handle, source, out _thumb); if (0 == result) { var props = new NativeMethods.ThumbnailProperties { Visible = false, ClientAreaOnly = ClientAreaOnly, Opacity = (byte)(255 * Opacity), Flags = NativeMethods.ThumbnailFlags.Visible | NativeMethods.ThumbnailFlags.SourceClientAreaOnly | NativeMethods.ThumbnailFlags.Opacity }; NativeMethods.DwmUpdateThumbnailProperties(_thumb, ref props); } } } }