From 728f49944fd3473aab300ba5d7934e3a13de0e82 Mon Sep 17 00:00:00 2001 From: Robin Date: Tue, 26 Apr 2016 23:33:17 +0200 Subject: [PATCH] BUG-1890: Fixed wrong cropping when window is maximized on Windows 10 --- .../Core/OperatingSystemExtensions.cs | 92 +++++++++++++++++++ GreenshotPlugin/Core/WindowsHelper.cs | 24 +++-- GreenshotPlugin/GreenshotPlugin.csproj | 1 + 3 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 GreenshotPlugin/Core/OperatingSystemExtensions.cs diff --git a/GreenshotPlugin/Core/OperatingSystemExtensions.cs b/GreenshotPlugin/Core/OperatingSystemExtensions.cs new file mode 100644 index 000000000..769547e03 --- /dev/null +++ b/GreenshotPlugin/Core/OperatingSystemExtensions.cs @@ -0,0 +1,92 @@ +/* + * Greenshot - a free and open source screenshot tool + * Copyright (C) 2007-2016 Thomas Braun, Jens Klingen, Robin Krom + * + * For more information see: http://getgreenshot.org/ + * The Greenshot project is hosted on GitHub: https://github.com/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 . + */ + +using System; + +namespace GreenshotPlugin.Core +{ + /// + /// Extensions to help with querying the Operating System + /// + public static class OperatingSystemExtensions + { + /// + /// Test if the current OS is Windows 10 + /// + /// OperatingSystem from Environment.OSVersion + /// true if we are running on Windows 10 + public static bool IsWindows10(this OperatingSystem operatingSystem) + { + return operatingSystem.Version.Major == 10; + } + + /// + /// Test if the current OS is Windows 8(.1) + /// + /// OperatingSystem from Environment.OSVersion + /// true if we are running on Windows 8(.1) + public static bool IsWindows8(this OperatingSystem operatingSystem) + { + return operatingSystem.Version.Major == 6 && operatingSystem.Version.Minor >= 2; + } + + /// + /// Test if the current OS is Windows 8 or later + /// + /// OperatingSystem from Environment.OSVersion + /// true if we are running on Windows 8 or later + public static bool IsWindows8OrLater(this OperatingSystem operatingSystem) + { + return (operatingSystem.Version.Major == 6 && operatingSystem.Version.Minor >= 2) || operatingSystem.Version.Major >= 6; + } + + /// + /// Test if the current OS is Windows 7 or later + /// + /// OperatingSystem from Environment.OSVersion + /// true if we are running on Windows 7 or later + public static bool IsWindows7OrLater(this OperatingSystem operatingSystem) + { + return (operatingSystem.Version.Major == 6 && operatingSystem.Version.Minor >= 1) || operatingSystem.Version.Major >= 6; + } + + /// + /// Test if the current OS is Windows Vista or later + /// + /// OperatingSystem from Environment.OSVersion + /// true if we are running on Windows Vista or later + public static bool IsWindowsVistaOrLater(this OperatingSystem operatingSystem) + { + return operatingSystem.Version.Major >= 6; + } + + /// + /// Test if the current OS is Windows XP or later + /// + /// OperatingSystem from Environment.OSVersion + /// true if we are running on Windows XP or later + public static bool IsWindowsXpOrLater(this OperatingSystem operatingSystem) + { + // Windows 2000 is Major 5 minor 0 + return Environment.OSVersion.Version.Major > 5 || (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1); + } + } +} diff --git a/GreenshotPlugin/Core/WindowsHelper.cs b/GreenshotPlugin/Core/WindowsHelper.cs index 412e018bc..0fa642128 100644 --- a/GreenshotPlugin/Core/WindowsHelper.cs +++ b/GreenshotPlugin/Core/WindowsHelper.cs @@ -768,7 +768,13 @@ namespace GreenshotPlugin.Core { if (previousWindowRectangle.IsEmpty || now - lastWindowRectangleRetrieveTime > CACHE_TIME) { Rectangle windowRect = Rectangle.Empty; if (DWM.isDWMEnabled()) { - GetExtendedFrameBounds(out windowRect); + if (GetExtendedFrameBounds(out windowRect) && Environment.OSVersion.IsWindows10()) + { + lastWindowRectangleRetrieveTime = now; + previousWindowRectangle = windowRect; + // DWM does it corectly, just return the window rectangle we just gotten. + return windowRect; + } } if (windowRect.IsEmpty) { @@ -915,10 +921,12 @@ namespace GreenshotPlugin.Core { Form tempForm = null; bool tempFormShown = false; try { - tempForm = new Form(); - tempForm.ShowInTaskbar = false; - tempForm.FormBorderStyle = FormBorderStyle.None; - tempForm.TopMost = true; + tempForm = new Form + { + ShowInTaskbar = false, + FormBorderStyle = FormBorderStyle.None, + TopMost = true + }; // Register the Thumbnail DWM.DwmRegisterThumbnail(tempForm.Handle, Handle, out thumbnailHandle); @@ -932,8 +940,8 @@ namespace GreenshotPlugin.Core { } // Calculate the location of the temp form - Point formLocation; Rectangle windowRectangle = WindowRectangle; + Point formLocation = formLocation = windowRectangle.Location; Size borderSize = new Size(); bool doesCaptureFit = false; if (!Maximised) { @@ -962,7 +970,7 @@ namespace GreenshotPlugin.Core { doesCaptureFit = true; } } - } else { + } else if (!Environment.OSVersion.IsWindows8OrLater()) { //GetClientRect(out windowRectangle); GetBorderSize(out borderSize); formLocation = new Point(windowRectangle.X - borderSize.Width, windowRectangle.Y - borderSize.Height); @@ -1067,7 +1075,7 @@ namespace GreenshotPlugin.Core { } if (capturedBitmap != null) { // Not needed for Windows 8 - if (!(Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor >= 2)) { + if (!Environment.OSVersion.IsWindows8OrLater()) { // Only if the Inivalue is set, not maximized and it's not a tool window. if (conf.WindowCaptureRemoveCorners && !Maximised && (ExtendedWindowStyle & ExtendedWindowStyleFlags.WS_EX_TOOLWINDOW) == 0) { // Remove corners diff --git a/GreenshotPlugin/GreenshotPlugin.csproj b/GreenshotPlugin/GreenshotPlugin.csproj index 917a12654..f29b3b79b 100644 --- a/GreenshotPlugin/GreenshotPlugin.csproj +++ b/GreenshotPlugin/GreenshotPlugin.csproj @@ -42,6 +42,7 @@ +