-
Notifications
You must be signed in to change notification settings - Fork 1
/
DisplayConfiguration.cs
187 lines (159 loc) · 8.58 KB
/
DisplayConfiguration.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
Copyright (c) 2015, Alexander Horn
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace Lux
{
class DisplayConfiguration
{
private const int MONITOR_DEFAULTTONEAREST = 2;
private const int PHYSICAL_MONITOR_DESCRIPTION_SIZE = 128;
private const int MC_CAPS_BRIGHTNESS = 0x2;
private const int MC_CAPS_CONTRAST = 0x4;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct PHYSICAL_MONITOR
{
public IntPtr hPhysicalMonitor;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = PHYSICAL_MONITOR_DESCRIPTION_SIZE)]
public String szPhysicalMonitorDescription;
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
int x;
int y;
}
[DllImport("user32.dll", SetLastError = true)]
private extern static bool GetCursorPos(out POINT lpPoint);
[DllImport("user32.dll", SetLastError = false)]
private extern static IntPtr MonitorFromPoint(POINT pt, uint dwFlags);
[DllImport("dxva2.dll", SetLastError = true)]
private extern static bool GetPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, uint dwPhysicalMonitorArraySize, [Out] PHYSICAL_MONITOR[] pPhysicalMonitorArray);
[DllImport("dxva2.dll", SetLastError = true)]
private extern static bool GetNumberOfPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, out uint pdwNumberOfPhysicalMonitors);
[DllImport("dxva2.dll", SetLastError = true)]
private extern static bool DestroyPhysicalMonitors(uint dwPhysicalMonitorArraySize, PHYSICAL_MONITOR[] pPhysicalMonitorArray);
[DllImport("dxva2.dll", SetLastError = true)]
private extern static bool GetMonitorCapabilities(IntPtr hMonitor, out uint pdwMonitorCapabilities, out uint pdwSupportedColorTemperatures);
[DllImport("dxva2.dll", SetLastError = true)]
private extern static bool GetMonitorBrightness(IntPtr hMonitor, out uint pdwMinimumBrightness, out uint pdwCurrentBrightness, out uint pdwMaximumBrightness);
[DllImport("dxva2.dll", SetLastError = true)]
private extern static bool SetMonitorBrightness(IntPtr hMonitor, uint dwNewBrightness);
[DllImport("dxva2.dll", SetLastError = true)]
private extern static bool GetMonitorContrast(IntPtr hMonitor, out uint pdwMinimumContrast, out uint pdwCurrentContrast, out uint pdwMaximumContrast);
[DllImport("dxva2.dll", SetLastError = true)]
private extern static bool SetMonitorContrast(IntPtr hMonitor, uint dwNewContrast);
public static IntPtr GetCurrentMonitor()
{
POINT point = new POINT();
if (!GetCursorPos(out point))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return MonitorFromPoint(point, MONITOR_DEFAULTTONEAREST);
}
public static PHYSICAL_MONITOR[] GetPhysicalMonitors(IntPtr hMonitor)
{
uint dwNumberOfPhysicalMonitors;
if (!GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, out dwNumberOfPhysicalMonitors))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
PHYSICAL_MONITOR[] physicalMonitorArray = new PHYSICAL_MONITOR[dwNumberOfPhysicalMonitors];
if (!GetPhysicalMonitorsFromHMONITOR(hMonitor, dwNumberOfPhysicalMonitors, physicalMonitorArray))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return physicalMonitorArray;
}
public static void DestroyPhysicalMonitors(PHYSICAL_MONITOR[] physicalMonitorArray)
{
if (!DestroyPhysicalMonitors((uint)physicalMonitorArray.Length, physicalMonitorArray))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
private static uint GetMonitorCapabilities(PHYSICAL_MONITOR physicalMonitor)
{
uint dwMonitorCapabilities, dwSupportedColorTemperatures;
if (!GetMonitorCapabilities(physicalMonitor.hPhysicalMonitor, out dwMonitorCapabilities, out dwSupportedColorTemperatures))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return dwMonitorCapabilities;
}
public static bool GetBrightnessSupport(PHYSICAL_MONITOR physicalMonitor)
{
return (GetMonitorCapabilities(physicalMonitor) & MC_CAPS_BRIGHTNESS) != 0;
}
public static bool GetContrastSupport(PHYSICAL_MONITOR physicalMonitor)
{
return (GetMonitorCapabilities(physicalMonitor) & MC_CAPS_CONTRAST) != 0;
}
public static double GetMonitorBrightness(PHYSICAL_MONITOR physicalMonitor)
{
uint dwMinimumBrightness, dwCurrentBrightness, dwMaximumBrightness;
if (!GetMonitorBrightness(physicalMonitor.hPhysicalMonitor, out dwMinimumBrightness, out dwCurrentBrightness, out dwMaximumBrightness))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return (double)(dwCurrentBrightness - dwMinimumBrightness) / (double)(dwMaximumBrightness - dwMinimumBrightness);
}
public static void SetMonitorBrightness(PHYSICAL_MONITOR physicalMonitor, double brightness)
{
uint dwMinimumBrightness, dwCurrentBrightness, dwMaximumBrightness;
if (!GetMonitorBrightness(physicalMonitor.hPhysicalMonitor, out dwMinimumBrightness, out dwCurrentBrightness, out dwMaximumBrightness))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
if (!SetMonitorBrightness(physicalMonitor.hPhysicalMonitor, (uint)(dwMinimumBrightness + (dwMaximumBrightness - dwMinimumBrightness) * brightness)))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
public static double GetMonitorContrast(PHYSICAL_MONITOR physicalMonitor)
{
uint dwMinimumContrast, dwCurrentContrast, dwMaximumContrast;
if (!GetMonitorContrast(physicalMonitor.hPhysicalMonitor, out dwMinimumContrast, out dwCurrentContrast, out dwMaximumContrast))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return (double)(dwCurrentContrast - dwMinimumContrast) / (double)(dwMaximumContrast - dwMinimumContrast);
}
public static void SetMonitorContrast(PHYSICAL_MONITOR physicalMonitor, double contrast)
{
uint dwMinimumContrast, dwCurrentContrast, dwMaximumContrast;
if (!GetMonitorContrast(physicalMonitor.hPhysicalMonitor, out dwMinimumContrast, out dwCurrentContrast, out dwMaximumContrast))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
if (!SetMonitorContrast(physicalMonitor.hPhysicalMonitor, (uint)(dwMinimumContrast + (dwMaximumContrast - dwMinimumContrast) * contrast)))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
}
}