Improve DPI support (#254)

* Improving the DPI handling for most forms, there are still issues with:
* the AboutForm.Designer.cs where the title with the version scales differently.
* the destination picker doesn't seem to scale the font correctly.
Some parts are not tested yet...

* Solved the issue with the destination picker font, and some other small issues.
There still is an issue when using Powertoys (the feature which is experimental), that the capture is somehow skipping.
This commit is contained in:
Robin Krom 2020-10-23 00:28:50 +02:00 committed by GitHub
commit 94c778d82c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
37 changed files with 216 additions and 216 deletions

View file

@ -155,10 +155,16 @@ namespace GreenshotPlugin.Core {
TopLevel = true
};
menu.Opened += (sender, args) =>
{
var scaledIconSize = DpiHelper.ScaleWithDpi(CoreConfig.IconSize, DpiHelper.GetDpi(menu.Handle));
menu.ImageScalingSize = scaledIconSize;
menu.Opening += (sender, args) =>
{
// find the DPI settings for the screen where this is going to land
var screenDpi = DpiHelper.GetDpi(menu.Location);
var scaledIconSize = DpiHelper.ScaleWithDpi(CoreConfig.IconSize, screenDpi);
menu.SuspendLayout();
var fontSize = DpiHelper.ScaleWithDpi(12f, screenDpi);
menu.Font = new Font(FontFamily.GenericSansSerif, fontSize, FontStyle.Regular, GraphicsUnit.Pixel);
menu.ImageScalingSize = scaledIconSize;
menu.ResumeLayout();
};
menu.Closing += delegate(object source, ToolStripDropDownClosingEventArgs eventArgs) {
@ -191,7 +197,10 @@ namespace GreenshotPlugin.Core {
menu.MouseEnter += delegate
{
// in case the menu has been unfocused, focus again so that dropdown menus will still open on mouseenter
if(!menu.ContainsFocus) menu.Focus();
if (!menu.ContainsFocus)
{
menu.Focus();
}
};
foreach (IDestination destination in destinations) {
// Fix foreach loop variable for the delegate

View file

@ -453,6 +453,24 @@ namespace GreenshotPlugin.Core
}
}
/// <summary>
/// Return the DPI for the screen which the location is located on
/// </summary>
/// <param name="location">POINT</param>
/// <returns>uint</returns>
public static uint GetDpi(POINT location)
{
RECT rect = new RECT(location.X, location.Y, 1,1);
IntPtr hMonitor = User32.MonitorFromRect(ref rect, User32.MONITOR_DEFAULTTONEAREST);
var result = GetDpiForMonitor(hMonitor, MonitorDpiType.EffectiveDpi, out var dpiX, out var dpiY);
if (result.Succeeded())
{
return dpiX;
}
return DefaultScreenDpi;
}
/// <summary>
/// Retrieve the DPI value for the supplied window handle
/// </summary>
@ -476,7 +494,8 @@ namespace GreenshotPlugin.Core
{
var hMonitor = User32.MonitorFromWindow(hWnd, MonitorFrom.DefaultToNearest);
// ReSharper disable once UnusedVariable
if (GetDpiForMonitor(hMonitor, MonitorDpiType.EffectiveDpi, out var dpiX, out var dpiY))
var result = GetDpiForMonitor(hMonitor, MonitorDpiType.EffectiveDpi, out var dpiX, out var dpiY);
if (result.Succeeded())
{
return dpiX;
}
@ -544,9 +563,8 @@ namespace GreenshotPlugin.Core
/// <param name="dpiX">out int for the horizontal dpi</param>
/// <param name="dpiY">out int for the vertical dpi</param>
/// <returns>true if all okay</returns>
[DllImport("shcore")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetDpiForMonitor(IntPtr hMonitor, MonitorDpiType dpiType, out uint dpiX, out uint dpiY);
[DllImport("shcore.dll", SetLastError = true)]
private static extern HResult GetDpiForMonitor(IntPtr hMonitor, MonitorDpiType dpiType, out uint dpiX, out uint dpiY);
/// <summary>
/// See <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/mt748621(v=vs.85).aspx">EnableNonClientDpiScaling function</a>

View file

@ -1,20 +1,20 @@
/*
* 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/>.
*/
@ -32,6 +32,8 @@ using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using GreenshotPlugin.Hooking;
namespace GreenshotPlugin.Core {
/// <summary>
@ -41,16 +43,17 @@ namespace GreenshotPlugin.Core {
HMACSHA1,
PLAINTEXT,
}
/// <summary>
/// Specify the autorize mode that is used to get the token from the cloud service.
/// Specify the authorize mode that is used to get the token from the cloud service.
/// </summary>
public enum OAuth2AuthorizeMode {
Unknown, // Will give an exception, caller needs to specify another value
LocalServer, // Will specify a redirect URL to http://localhost:port/authorize, while having a HttpListener
MonitorTitle, // Not implemented yet: Will monitor for title changes
Pin, // Not implemented yet: Will ask the user to enter the shown PIN
EmbeddedBrowser // Will open into an embedded _browser (OAuthLoginForm), and catch the redirect
Unknown, // Will give an exception, caller needs to specify another value
LocalServer, // Will specify a redirect URL to http://localhost:port/authorize, while having a HttpListener
MonitorTitle, // Not implemented yet: Will monitor for title changes
Pin, // Not implemented yet: Will ask the user to enter the shown PIN
EmbeddedBrowser, // Will open into an embedded _browser (OAuthLoginForm), and catch the redirect
OutOfBoundAuto
}
/// <summary>
@ -211,7 +214,7 @@ namespace GreenshotPlugin.Core {
//
// List of know and used oauth parameters' names
//
//
protected const string OAUTH_CONSUMER_KEY_KEY = "oauth_consumer_key";
protected const string OAUTH_CALLBACK_KEY = "oauth_callback";
protected const string OAUTH_VERSION_KEY = "oauth_version";
@ -395,7 +398,7 @@ namespace GreenshotPlugin.Core {
}
/// <summary>
/// Generate the timestamp for the signature
/// Generate the timestamp for the signature
/// </summary>
/// <returns></returns>
public static string GenerateTimeStamp() {
@ -472,7 +475,7 @@ namespace GreenshotPlugin.Core {
/// <summary>
/// Get the access token
/// </summary>
/// <returns>The access token.</returns>
/// <returns>The access token.</returns>
private string GetAccessToken() {
if (string.IsNullOrEmpty(Token) || (CheckVerifier && string.IsNullOrEmpty(Verifier))) {
Exception e = new Exception("The request token and verifier were not set");
@ -1121,12 +1124,43 @@ Greenshot received information from CloudServiceName. You can close this browser
{
OAuth2AuthorizeMode.LocalServer => AuthenticateViaLocalServer(settings),
OAuth2AuthorizeMode.EmbeddedBrowser => AuthenticateViaEmbeddedBrowser(settings),
_ => throw new NotImplementedException($"Authorize mode '{settings.AuthorizeMode}' is not 'yet' implemented."),
OAuth2AuthorizeMode.OutOfBoundAuto => AuthenticateViaDefaultBrowser(settings),
_ => throw new NotImplementedException($"Authorize mode '{settings.AuthorizeMode}' is not 'yet' implemented."),
};
return completed;
}
/// <summary>
/// <summary>
/// Authenticate via the default browser
/// If this works, return the code
/// </summary>
/// <param name="settings">OAuth2Settings with the Auth / Token url etc</param>
/// <returns>true if completed, false if canceled</returns>
private static bool AuthenticateViaDefaultBrowser(OAuth2Settings settings)
{
var monitor = new WindowsTitleMonitor();
string[] code = new string[1];
monitor.TitleChangeEvent += args =>
{
if (args.Title.Contains(settings.State))
{
code[0] = args.Title;
settings.Code = args.Title;
}
};
using (var process = Process.Start(settings.FormattedAuthUrl))
{
while (string.IsNullOrEmpty(code[0]))
{
Application.DoEvents();
}
};
return true;
}
/// <summary>
/// Authenticate via an embedded browser
/// If this works, return the code
/// </summary>
@ -1192,7 +1226,7 @@ Greenshot received information from CloudServiceName. You can close this browser
}
/// <summary>
/// Check and authenticate or refresh tokens
/// Check and authenticate or refresh tokens
/// </summary>
/// <param name="settings">OAuth2Settings</param>
public static void CheckAndAuthenticateOrRefresh(OAuth2Settings settings) {

View file

@ -1137,46 +1137,50 @@ namespace GreenshotPlugin.Core
/// <summary>
/// Set the window as foreground window
/// </summary>
/// <param name="handle">hWnd of the window to bring to the foreground</param>
/// <param name="workaround">bool with true to use a trick to really bring the window to the foreground</param>
public static void ToForeground(IntPtr handle, bool workaround = true)
/// <param name="hWnd">hWnd of the window to bring to the foreground</param>
public static void ToForeground(IntPtr hWnd)
{
var window = new WindowDetails(handle);
var foregroundWindow = User32.GetForegroundWindow();
if (hWnd == foregroundWindow)
{
return;
}
var window = new WindowDetails(hWnd);
// Nothing we can do if it's not visible!
if (!window.Visible)
{
return;
}
var threadId1 = User32.GetWindowThreadProcessId(foregroundWindow, IntPtr.Zero);
var threadId2 = User32.GetWindowThreadProcessId(hWnd, IntPtr.Zero);
// Show window in foreground.
if (threadId1 != threadId2)
{
User32.AttachThreadInput(threadId1, threadId2, 1);
User32.SetForegroundWindow(hWnd);
User32.AttachThreadInput(threadId1, threadId2, 0);
}
else
{
User32.SetForegroundWindow(hWnd);
}
User32.BringWindowToTop(hWnd);
if (window.Iconic)
{
window.Iconic = false;
while (window.Iconic)
{
Application.DoEvents();
}
}
// See https://msdn.microsoft.com/en-us/library/windows/desktop/ms633539(v=vs.85).aspx
if (workaround)
{
const byte alt = 0xA4;
const int extendedKey = 0x1;
const int keyup = 0x2;
// Simulate an "ALT" key press.
User32.keybd_event(alt, 0x45, extendedKey | 0, 0);
// Simulate an "ALT" key release.
User32.keybd_event(alt, 0x45, extendedKey | keyup, 0);
}
// Show window in forground.
User32.BringWindowToTop(handle);
User32.SetForegroundWindow(handle);
}
/// <summary>
/// Set the window as foreground window
/// </summary>
/// <param name="workaround">true to use a workaround, otherwise the window might only flash</param>
public void ToForeground(bool workaround = true) {
ToForeground(Handle, workaround);
public void ToForeground() {
ToForeground(Handle);
}
/// <summary>