Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
SHCore p/invoke work
Browse files Browse the repository at this point in the history
* Also fix capitalization of `GetDpiForShellUIComponent`.
* Add `GetScaleFactorForMonitor`
* Add `RegisterScaleChangeEvent`
* Add `UnregisterScaleChangeEvent`
* Remove `SetLastError` from SHCore functions
  • Loading branch information
AArnott committed Jun 14, 2020
1 parent 734269d commit 4b7a4ad
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/SHCore/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -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
44 changes: 39 additions & 5 deletions src/SHCore/SHCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static partial class SHCore
/// <item><see cref="PROCESS_DPI_AWARENESS.PROCESS_PER_MONITOR_DPI_AWARE"/>: The actual DPI value set by the user for that display</item>
/// </list>
/// </remarks>
[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);

/// <summary>
Expand All @@ -61,7 +61,7 @@ public static partial class SHCore
/// <item><see cref="HResult.Code.E_ACCESSDENIED"/>: The application does not have sufficient privileges</item>
/// </list>
/// </returns>
[DllImport(nameof(SHCore), SetLastError = true)]
[DllImport(nameof(SHCore))]
public static extern HResult GetProcessDpiAwareness(IntPtr hprocess, out PROCESS_DPI_AWARENESS value);

/// <summary>
Expand Down Expand Up @@ -90,15 +90,49 @@ public static partial class SHCore
/// If the DPI awareness level is not set, the default value is <see cref="PROCESS_DPI_AWARENESS.PROCESS_DPI_UNAWARE"/>.
/// </para>
/// </remarks>
[DllImport(nameof(SHCore), SetLastError = true)]
[DllImport(nameof(SHCore))]
public static extern HResult SetProcessDpiAwareness(PROCESS_DPI_AWARENESS value);

/// <summary>
/// Retrieves the dots per inch (dpi) occupied by a <see cref="SHELL_UI_COMPONENT"/> based on the current scale factor and <see cref="PROCESS_DPI_AWARENESS"/>.
/// </summary>
/// <param name="component">The type of shell component</param>
/// <returns>The DPI required for an icon of this type</returns>
[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);

/// <summary>
/// Gets the scale factor of a specific monitor. This function replaces GetScaleFactorForDevice.
/// </summary>
/// <param name="hMon">The monitor's handle.</param>
/// <param name="pScale">
/// When this function returns successfully, this value points to one of the <see cref="DEVICE_SCALE_FACTOR"/> 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.
/// </param>
/// <returns>If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.</returns>
[DllImport(nameof(SHCore))]
public static extern HResult GetScaleFactorForMonitor(IntPtr hMon, out DEVICE_SCALE_FACTOR pScale);

/// <summary>
/// Registers for an event that is triggered when the scale has possibly changed.
/// This function replaces RegisterScaleChangeNotifications.
/// </summary>
/// <param name="hEvent">Handle of the event to register for scale change notifications.</param>
/// <param name="pdwCookie">
/// 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 <see cref="UnregisterScaleChangeEvent"/>.
/// </param>
/// <returns>If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.</returns>
[DllImport(nameof(SHCore))]
public static extern HResult RegisterScaleChangeEvent(
IntPtr hEvent,
out IntPtr pdwCookie);

/// <summary>
/// Unregisters for the scale change event registered through <see cref="RegisterScaleChangeEvent"/>. This function replaces RevokeScaleChangeNotifications.
/// </summary>
/// <param name="dwCookie">A pointer to the cookie retrieved in the call to <see cref="RegisterScaleChangeEvent"/>.</param>
/// <returns>If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.</returns>
[DllImport(nameof(SHCore))]
public static extern HResult UnregisterScaleChangeEvent(IntPtr dwCookie);
}
}
18 changes: 18 additions & 0 deletions src/Windows.ShellScalingApi/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/Windows.ShellScalingApi/ShellScalingApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ namespace PInvoke
[OfferFriendlyOverloads]
public partial class ShellScalingApi
{
// SHCore functions are defined in the SHCore library.
}
}
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Indicates a spoofed device scale factor, as a percent.
/// </summary>
public enum DEVICE_SCALE_FACTOR
{
DEVICE_SCALE_FACTOR_INVALID = 0,

/// <summary>
/// 100%. The scale factor for the device is 1x.
/// </summary>
SCALE_100_PERCENT = 100,

/// <summary>
/// 120%. The scale factor for the device is 1.2x.
/// </summary>
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,
}
}

0 comments on commit 4b7a4ad

Please sign in to comment.