-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisplayInfo.cs
170 lines (140 loc) · 7.8 KB
/
DisplayInfo.cs
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Management;
using BroDisplaySetup.DPI;
using BroDisplaySetup.Extern;
using Microsoft.Win32;
namespace BroDisplaySetup
{
class DisplayInfo
{
public string UserFriendlyName { get; set; }
public string Serial { get; set; }
public string ProductCodeID { get; set; }
public string Manufacturer { get; set; }
public string PnpDeviceId { get; set; }
public string InstanceName { get; set; }
public string VideoOutputTechnology { get; set; }
public bool Internal { get; set; }
public string DeviceName { get; set; }
public Rectangle Bounds { get; set; }
public Size OptimalResolution { get; set; }
public DPIScalingInfo DpiScalingInfo { get; set; }
public override string ToString()
{
string internalStringValue = Internal ? "Yes" : "No";
return $"UserFriendlyName: {UserFriendlyName}{Environment.NewLine}" +
$"DeviceName: {DeviceName}{Environment.NewLine}" +
$"Serial: {Serial}{Environment.NewLine}" +
$"ProductCodeID: {ProductCodeID}{Environment.NewLine}" +
$"Manufacturer: {Manufacturer}{Environment.NewLine}" +
$"PnpDeviceId: {PnpDeviceId}{Environment.NewLine}" +
$"InstanceName: {InstanceName}{Environment.NewLine}" +
$"VideoOutputTechnology: {VideoOutputTechnology}{Environment.NewLine}" +
$"Internal: {internalStringValue}{Environment.NewLine}" +
$"Bounds: {Bounds}{Environment.NewLine}" +
$"OptimalResolution: {OptimalResolution.Width} x {OptimalResolution.Height}{Environment.NewLine}" +
$"DpiScalingInfo: {DpiScalingInfo.Current}% (min: {DpiScalingInfo.Minimum}%, max: {DpiScalingInfo.Maximum}%, recommended: {DpiScalingInfo.Recommended}%)";
}
public static List<DisplayInfo> GetDisplayInfoForAllConnectedDisplayDevices()
{
var displayPnpIdToDisplayName = Extern.Displays.GetDisplayDeviceIdToDisplayDeviceNameMapping();
foreach (KeyValuePair<string, string> kvp in displayPnpIdToDisplayName)
{
System.Diagnostics.Debug.WriteLine("{0}={1}", kvp.Key, kvp.Value);
}
var displayInfoList = new List<DisplayInfo>();
var scope = new ManagementScope("root\\wmi");
var query = new ObjectQuery("SELECT * FROM WmiMonitorID");
var searcher = new ManagementObjectSearcher(scope, query);
var wmiMonitorCollection = searcher.Get();
Dictionary<string, Extern.DISPLAYCONFIG_VIDEO_OUTPUT_TECHNOLOGY> displayOutputTechnologies = Extern.Displays.GetVideoOutputTechnologyByDevicePathMap();
foreach (var tech in displayOutputTechnologies)
{
System.Diagnostics.Debug.WriteLine("{0}={1}", tech.Key, tech.Value);
}
IEnumerable<KeyValuePair<string, DPIScalingInfo>> dpiScales = DPI.DPIHelper.GetDpiScalingInfoByDevicePathMap();
foreach (var dpi in dpiScales)
{
System.Diagnostics.Debug.WriteLine("{0}={1} DPI (min:{2}, max:{3})", dpi.Key, dpi.Value.Current, dpi.Value.Minimum, dpi.Value.Maximum);
}
foreach (ManagementObject monitor in wmiMonitorCollection)
{
string userFriendlyName = DecodeName(monitor["UserFriendlyName"] as ushort[]);
string serial = DecodeName(monitor["SerialNumberID"] as ushort[]);
string productCodeID = DecodeName(monitor["ProductCodeID"] as ushort[]);
string manufacturerName = DecodeName(monitor["ManufacturerName"] as ushort[]);
string instanceName = (string)monitor["InstanceName"];
string monitorPnpDeviceId = ((string)monitor["InstanceName"]).Split('_')[0].ToUpper();
DPIScalingInfo deviceDPIScalingInfo = new DPIScalingInfo();
string displayProduct = monitorPnpDeviceId.Split('\\')[1];
string matchUser32DeviceId = monitorPnpDeviceId.Replace("\\", "#");
matchUser32DeviceId = "\\\\?\\" + matchUser32DeviceId;
String outputTechnology = "Unknown";
bool internalMonitor = false;
foreach (var tech in displayOutputTechnologies)
{
System.Diagnostics.Debug.WriteLine("{0} contains {1}", tech.Key.ToUpper(), monitorPnpDeviceId.Replace("\\", "#"));
if (tech.Key.ToUpper().Contains(monitorPnpDeviceId.Replace("\\", "#")))
{
outputTechnology = tech.Value.ToString();
if (tech.Value == DISPLAYCONFIG_VIDEO_OUTPUT_TECHNOLOGY.DISPLAYCONFIG_OUTPUT_TECHNOLOGY_INTERNAL)
{
internalMonitor = true;
} else {
internalMonitor = false;
}
break;
}
}
foreach(var dpi in dpiScales)
{
System.Diagnostics.Debug.WriteLine("{0} contains {1}", dpi.Key.ToUpper(), monitorPnpDeviceId.Replace("\\", "#"));
if (dpi.Key.ToUpper().Contains(monitorPnpDeviceId.Replace("\\", "#")))
{
System.Diagnostics.Debug.WriteLine("Found DPI scaling for {0}", monitorPnpDeviceId);
System.Diagnostics.Debug.WriteLine("DPI scaling is {0}", dpi.Value.Current);
deviceDPIScalingInfo = dpi.Value;
break;
}
}
var displayInfo = new DisplayInfo
{
UserFriendlyName = userFriendlyName,
Serial = serial,
ProductCodeID = productCodeID,
Manufacturer = manufacturerName,
PnpDeviceId = monitorPnpDeviceId,
InstanceName = instanceName,
VideoOutputTechnology = outputTechnology,
Internal = internalMonitor,
DpiScalingInfo = deviceDPIScalingInfo
};
System.Diagnostics.Debug.WriteLine("Try matching pnp device id to known displays {0}", matchUser32DeviceId);
string user32DeviceId = displayPnpIdToDisplayName.Keys.FirstOrDefault(x => x.ToUpper().StartsWith(matchUser32DeviceId));
System.Diagnostics.Debug.WriteLine("Found matching device id:" + user32DeviceId);
String deviceName = displayPnpIdToDisplayName[user32DeviceId];
displayInfo.DeviceName = deviceName;
DEVMODE currentDevMode = Extern.Displays.GetCurrentDisplayMode(deviceName);
displayInfo.Bounds = new Rectangle(currentDevMode.dmPosition.x, currentDevMode.dmPosition.y, (int)currentDevMode.dmPelsWidth, (int)currentDevMode.dmPelsHeight);
Extern.DEVMODE optimalDevMode = Extern.Displays.GetOptimalDisplayMode(deviceName);
displayInfo.OptimalResolution = new Size((int)optimalDevMode.dmPelsWidth, (int)optimalDevMode.dmPelsHeight);
foreach (DisplayInfo d in displayInfoList)
{
System.Diagnostics.Debug.Write("DisplayInfo: " + d.ToString());
}
displayInfoList.Add(displayInfo);
}
return displayInfoList;
}
private static string DecodeName(ushort[] input)
{
if (input == null)
return null;
return new string(input.Where(x => x != 0).Select(x => (char)x).ToArray());
}
}
}