From 4b7a4ad8228d7fbeabd583c87e70c39320ca15d4 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Sun, 14 Jun 2020 11:21:17 -0600 Subject: [PATCH] SHCore p/invoke work * Also fix capitalization of `GetDpiForShellUIComponent`. * Add `GetScaleFactorForMonitor` * Add `RegisterScaleChangeEvent` * Add `UnregisterScaleChangeEvent` * Remove `SetLastError` from SHCore functions --- src/SHCore/PublicAPI.Unshipped.txt | 4 ++ src/SHCore/SHCore.cs | 44 ++++++++++++++++--- .../PublicAPI.Unshipped.txt | 18 ++++++++ .../ShellScalingApi.cs | 1 + .../ShellScallingApi+DEVICE_SCALE_FACTOR.cs | 40 +++++++++++++++++ 5 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 src/Windows.ShellScalingApi/ShellScallingApi+DEVICE_SCALE_FACTOR.cs diff --git a/src/SHCore/PublicAPI.Unshipped.txt b/src/SHCore/PublicAPI.Unshipped.txt index e69de29b..e7d37be5 100644 --- a/src/SHCore/PublicAPI.Unshipped.txt +++ b/src/SHCore/PublicAPI.Unshipped.txt @@ -0,0 +1,4 @@ +static extern PInvoke.SHCore.GetDpiForShellUIComponent(PInvoke.SHELL_UI_COMPONENT component) -> int +static extern PInvoke.SHCore.GetScaleFactorForMonitor(System.IntPtr hMon, out PInvoke.DEVICE_SCALE_FACTOR pScale) -> PInvoke.HResult +static extern PInvoke.SHCore.RegisterScaleChangeEvent(System.IntPtr hEvent, out System.IntPtr pdwCookie) -> PInvoke.HResult +static extern PInvoke.SHCore.UnregisterScaleChangeEvent(System.IntPtr dwCookie) -> PInvoke.HResult \ No newline at end of file diff --git a/src/SHCore/SHCore.cs b/src/SHCore/SHCore.cs index 8969b433..3743b72c 100644 --- a/src/SHCore/SHCore.cs +++ b/src/SHCore/SHCore.cs @@ -45,7 +45,7 @@ public static partial class SHCore /// : The actual DPI value set by the user for that display /// /// - [DllImport(nameof(SHCore), SetLastError = true)] + [DllImport(nameof(SHCore))] public static extern HResult GetDpiForMonitor(IntPtr hmonitor, MONITOR_DPI_TYPE dpiType, out int dpiX, out int dpiY); /// @@ -61,7 +61,7 @@ public static partial class SHCore /// : The application does not have sufficient privileges /// /// - [DllImport(nameof(SHCore), SetLastError = true)] + [DllImport(nameof(SHCore))] public static extern HResult GetProcessDpiAwareness(IntPtr hprocess, out PROCESS_DPI_AWARENESS value); /// @@ -90,7 +90,7 @@ public static partial class SHCore /// If the DPI awareness level is not set, the default value is . /// /// - [DllImport(nameof(SHCore), SetLastError = true)] + [DllImport(nameof(SHCore))] public static extern HResult SetProcessDpiAwareness(PROCESS_DPI_AWARENESS value); /// @@ -98,7 +98,41 @@ public static partial class SHCore /// /// The type of shell component /// The DPI required for an icon of this type - [DllImport(nameof(SHCore), SetLastError = true)] - public static extern int GetDpiForShellUiComponent(SHELL_UI_COMPONENT component); + [DllImport(nameof(SHCore))] + public static extern int GetDpiForShellUIComponent(SHELL_UI_COMPONENT component); + + /// + /// Gets the scale factor of a specific monitor. This function replaces GetScaleFactorForDevice. + /// + /// The monitor's handle. + /// + /// When this function returns successfully, this value points to one of the values that specify the scale factor of the specified monitor. + /// If the function call fails, this value points to a valid scale factor so that apps can opt to continue on with incorrectly sized resources. + /// + /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. + [DllImport(nameof(SHCore))] + public static extern HResult GetScaleFactorForMonitor(IntPtr hMon, out DEVICE_SCALE_FACTOR pScale); + + /// + /// Registers for an event that is triggered when the scale has possibly changed. + /// This function replaces RegisterScaleChangeNotifications. + /// + /// Handle of the event to register for scale change notifications. + /// + /// When this function returns successfully, this value receives the address of a pointer to a cookie that can be used later to unregister for the scale change notifications through . + /// + /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. + [DllImport(nameof(SHCore))] + public static extern HResult RegisterScaleChangeEvent( + IntPtr hEvent, + out IntPtr pdwCookie); + + /// + /// Unregisters for the scale change event registered through . This function replaces RevokeScaleChangeNotifications. + /// + /// A pointer to the cookie retrieved in the call to . + /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. + [DllImport(nameof(SHCore))] + public static extern HResult UnregisterScaleChangeEvent(IntPtr dwCookie); } } diff --git a/src/Windows.ShellScalingApi/PublicAPI.Unshipped.txt b/src/Windows.ShellScalingApi/PublicAPI.Unshipped.txt index e69de29b..edf42e73 100644 --- a/src/Windows.ShellScalingApi/PublicAPI.Unshipped.txt +++ b/src/Windows.ShellScalingApi/PublicAPI.Unshipped.txt @@ -0,0 +1,18 @@ +PInvoke.DEVICE_SCALE_FACTOR +PInvoke.DEVICE_SCALE_FACTOR.DEVICE_SCALE_FACTOR_INVALID = 0 -> PInvoke.DEVICE_SCALE_FACTOR +PInvoke.DEVICE_SCALE_FACTOR.SCALE_100_PERCENT = 100 -> PInvoke.DEVICE_SCALE_FACTOR +PInvoke.DEVICE_SCALE_FACTOR.SCALE_120_PERCENT = 120 -> PInvoke.DEVICE_SCALE_FACTOR +PInvoke.DEVICE_SCALE_FACTOR.SCALE_125_PERCENT = 125 -> PInvoke.DEVICE_SCALE_FACTOR +PInvoke.DEVICE_SCALE_FACTOR.SCALE_140_PERCENT = 140 -> PInvoke.DEVICE_SCALE_FACTOR +PInvoke.DEVICE_SCALE_FACTOR.SCALE_150_PERCENT = 150 -> PInvoke.DEVICE_SCALE_FACTOR +PInvoke.DEVICE_SCALE_FACTOR.SCALE_160_PERCENT = 160 -> PInvoke.DEVICE_SCALE_FACTOR +PInvoke.DEVICE_SCALE_FACTOR.SCALE_175_PERCENT = 175 -> PInvoke.DEVICE_SCALE_FACTOR +PInvoke.DEVICE_SCALE_FACTOR.SCALE_180_PERCENT = 180 -> PInvoke.DEVICE_SCALE_FACTOR +PInvoke.DEVICE_SCALE_FACTOR.SCALE_200_PERCENT = 200 -> PInvoke.DEVICE_SCALE_FACTOR +PInvoke.DEVICE_SCALE_FACTOR.SCALE_225_PERCENT = 225 -> PInvoke.DEVICE_SCALE_FACTOR +PInvoke.DEVICE_SCALE_FACTOR.SCALE_250_PERCENT = 250 -> PInvoke.DEVICE_SCALE_FACTOR +PInvoke.DEVICE_SCALE_FACTOR.SCALE_300_PERCENT = 300 -> PInvoke.DEVICE_SCALE_FACTOR +PInvoke.DEVICE_SCALE_FACTOR.SCALE_350_PERCENT = 350 -> PInvoke.DEVICE_SCALE_FACTOR +PInvoke.DEVICE_SCALE_FACTOR.SCALE_400_PERCENT = 400 -> PInvoke.DEVICE_SCALE_FACTOR +PInvoke.DEVICE_SCALE_FACTOR.SCALE_450_PERCENT = 450 -> PInvoke.DEVICE_SCALE_FACTOR +PInvoke.DEVICE_SCALE_FACTOR.SCALE_500_PERCENT = 500 -> PInvoke.DEVICE_SCALE_FACTOR \ No newline at end of file diff --git a/src/Windows.ShellScalingApi/ShellScalingApi.cs b/src/Windows.ShellScalingApi/ShellScalingApi.cs index 52bb035a..22e53131 100644 --- a/src/Windows.ShellScalingApi/ShellScalingApi.cs +++ b/src/Windows.ShellScalingApi/ShellScalingApi.cs @@ -9,5 +9,6 @@ namespace PInvoke [OfferFriendlyOverloads] public partial class ShellScalingApi { + // SHCore functions are defined in the SHCore library. } } diff --git a/src/Windows.ShellScalingApi/ShellScallingApi+DEVICE_SCALE_FACTOR.cs b/src/Windows.ShellScalingApi/ShellScallingApi+DEVICE_SCALE_FACTOR.cs new file mode 100644 index 00000000..18ecfc38 --- /dev/null +++ b/src/Windows.ShellScalingApi/ShellScallingApi+DEVICE_SCALE_FACTOR.cs @@ -0,0 +1,40 @@ +// Copyright © .NET Foundation and Contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace PInvoke +{ + using System; + using System.Runtime.InteropServices; + + /// + /// Indicates a spoofed device scale factor, as a percent. + /// + public enum DEVICE_SCALE_FACTOR + { + DEVICE_SCALE_FACTOR_INVALID = 0, + + /// + /// 100%. The scale factor for the device is 1x. + /// + SCALE_100_PERCENT = 100, + + /// + /// 120%. The scale factor for the device is 1.2x. + /// + SCALE_120_PERCENT = 120, + SCALE_125_PERCENT = 125, + SCALE_140_PERCENT = 140, + SCALE_150_PERCENT = 150, + SCALE_160_PERCENT = 160, + SCALE_175_PERCENT = 175, + SCALE_180_PERCENT = 180, + SCALE_200_PERCENT = 200, + SCALE_225_PERCENT = 225, + SCALE_250_PERCENT = 250, + SCALE_300_PERCENT = 300, + SCALE_350_PERCENT = 350, + SCALE_400_PERCENT = 400, + SCALE_450_PERCENT = 450, + SCALE_500_PERCENT = 500, + } +}