BUG-2553, BUG-2535: This should fix the issues we are having with selection / presenting the wrong windows. (Previous commit had a bug)

This commit is contained in:
Robin 2020-03-03 09:10:33 +01:00
commit 0202c2fa8b
8 changed files with 129 additions and 103 deletions

View file

@ -97,7 +97,7 @@ namespace Greenshot.Forms {
public WindowDetails SelectedCaptureWindow => _selectedCaptureWindow;
/// <summary>
/// This should prevent childs to draw backgrounds
/// This should prevent children to draw backgrounds
/// </summary>
protected override CreateParams CreateParams {
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]

View file

@ -927,7 +927,7 @@ namespace Greenshot {
public void AddCaptureWindowMenuItems(ToolStripMenuItem menuItem, EventHandler eventHandler) {
menuItem.DropDownItems.Clear();
// check if thumbnailPreview is enabled and DWM is enabled
bool thumbnailPreview = _conf.ThumnailPreview && DWM.IsDwmEnabled();
bool thumbnailPreview = _conf.ThumnailPreview && DWM.IsDwmEnabled;
foreach(WindowDetails window in WindowDetails.GetTopLevelWindows()) {

View file

@ -193,7 +193,7 @@ namespace Greenshot {
private void SetWindowCaptureMode(WindowCaptureMode selectedWindowCaptureMode) {
WindowCaptureMode[] availableModes;
if (!DWM.IsDwmEnabled()) {
if (!DWM.IsDwmEnabled) {
// Remove DWM from configuration, as DWM is disabled!
if (coreConfiguration.WindowCaptureMode == WindowCaptureMode.Aero || coreConfiguration.WindowCaptureMode == WindowCaptureMode.AeroTransparent) {
coreConfiguration.WindowCaptureMode = WindowCaptureMode.GDI;

View file

@ -813,7 +813,7 @@ namespace Greenshot.Helpers {
Rectangle windowRectangle = windowToCapture.WindowRectangle;
// When Vista & DWM (Aero) enabled
bool dwmEnabled = DWM.IsDwmEnabled();
bool dwmEnabled = DWM.IsDwmEnabled;
// get process name to be able to exclude certain processes from certain capture modes
using (Process process = windowToCapture.Process) {
bool isAutoMode = windowCaptureMode == WindowCaptureMode.Auto;
@ -1010,7 +1010,9 @@ namespace Greenshot.Helpers {
} finally {
captureForm.Hide();
}
if (result == DialogResult.OK) {
if (result != DialogResult.OK) return;
_selectedCaptureWindow = captureForm.SelectedCaptureWindow;
_captureRect = captureForm.CaptureRectangle;
// Get title
@ -1032,4 +1034,3 @@ namespace Greenshot.Helpers {
}
}
}
}

View file

@ -553,6 +553,11 @@ namespace GreenshotPlugin.Core
/// </summary>
public bool Visible {
get {
// Tip from Raymond Chen
if (DWM.IsWindowCloaked(Handle))
{
return false;
}
if (IsApp) {
Rectangle windowRectangle = WindowRectangle;
foreach (Screen screen in Screen.AllScreens) {
@ -584,8 +589,7 @@ namespace GreenshotPlugin.Core
if (IsAppLauncher) {
return IsAppLauncherVisible;
}
// Tip from Raymond Chen
return User32.IsWindowVisible(Handle) && !DWM.IsWindowCloaked(Handle);
return User32.IsWindowVisible(Handle);
}
}
@ -634,14 +638,17 @@ namespace GreenshotPlugin.Core
// Try to return a cached value
long now = DateTime.Now.Ticks;
if (_previousWindowRectangle.IsEmpty || !_frozen) {
if (_previousWindowRectangle.IsEmpty || now - _lastWindowRectangleRetrieveTime > CacheTime) {
if (!_previousWindowRectangle.IsEmpty && now - _lastWindowRectangleRetrieveTime <= CacheTime)
{
return _previousWindowRectangle;
}
Rectangle windowRect = Rectangle.Empty;
if (DWM.IsDwmEnabled())
if (DWM.IsDwmEnabled)
{
bool gotFrameBounds = GetExtendedFrameBounds(out windowRect);
if (IsApp)
{
// Pre-Cache for Maximised call, this is only on Windows 8 apps (full screen)
// Pre-Cache for maximized call, this is only on Windows 8 apps (full screen)
if (gotFrameBounds)
{
_previousWindowRectangle = windowRect;
@ -683,7 +690,6 @@ namespace GreenshotPlugin.Core
_previousWindowRectangle = windowRect;
return windowRect;
}
}
return _previousWindowRectangle;
}
}
@ -752,12 +758,8 @@ namespace GreenshotPlugin.Core
/// Get / Set the WindowStyle
/// </summary>
public WindowStyleFlags WindowStyle {
get {
return (WindowStyleFlags)User32.GetWindowLongWrapper(Handle, (int)WindowLongIndex.GWL_STYLE);
}
set {
User32.SetWindowLongWrapper(Handle, (int)WindowLongIndex.GWL_STYLE, new IntPtr((long)value));
}
get => (WindowStyleFlags)User32.GetWindowLongWrapper(Handle, (int)WindowLongIndex.GWL_STYLE);
set => User32.SetWindowLongWrapper(Handle, (int)WindowLongIndex.GWL_STYLE, new IntPtr((long)value));
}
/// <summary>
@ -778,12 +780,8 @@ namespace GreenshotPlugin.Core
/// Get/Set the Extended WindowStyle
/// </summary>
public ExtendedWindowStyleFlags ExtendedWindowStyle {
get {
return (ExtendedWindowStyleFlags)User32.GetWindowLongWrapper(Handle, (int)WindowLongIndex.GWL_EXSTYLE);
}
set {
User32.SetWindowLongWrapper(Handle, (int)WindowLongIndex.GWL_EXSTYLE, new IntPtr((uint)value));
}
get => (ExtendedWindowStyleFlags)User32.GetWindowLongWrapper(Handle, (int)WindowLongIndex.GWL_EXSTYLE);
set => User32.SetWindowLongWrapper(Handle, (int)WindowLongIndex.GWL_EXSTYLE, new IntPtr((uint)value));
}
/// <summary>
@ -806,7 +804,7 @@ namespace GreenshotPlugin.Core
/// </summary>
/// <param name="capture">Capture to fill</param>
/// <param name="windowCaptureMode">Wanted WindowCaptureMode</param>
/// <param name="autoMode">True if auto modus is used</param>
/// <param name="autoMode">True if auto mode is used</param>
/// <returns>ICapture with the capture</returns>
public ICapture CaptureDwmWindow(ICapture capture, WindowCaptureMode windowCaptureMode, bool autoMode) {
IntPtr thumbnailHandle = IntPtr.Zero;
@ -838,6 +836,7 @@ namespace GreenshotPlugin.Core
if (!Maximised) {
// Assume using it's own location
formLocation = windowRectangle.Location;
// TODO: Use Rectangle.Union!
using Region workingArea = new Region(Screen.PrimaryScreen.Bounds);
// Find the screen where the window is and check if it fits
foreach (Screen screen in Screen.AllScreens) {
@ -1152,12 +1151,12 @@ namespace GreenshotPlugin.Core
if (workaround)
{
const byte alt = 0xA4;
const int extendedkey = 0x1;
const int extendedKey = 0x1;
const int keyup = 0x2;
// Simulate an "ALT" key press.
User32.keybd_event(alt, 0x45, extendedkey | 0, 0);
User32.keybd_event(alt, 0x45, extendedKey | 0, 0);
// Simulate an "ALT" key release.
User32.keybd_event(alt, 0x45, extendedkey | keyup, 0);
User32.keybd_event(alt, 0x45, extendedKey | keyup, 0);
}
// Show window in forground.
User32.BringWindowToTop(handle);

View file

@ -59,7 +59,6 @@ namespace GreenshotPlugin.Core {
/// <param name="classname">Window Classname to copy, use null to copy all</param>
public WindowsEnumerator GetWindows(IntPtr hWndParent, string classname) {
Items = new List<WindowDetails>();
IList<WindowDetails> windows = new List<WindowDetails>();
User32.EnumChildWindows(hWndParent, WindowEnum, IntPtr.Zero);
bool hasParent = !IntPtr.Zero.Equals(hWndParent);
@ -70,6 +69,7 @@ namespace GreenshotPlugin.Core {
parentText = title.ToString();
}
List<WindowDetails> windows = new List<WindowDetails>();
foreach (var window in Items) {
if (hasParent) {
window.Text = parentText;
@ -104,7 +104,7 @@ namespace GreenshotPlugin.Core {
/// </summary>
/// <param name="hWnd">Window handle to add</param>
/// <returns>True to continue enumeration, False to stop</returns>
protected bool OnWindowEnum(IntPtr hWnd) {
private bool OnWindowEnum(IntPtr hWnd) {
if (!WindowDetails.IsIgnoreHandle(hWnd)) {
Items.Add(new WindowDetails(hWnd));
}

View file

@ -30,7 +30,7 @@ using Microsoft.Win32;
namespace GreenshotPlugin.UnmanagedHelpers {
/// <summary>
/// Description of DWM.
/// Desktop Window Manager helper code
/// </summary>
public static class DWM {
public static readonly uint DWM_EC_DISABLECOMPOSITION = 0;
@ -75,12 +75,12 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// <returns>bool</returns>
public static bool IsWindowCloaked(IntPtr hWnd)
{
if (WindowsVersion.IsWindows8OrLater)
if (!WindowsVersion.IsWindows8OrLater)
{
return false;
}
DwmGetWindowAttribute(hWnd, DWMWINDOWATTRIBUTE.DWMWA_CLOAKED, out bool isCloaked, sizeof(bool));
DwmGetWindowAttribute(hWnd, DWMWINDOWATTRIBUTE.DWMWA_CLOAKED, out bool isCloaked, Marshal.SizeOf(typeof(bool)));
return isCloaked;
}
@ -88,19 +88,24 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// Helper method for an easy DWM check
/// </summary>
/// <returns>bool true if DWM is available AND active</returns>
public static bool IsDwmEnabled() {
public static bool IsDwmEnabled {
get
{
// According to: http://technet.microsoft.com/en-us/subscriptions/aa969538%28v=vs.85%29.aspx
// And: http://msdn.microsoft.com/en-us/library/windows/desktop/aa969510%28v=vs.85%29.aspx
// DMW is always enabled on Windows 8! So return true and save a check! ;-)
if (WindowsVersion.IsWindows8OrLater) {
if (WindowsVersion.IsWindows8OrLater)
{
return true;
}
if (WindowsVersion.IsWindowsVistaOrLater) {
if (WindowsVersion.IsWindowsVistaOrLater)
{
DwmIsCompositionEnabled(out var dwmEnabled);
return dwmEnabled;
}
return false;
}
}
public static Color ColorizationColor {
get {

View file

@ -1,3 +1,24 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
namespace GreenshotPlugin.UnmanagedHelpers
@ -5,8 +26,8 @@ namespace GreenshotPlugin.UnmanagedHelpers
/// <summary>
/// Used with EnumWindows or EnumChildWindows
/// </summary>
/// <param name="hwnd"></param>
/// <param name="lParam"></param>
/// <returns></returns>
public delegate int EnumWindowsProc(IntPtr hwnd, int lParam);
/// <param name="hWnd">IntPtr</param>
/// <param name="lParam">int</param>
/// <returns>int</returns>
public delegate int EnumWindowsProc(IntPtr hWnd, int lParam);
}