Repository navigation
Expand file tree
/
Copy pathMonitorService.cs
More file actions
113 lines (102 loc) · 4.13 KB
/
Copy pathMonitorService.cs
File metadata and controls
113 lines (102 loc) · 4.13 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
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace MonitorSwitcher
{
// A lightweight description of one attached display.
internal class MonitorInfo
{
public int Index; // 1-based, matches the Ctrl+Alt+<n> hotkeys
public string DeviceName; // \\.\DISPLAY1
public string AdapterString; // e.g. "NVIDIA GeForce RTX ..."
public string MonitorString; // e.g. "Generic PnP Monitor" / model
public int X;
public int Y;
public int Width;
public int Height;
public bool IsPrimary;
}
// Enumerates displays and changes which one is the Windows primary monitor,
// using only the documented Win32 display APIs.
internal static class MonitorService
{
public static List<MonitorInfo> GetMonitors()
{
var list = new List<MonitorInfo>();
uint i = 0;
int index = 0;
while (true)
{
var dd = new NativeMethods.DISPLAY_DEVICE();
dd.cb = Marshal.SizeOf(typeof(NativeMethods.DISPLAY_DEVICE));
if (!NativeMethods.EnumDisplayDevices(null, i, ref dd, 0))
break;
i++;
bool attached = (dd.StateFlags & NativeMethods.DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) != 0;
bool mirroring = (dd.StateFlags & NativeMethods.DISPLAY_DEVICE_MIRRORING_DRIVER) != 0;
if (!attached || mirroring)
continue;
var dm = new NativeMethods.DEVMODE();
dm.dmSize = (ushort)Marshal.SizeOf(typeof(NativeMethods.DEVMODE));
if (!NativeMethods.EnumDisplaySettings(dd.DeviceName,
NativeMethods.ENUM_CURRENT_SETTINGS, ref dm))
continue;
index++;
var info = new MonitorInfo();
info.Index = index;
info.DeviceName = dd.DeviceName;
info.AdapterString = dd.DeviceString;
info.MonitorString = GetMonitorName(dd.DeviceName);
info.X = dm.dmPositionX;
info.Y = dm.dmPositionY;
info.Width = (int)dm.dmPelsWidth;
info.Height = (int)dm.dmPelsHeight;
info.IsPrimary = (dm.dmPositionX == 0 && dm.dmPositionY == 0);
list.Add(info);
}
return list;
}
// Friendly monitor name (the child device of an adapter), if available.
private static string GetMonitorName(string adapterDeviceName)
{
var dd = new NativeMethods.DISPLAY_DEVICE();
dd.cb = Marshal.SizeOf(typeof(NativeMethods.DISPLAY_DEVICE));
if (NativeMethods.EnumDisplayDevices(adapterDeviceName, 0, ref dd, 0))
return dd.DeviceString;
return "";
}
// Makes the monitor with the given device name the primary display.
// Uses the modern CCD API (SetDisplayConfig); the legacy
// ChangeDisplaySettingsEx path validates the mode but refuses to commit a
// primary/topology change on Windows 10/11 (returns DISP_CHANGE_FAILED).
public static bool SetPrimary(string targetDeviceName)
{
var monitors = GetMonitors();
MonitorInfo target = null;
foreach (var m in monitors)
{
if (m.DeviceName == targetDeviceName)
{
target = m;
break;
}
}
if (target == null)
return false;
if (target.IsPrimary)
return true;
// The primary is the source at (0,0); CCD shifts every source so the
// target (currently at target.X/target.Y) moves there.
return CcdNative.SetPrimaryByPosition(target.X, target.Y);
}
public static MonitorInfo FindByIndex(int index)
{
foreach (var m in GetMonitors())
{
if (m.Index == index)
return m;
}
return null;
}
}
}