1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| using System; using System.Drawing; using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Windows; using System.Windows.Forms;
namespace ColorPicker.Utils { internal class ScreenHelper {
private const string User32 = "user32.dll";
[DllImport(User32, CharSet = CharSet.Auto)] [ResourceExposure(ResourceScope.None)] public static extern bool GetMonitorInfo(HandleRef hmonitor, [In, Out] MONITORINFOEX info);
[DllImport(User32, ExactSpelling = true)] [ResourceExposure(ResourceScope.None)] public static extern bool EnumDisplayMonitors(HandleRef hdc, COMRECT rcClip, MonitorEnumProc lpfnEnum, IntPtr dwData);
public delegate bool MonitorEnumProc(IntPtr monitor, IntPtr hdc, IntPtr lprcMonitor, IntPtr lParam);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 4)] public class MONITORINFOEX { internal int cbSize = Marshal.SizeOf(typeof(MONITORINFOEX)); internal RECT rcMonitor = new RECT(); internal RECT rcWork = new RECT(); internal int dwFlags = 0; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] internal char[] szDevice = new char[32]; }
[StructLayout(LayoutKind.Sequential)] public struct RECT { public int left; public int top; public int right; public int bottom;
public RECT(Rect r) { left = (int)r.Left; top = (int)r.Top; right = (int)r.Right; bottom = (int)r.Bottom; } }
[StructLayout(LayoutKind.Sequential)] public class COMRECT { public int left; public int top; public int right; public int bottom; }
public static readonly HandleRef NullHandleRef = new HandleRef(null, IntPtr.Zero);
public static double GetScalingRatio() { var logicalHeight = GetLogicalHeight(); var actualHeight = GetActualHeight();
if (logicalHeight > 0 && actualHeight > 0) { return logicalHeight / actualHeight; }
return 1; }
private static double GetActualHeight() { return SystemParameters.PrimaryScreenHeight; }
private static double GetLogicalHeight() { var logicalHeight = 0.0;
MonitorEnumProc proc = (m, h, lm, lp) => { MONITORINFOEX info = new MONITORINFOEX(); GetMonitorInfo(new HandleRef(null, m), info);
if ((info.dwFlags & 0x00000001) != 0) { logicalHeight = info.rcMonitor.bottom - info.rcMonitor.top; }
return true; }; EnumDisplayMonitors(NullHandleRef, null, proc, IntPtr.Zero);
return logicalHeight; } } }
|