Skip to content

Commit

Permalink
Cleanup GetSystemMetrics
Browse files Browse the repository at this point in the history
  • Loading branch information
hughbe committed Jul 29, 2019
1 parent 430bfa2 commit 3aeab29
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 391 deletions.
120 changes: 120 additions & 0 deletions src/Common/src/Interop/User32/Interop.GetSystemMetrics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

internal static partial class Interop
{
internal static partial class User32
{
public enum SystemMetric
{
SM_CXSCREEN = 0,
SM_CYSCREEN = 1,
SM_CXVSCROLL = 2,
SM_CYHSCROLL = 3,
SM_CYCAPTION = 4,
SM_CXBORDER = 5,
SM_CYBORDER = 6,
SM_CXFIXEDFRAME = 7,
SM_CYFIXEDFRAME = 8,
SM_CYVTHUMB = 9,
SM_CXHTHUMB = 10,
SM_CXICON = 11,
SM_CYICON = 12,
SM_CXCURSOR = 13,
SM_CYCURSOR = 14,
SM_CYMENU = 15,
SM_CYKANJIWINDOW = 18,
SM_MOUSEPRESENT = 19,
SM_CYVSCROLL = 20,
SM_CXHSCROLL = 21,
SM_DEBUG = 22,
SM_SWAPBUTTON = 23,
SM_CXMIN = 28,
SM_CYMIN = 29,
SM_CXSIZE = 30,
SM_CYSIZE = 31,
SM_CXFRAME = 32,
SM_CYFRAME = 33,
SM_CXMINTRACK = 34,
SM_CYMINTRACK = 35,
SM_CXDOUBLECLK = 36,
SM_CYDOUBLECLK = 37,
SM_CXICONSPACING = 38,
SM_CYICONSPACING = 39,
SM_MENUDROPALIGNMENT = 40,
SM_PENWINDOWS = 41,
SM_DBCSENABLED = 42,
SM_CMOUSEBUTTONS = 43,
SM_SECURE = 44,
SM_CXEDGE = 45,
SM_CYEDGE = 46,
SM_CXMINSPACING = 47,
SM_CYMINSPACING = 48,
SM_CXSMICON = 49,
SM_CYSMICON = 50,
SM_CYSMCAPTION = 51,
SM_CXSMSIZE = 52,
SM_CYSMSIZE = 53,
SM_CXMENUSIZE = 54,
SM_CYMENUSIZE = 55,
SM_ARRANGE = 56,
SM_CXMINIMIZED = 57,
SM_CYMINIMIZED = 58,
SM_CXMAXTRACK = 59,
SM_CYMAXTRACK = 60,
SM_CXMAXIMIZED = 61,
SM_CYMAXIMIZED = 62,
SM_NETWORK = 63,
SM_CLEANBOOT = 67,
SM_CXDRAG = 68,
SM_CYDRAG = 69,
SM_SHOWSOUNDS = 70,
SM_CXMENUCHECK = 71,
SM_CYMENUCHECK = 72,
SM_MIDEASTENABLED = 74,
SM_MOUSEWHEELPRESENT = 75,
SM_XVIRTUALSCREEN = 76,
SM_YVIRTUALSCREEN = 77,
SM_CXVIRTUALSCREEN = 78,
SM_CYVIRTUALSCREEN = 79,
SM_CMONITORS = 80,
SM_SAMEDISPLAYFORMAT = 81,
SM_CYFOCUSBORDER = 84,
SM_CXFOCUSBORDER = 83,
SM_REMOTESESSION = 0x1000,
SM_CXSIZEFRAME = SM_CXFRAME,
SM_CYSIZEFRAME = SM_CYFRAME
}

[DllImport(Libraries.User32, ExactSpelling = true)]
public static extern int GetSystemMetrics(SystemMetric nIndex);

[DllImport(Libraries.User32, ExactSpelling = true)]
private static extern int GetSystemMetricsForDpi(SystemMetric nIndex, uint dpi);

/// <summary>
/// Tries to get system metrics for the dpi. dpi is ignored if "GetSystemMetricsForDpi" is not available on the OS that this application is running.
/// </summary>
/// <param name="nIndex">index</param>
/// <param name="dpi">dpi requested</param>
/// <returns>returns system metrics for dpi</returns>
public static int GetCurrentSystemMetrics(SystemMetric nIndex, uint dpi)
{
if (OsVersion.IsWindows10_1607OrGreater)
{
return GetSystemMetricsForDpi(nIndex, dpi);
}
else
{
Debug.Fail("GetSystemMetricsForDpi() is not available on this OS");
return GetSystemMetrics(nIndex);
}
}
}
}
79 changes: 1 addition & 78 deletions src/Common/src/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,80 +1231,7 @@ public static int MAKELCID(int lgid, int sort)
SWP_SHOWWINDOW = 0x0040,
SWP_HIDEWINDOW = 0x0080,
SWP_DRAWFRAME = 0x0020,
SWP_NOOWNERZORDER = 0x0200,
SM_CXSCREEN = 0,
SM_CYSCREEN = 1,
SM_CXVSCROLL = 2,
SM_CYHSCROLL = 3,
SM_CYCAPTION = 4,
SM_CXBORDER = 5,
SM_CYBORDER = 6,
SM_CYVTHUMB = 9,
SM_CXHTHUMB = 10,
SM_CXICON = 11,
SM_CYICON = 12,
SM_CXCURSOR = 13,
SM_CYCURSOR = 14,
SM_CYMENU = 15,
SM_CYKANJIWINDOW = 18,
SM_MOUSEPRESENT = 19,
SM_CYVSCROLL = 20,
SM_CXHSCROLL = 21,
SM_DEBUG = 22,
SM_SWAPBUTTON = 23,
SM_CXMIN = 28,
SM_CYMIN = 29,
SM_CXSIZE = 30,
SM_CYSIZE = 31,
SM_CXFRAME = 32,
SM_CYFRAME = 33,
SM_CXMINTRACK = 34,
SM_CYMINTRACK = 35,
SM_CXDOUBLECLK = 36,
SM_CYDOUBLECLK = 37,
SM_CXICONSPACING = 38,
SM_CYICONSPACING = 39,
SM_MENUDROPALIGNMENT = 40,
SM_PENWINDOWS = 41,
SM_DBCSENABLED = 42,
SM_CMOUSEBUTTONS = 43,
SM_CXFIXEDFRAME = 7,
SM_CYFIXEDFRAME = 8,
SM_SECURE = 44,
SM_CXEDGE = 45,
SM_CYEDGE = 46,
SM_CXMINSPACING = 47,
SM_CYMINSPACING = 48,
SM_CXSMICON = 49,
SM_CYSMICON = 50,
SM_CYSMCAPTION = 51,
SM_CXSMSIZE = 52,
SM_CYSMSIZE = 53,
SM_CXMENUSIZE = 54,
SM_CYMENUSIZE = 55,
SM_ARRANGE = 56,
SM_CXMINIMIZED = 57,
SM_CYMINIMIZED = 58,
SM_CXMAXTRACK = 59,
SM_CYMAXTRACK = 60,
SM_CXMAXIMIZED = 61,
SM_CYMAXIMIZED = 62,
SM_NETWORK = 63,
SM_CLEANBOOT = 67,
SM_CXDRAG = 68,
SM_CYDRAG = 69,
SM_SHOWSOUNDS = 70,
SM_CXMENUCHECK = 71,
SM_CYMENUCHECK = 72,
SM_MIDEASTENABLED = 74,
SM_MOUSEWHEELPRESENT = 75,
SM_XVIRTUALSCREEN = 76,
SM_YVIRTUALSCREEN = 77,
SM_CXVIRTUALSCREEN = 78,
SM_CYVIRTUALSCREEN = 79,
SM_CMONITORS = 80,
SM_SAMEDISPLAYFORMAT = 81,
SM_REMOTESESSION = 0x1000;
SWP_NOOWNERZORDER = 0x0200;

