Last active
May 24, 2024 16:37
-
-
Save yamamaya/4a4c3051fa03a2425549f10ef11dafd5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
namespace OaktreeLab.Utils { | |
internal class DisplayHelper { | |
public static uint GetRefreshRate( IntPtr hWnd ) { | |
IntPtr hmonitor = MonitorFromWindow( hWnd, MONITOR_DEFAULTTONEAREST ); | |
MONITORINFOEXW monitorInfo = new MONITORINFOEXW(); | |
monitorInfo.cbSize = (uint)Marshal.SizeOf<MONITORINFOEXW>(); | |
GetMonitorInfoW( hmonitor, ref monitorInfo ); | |
DEVMODEW devMode = new DEVMODEW(); | |
devMode.dmSize = (ushort)Marshal.SizeOf<DEVMODEW>(); | |
EnumDisplaySettingsW( monitorInfo.szDevice, ENUM_CURRENT_SETTINGS, out devMode ); | |
return devMode.dmDisplayFrequency; | |
} | |
private const uint MONITOR_DEFAULTTONEAREST = 2; | |
[DllImport( "user32.dll", SetLastError = false )] | |
private static extern IntPtr MonitorFromWindow( IntPtr hWnd, uint dwFlags ); | |
[StructLayout( LayoutKind.Sequential )] | |
private struct RECT { | |
public int left; | |
public int top; | |
public int right; | |
public int bottom; | |
} | |
[StructLayout( LayoutKind.Sequential, CharSet = CharSet.Unicode )] | |
private unsafe struct MONITORINFOEXW { | |
public uint cbSize; | |
public RECT rcMonitor; | |
public RECT rcWork; | |
public uint dwFlags; | |
[MarshalAs( UnmanagedType.ByValTStr, SizeConst = 32 )] | |
public string szDevice; | |
} | |
[DllImport( "user32.dll", SetLastError = false, CharSet = CharSet.Unicode )] | |
[return: MarshalAs( UnmanagedType.Bool )] | |
private static extern bool GetMonitorInfoW( IntPtr hMonitor, ref MONITORINFOEXW lpmi ); | |
private const uint ENUM_CURRENT_SETTINGS = unchecked((uint)-1); | |
[DllImport( "user32.dll", SetLastError = false, CharSet = CharSet.Unicode )] | |
[return: MarshalAs( UnmanagedType.Bool )] | |
private static extern bool EnumDisplaySettingsW( [MarshalAs( UnmanagedType.LPWStr )] string lpszDeviceName, uint iModeNum, out DEVMODEW lpDevMode ); | |
[StructLayout( LayoutKind.Sequential, CharSet = CharSet.Unicode )] | |
private struct DEVMODEW { | |
[MarshalAs( UnmanagedType.ByValTStr, SizeConst = 32 )] | |
public string dmDeviceName; | |
public ushort dmSpecVersion; | |
public ushort dmDriverVersion; | |
public ushort dmSize; | |
public ushort dmDriverExtra; | |
public uint dmFields; | |
public int dmPositionX; | |
public int dmPositionY; | |
public uint dmDisplayOrientation; | |
public uint dmDisplayFixedOutput; | |
public short dmColor; | |
public short dmDuplex; | |
public short dmYResolution; | |
public short dmTTOption; | |
public short dmCollate; | |
[MarshalAs( UnmanagedType.ByValTStr, SizeConst = 32 )] | |
public string dmFormName; | |
public short dmLogPixels; | |
public uint dmBitsPerPel; | |
public uint dmPelsWidth; | |
public uint dmPelsHeight; | |
public uint dmNupOrDisplayFlags; | |
public uint dmDisplayFrequency; | |
public uint dmICMMethod; | |
public uint dmICMIntent; | |
public uint dmMediaType; | |
public uint dmDitherType; | |
public uint dmReserved1; | |
public uint dmReserved2; | |
public uint dmPanningWidth; | |
public uint dmPanningHeight; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment