Revert of the change to the DWMWINDOWATTRIBUTE, as the enum values were not fixed we lost some functionality. Also fixed some potential issues with the ThumbnailForm, and aligned a bit of code with Dapplo.Window [skip ci]

This commit is contained in:
Robin Krom 2021-03-26 22:30:28 +01:00
commit b22d982f36
No known key found for this signature in database
GPG key ID: BCC01364F1371490
7 changed files with 174 additions and 102 deletions

View file

@ -22,6 +22,7 @@ using System;
using System.Windows.Forms;
using GreenshotPlugin.Core;
using System.Drawing;
using GreenshotPlugin.Core.Enums;
using GreenshotPlugin.IniFile;
using GreenshotPlugin.UnmanagedHelpers;
using GreenshotPlugin.UnmanagedHelpers.Enums;
@ -32,7 +33,7 @@ namespace GreenshotPlugin.Controls {
/// This form allows us to show a Thumbnail preview of a window near the context menu when selecting a window to capture.
/// Didn't make it completely "generic" yet, but at least most logic is in here so we don't have it in the mainform.
/// </summary>
public class ThumbnailForm : FormWithoutActivation {
public sealed class ThumbnailForm : FormWithoutActivation {
private static readonly CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private IntPtr _thumbnailHandle = IntPtr.Zero;
@ -59,12 +60,13 @@ namespace GreenshotPlugin.Controls {
base.Hide();
}
private void UnregisterThumbnail() {
if (_thumbnailHandle != IntPtr.Zero) {
DWM.DwmUnregisterThumbnail(_thumbnailHandle);
_thumbnailHandle = IntPtr.Zero;
}
}
private void UnregisterThumbnail()
{
if (_thumbnailHandle == IntPtr.Zero) return;
DWM.DwmUnregisterThumbnail(_thumbnailHandle);
_thumbnailHandle = IntPtr.Zero;
}
/// <summary>
/// Show the thumbnail of the supplied window above (or under) the parent Control
@ -75,41 +77,60 @@ namespace GreenshotPlugin.Controls {
UnregisterThumbnail();
DWM.DwmRegisterThumbnail(Handle, window.Handle, out _thumbnailHandle);
if (_thumbnailHandle != IntPtr.Zero) {
DWM.DwmQueryThumbnailSourceSize(_thumbnailHandle, out var sourceSize);
int thumbnailHeight = 200;
int 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
DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES
{
Opacity = 255,
Visible = true,
SourceClientAreaOnly = false,
Destination = new RECT(0, 0, thumbnailWidth, thumbnailHeight)
};
DWM.DwmUpdateThumbnailProperties(_thumbnailHandle, ref props);
if (parentControl != null) {
AlignToControl(parentControl);
}
if (_thumbnailHandle == IntPtr.Zero) return;
if (!Visible) {
Show();
}
// Make sure it's on "top"!
if (parentControl != null) {
User32.SetWindowPos(Handle, parentControl.Handle, 0, 0, 0, 0, WindowPos.SWP_NOMOVE | WindowPos.SWP_NOSIZE | WindowPos.SWP_NOACTIVATE);
}
}
}
var result = DWM.DwmQueryThumbnailSourceSize(_thumbnailHandle, out var sourceSize);
if (result.Failed())
{
DWM.DwmUnregisterThumbnail(_thumbnailHandle);
return;
}
if (sourceSize.IsEmpty)
{
DWM.DwmUnregisterThumbnail(_thumbnailHandle);
return;
}
int thumbnailHeight = 200;
int 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 dwmThumbnailProperties = new DWM_THUMBNAIL_PROPERTIES
{
Opacity = 255,
Visible = true,
SourceClientAreaOnly = false,
Destination = new RECT(0, 0, thumbnailWidth, thumbnailHeight)
};
result = DWM.DwmUpdateThumbnailProperties(_thumbnailHandle, ref dwmThumbnailProperties);
if (result.Failed())
{
DWM.DwmUnregisterThumbnail(_thumbnailHandle);
return;
}
if (parentControl != null) {
AlignToControl(parentControl);
}
if (!Visible) {
Show();
}
// Make sure it's on "top"!
if (parentControl != null) {
User32.SetWindowPos(Handle, parentControl.Handle, 0, 0, 0, 0, WindowPos.SWP_NOMOVE | WindowPos.SWP_NOSIZE | WindowPos.SWP_NOACTIVATE);
}
}
public void AlignToControl(Control alignTo) {
Rectangle screenBounds = WindowCapture.GetScreenBounds();
var screenBounds = WindowCapture.GetScreenBounds();
if (screenBounds.Contains(alignTo.Left, alignTo.Top - Height)) {
Location = new Point(alignTo.Left + (alignTo.Width / 2) - (Width / 2), alignTo.Top - Height);
} else {