public const int HLP_FILE = 1,
HLP_KEYWORD = 2,
Expand Down Expand Up @@ -1370,10 +1297,6 @@ public static int MAKELCID(int lgid, int sort)
SPI_GETANIMATION = 0x0048,
SPI_GETBORDER = 0x0005,
SPI_GETCARETWIDTH = 0x2006,
SM_CYFOCUSBORDER = 84,
SM_CXFOCUSBORDER = 83,
SM_CYSIZEFRAME = SM_CYFRAME,
SM_CXSIZEFRAME = SM_CXFRAME,
SPI_GETDRAGFULLWINDOWS = 38,
SPI_GETNONCLIENTMETRICS = 41,
SPI_GETWORKAREA = 48,
Expand Down
26 changes: 0 additions & 26 deletions src/Common/src/UnsafeNativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -676,35 +676,9 @@ public static extern IntPtr CallWindowProc(IntPtr wndProc, IntPtr hWnd, int msg,
[DllImport(ExternDll.User32, CharSet = CharSet.Unicode, ExactSpelling = true)]
public static extern bool GetClassInfoW(HandleRef hInstance, string lpClassName, ref NativeMethods.WNDCLASS lpWndClass);

[DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern int GetSystemMetrics(int nIndex);

// This API is available starting Windows10 RS1
[DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern int GetSystemMetricsForDpi(int nIndex, uint dpi);

[DllImport(ExternDll.Gdi32, CharSet = CharSet.Auto)]
public static extern bool GetTextMetrics(HandleRef hdc, NativeMethods.TEXTMETRIC tm);

/// <summary>
/// Tries to get system metrics for the dpi. dpi is ignored if "GetSystemMetricsForDpi" is not available on the OS that this application is running.
/// </summary>
/// <param name="nIndex">index</param>
/// <param name="dpi">dpi requested</param>
/// <returns>returns system metrics for dpi</returns>
public static int TryGetSystemMetricsForDpi(int nIndex, uint dpi)
{
if (OsVersion.IsWindows10_1607OrGreater)
{
return GetSystemMetricsForDpi(nIndex, dpi);
}
else
{
Debug.Assert(false, "GetSystemMetricsForDpi() is not available on this OS");
return GetSystemMetrics(nIndex);
}
}

[DllImport(ExternDll.User32, CharSet = CharSet.Unicode, ExactSpelling = true)]
private unsafe static extern bool SystemParametersInfoW(uint uiAction, uint uiParam, void* pvParam, uint fWinIni);

Expand Down
1 change: 1 addition & 0 deletions src/System.Windows.Forms/src/System.Windows.Forms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<Compile Include="..\..\Common\src\Interop\Shell32\Interop.SHGetPathFromIDListLongPath.cs" Link="Interop\Shell32\Interop.SHGetPathFromIDListLongPath.cs" />
<Compile Include="..\..\Common\src\Interop\Shell32\Interop.SHGetSpecialFolderLocation.cs" Link="Interop\Shell32\Interop.SHGetSpecialFolderLocation.cs" />
<Compile Include="..\..\Common\src\Interop\User32\Interop.GetDC.cs" Link="Interop\User32\Interop.GetDC.cs" />
<Compile Include="..\..\Common\src\Interop\User32\Interop.GetSystemMetrics.cs" Link="Interop\User32\Interop.GetSystemMetrics.cs" />
<Compile Include="..\..\Common\src\Interop\User32\Interop.GetUpdateRgn.cs" Link="Interop\User32\Interop.GetUpdateRgn.cs" />
<Compile Include="..\..\Common\src\Interop\User32\Interop.GetWindowText.cs" Link="Interop\User32\Interop.GetWindowText.cs" />
<Compile Include="..\..\Common\src\Interop\User32\Interop.SetWindowText.cs" Link="Interop\User32\Interop.SetWindowText.cs" />
Expand Down
7 changes: 3 additions & 4 deletions src/System.Windows.Forms/src/System/Windows/Forms/Cursor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,10 @@ public Size Size
if (cursorSize.IsEmpty)
{
cursorSize = new Size(
UnsafeNativeMethods.GetSystemMetrics(NativeMethods.SM_CXCURSOR),
UnsafeNativeMethods.GetSystemMetrics(NativeMethods.SM_CYCURSOR)
);

Interop.User32.GetSystemMetrics(Interop.User32.SystemMetric.SM_CXCURSOR),
Interop.User32.GetSystemMetrics(Interop.User32.SystemMetric.SM_CYCURSOR));
}

return cursorSize;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class Screen
private const int MONITOR_DEFAULTTONEAREST = 0x00000002;
private const int MONITORINFOF_PRIMARY = 0x00000001;

private static readonly bool multiMonitorSupport = (UnsafeNativeMethods.GetSystemMetrics(NativeMethods.SM_CMONITORS) != 0);
private static readonly bool multiMonitorSupport = (Interop.User32.GetSystemMetrics(Interop.User32.SystemMetric.SM_CMONITORS) != 0);
private static Screen[] screens;

internal Screen(IntPtr monitor) : this(monitor, IntPtr.Zero)
Expand Down
Loading

0 comments on commit 3aeab29

Please sign in to comment.