Repository navigation
Expand file tree
/
Copy pathNativeMethods.cs
More file actions
118 lines (99 loc) · 4.66 KB
/
Copy pathNativeMethods.cs
File metadata and controls
118 lines (99 loc) · 4.66 KB
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
110
111
112
113
114
115
116
117
118
using System;
using System.Runtime.InteropServices;
namespace MonitorSwitcher
{
// All Win32 P/Invoke signatures, structs and constants used by the app.
internal static class NativeMethods
{
// ---- Display enumeration / change (user32) ----
public const int ENUM_CURRENT_SETTINGS = -1;
public const uint DM_POSITION = 0x00000020;
public const uint CDS_UPDATEREGISTRY = 0x00000001;
public const uint CDS_SET_PRIMARY = 0x00000010;
public const uint CDS_NORESET = 0x10000000;
public const uint DISPLAY_DEVICE_ATTACHED_TO_DESKTOP = 0x00000001;
public const uint DISPLAY_DEVICE_PRIMARY_DEVICE = 0x00000004;
public const uint DISPLAY_DEVICE_MIRRORING_DRIVER = 0x00000008;
public const int DISP_CHANGE_SUCCESSFUL = 0;
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern bool EnumDisplayDevices(string lpDevice, uint iDevNum,
ref DISPLAY_DEVICE lpDisplayDevice, uint dwFlags);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern bool EnumDisplaySettings(string lpszDeviceName, int iModeNum,
ref DEVMODE lpDevMode);
// Overload that passes a DEVMODE by reference (queue a change for one display).
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int ChangeDisplaySettingsEx(string lpszDeviceName, ref DEVMODE lpDevMode,
IntPtr hwnd, uint dwflags, IntPtr lParam);
// Overload with a null DEVMODE (used to apply all queued changes at once).
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int ChangeDisplaySettingsEx(string lpszDeviceName, IntPtr lpDevMode,
IntPtr hwnd, uint dwflags, IntPtr lParam);
// ---- Global hotkeys (user32) ----
public const int WM_HOTKEY = 0x0312;
public const uint MOD_ALT = 0x0001;
public const uint MOD_CONTROL = 0x0002;
public const uint MOD_SHIFT = 0x0004;
public const uint MOD_WIN = 0x0008;
public const uint MOD_NOREPEAT = 0x4000;
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
// ---- DPI awareness (user32) ----
[DllImport("user32.dll")]
public static extern bool SetProcessDPIAware();
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct DISPLAY_DEVICE
{
public int cb;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceString;
public uint StateFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceKey;
}
// DEVMODEW laid out for the *display* branch of its internal union.
// Total marshaled size must be 220 bytes; dmSize is set to that at runtime.
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct DEVMODE
{
[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; // POINTL dmPosition.x
public int dmPositionY; // POINTL dmPosition.y
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 ushort dmLogPixels;
public uint dmBitsPerPel;
public uint dmPelsWidth;
public uint dmPelsHeight;
public uint dmDisplayFlags;
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;
}
}
}