using System;
using System.Drawing;
using System.Windows.Forms;
using Vanara.PInvoke;
using static Vanara.PInvoke.DwmApi;
namespace Vanara.Windows.Forms
{
/// Extracts all or a portion of a window and renders it as a thumbnail on another portion of the desktop.
public class LiveThumbnail : IDisposable
{
private readonly HTHUMBNAIL hThumbnail;
private DWM_THUMBNAIL_PROPERTIES tProps;
/// Initializes a new instance of the class.
/// The window.
internal LiveThumbnail(IWin32Window win)
{
hThumbnail = DesktopWindowManager.thumbnailMgr.Register(win);
SourceClientAreaOnly = true;
tProps.opacity = 255;
}
/// Gets or sets the area in the destination window where the thumbnail will be rendered.
/// The destination rectangle.
public Rectangle DestinationRectangle
{
get => tProps.rcDestination;
set
{
tProps.dwFlags = DWM_TNP.DWM_TNP_RECTDESTINATION;
tProps.rcDestination = value;
DwmUpdateThumbnailProperties(hThumbnail, tProps).ThrowIfFailed();
}
}
///
/// Gets or sets the opacity with which to render the thumbnail. 0 is fully transparent while 255 is fully opaque. The default value is 255.
///
/// The opacity.
public byte Opacity
{
get => tProps.opacity;
set
{
tProps.dwFlags = DWM_TNP.DWM_TNP_OPACITY;
tProps.opacity = value;
DwmUpdateThumbnailProperties(hThumbnail, tProps).ThrowIfFailed();
}
}
/// Gets or sets a value indicating whether to use only the thumbnail source's client area.
/// TRUE to use only the thumbnail source's client area; otherwise, FALSE. The default is FALSE.
public bool SourceClientAreaOnly
{
get => tProps.fSourceClientAreaOnly;
set
{
tProps.dwFlags = DWM_TNP.DWM_TNP_SOURCECLIENTAREAONLY;
tProps.fSourceClientAreaOnly = value;
DwmUpdateThumbnailProperties(hThumbnail, tProps).ThrowIfFailed();
}
}
/// Gets or sets the region of the source window to use as the thumbnail. By default, the entire window is used as the thumbnail.
/// The source region rectangle.
public Rectangle SourceRectangle
{
get => tProps.rcSource;
set
{
tProps.dwFlags = DWM_TNP.DWM_TNP_RECTSOURCE;
tProps.rcSource = value;
DwmUpdateThumbnailProperties(hThumbnail, tProps).ThrowIfFailed();
}
}
/// Gets the size of the source.
/// The size of the source.
public Size SourceSize
{
get
{
DwmQueryThumbnailSourceSize(hThumbnail, out var sz).ThrowIfFailed();
return sz;
}
}
/// Gets a value indicating whether this is visible.
/// true if visible; otherwise, false.
public bool Visible
{
get => tProps.fVisible;
private set
{
tProps.dwFlags = DWM_TNP.DWM_TNP_VISIBLE;
tProps.fVisible = value;
DwmUpdateThumbnailProperties(hThumbnail, tProps).ThrowIfFailed();
}
}
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose() => DesktopWindowManager.thumbnailMgr.Unregister(hThumbnail);
/// Hides the thumbnail.
public void Hide() => Visible = false;
/// Shows the thumbnail at the location specified by .
public void Show() => Visible = true;
/// Shows the thumbnail at the location specified by .
/// The destination window where the thumbnail will be rendered.
public void Show(Rectangle destRect)
{
tProps.dwFlags = DWM_TNP.DWM_TNP_VISIBLE | DWM_TNP.DWM_TNP_RECTDESTINATION;
tProps.fVisible = true;
tProps.rcDestination = destRect;
DwmUpdateThumbnailProperties(hThumbnail, tProps).ThrowIfFailed();
}
/// Shows the thumbnail at the location specified by and at the specified opacity.
/// The destination window where the thumbnail will be rendered.
/// The opacity with which to render the thumbnail. 0 is fully transparent while 255 is fully opaque.
public void Show(Rectangle destRect, byte opacity)
{
tProps.dwFlags = DWM_TNP.DWM_TNP_VISIBLE | DWM_TNP.DWM_TNP_RECTDESTINATION | DWM_TNP.DWM_TNP_OPACITY;
tProps.fVisible = true;
tProps.rcDestination = destRect;
tProps.opacity = opacity;
DwmUpdateThumbnailProperties(hThumbnail, tProps).ThrowIfFailed();
}
}
}