-
Notifications
You must be signed in to change notification settings - Fork 7
/
BrightnessControl.cs
81 lines (70 loc) · 3.46 KB
/
BrightnessControl.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Interop;
using System.Diagnostics;
namespace DDCMonitorManager
{
class BrightnessControl
{
private IntPtr hWnd;
private NativeStructures.PHYSICAL_MONITOR[] pPhysicalMonitorArray;
private uint pdwNumberOfPhysicalMonitors;
public BrightnessControl(IntPtr hWnd)
{
this.hWnd = hWnd;
SetupMonitors();
}
private void SetupMonitors()
{
IntPtr hMonitor = NativeCalls.MonitorFromWindow(hWnd, NativeConstants.MONITOR_DEFAULTTOPRIMARY);
int lastWin32Error = Marshal.GetLastWin32Error();
bool numberOfPhysicalMonitorsFromHmonitor = NativeCalls.GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, ref pdwNumberOfPhysicalMonitors);
lastWin32Error = Marshal.GetLastWin32Error();
pPhysicalMonitorArray = new NativeStructures.PHYSICAL_MONITOR[pdwNumberOfPhysicalMonitors];
bool physicalMonitorsFromHmonitor = NativeCalls.GetPhysicalMonitorsFromHMONITOR(hMonitor, pdwNumberOfPhysicalMonitors, pPhysicalMonitorArray);
lastWin32Error = Marshal.GetLastWin32Error();
}
private void GetMonitorCapabilities(int monitorNumber)
{
uint pdwMonitorCapabilities = 0u;
uint pdwSupportedColorTemperatures = 0u;
var monitorCapabilities = NativeCalls.GetMonitorCapabilities(pPhysicalMonitorArray[monitorNumber].hPhysicalMonitor, ref pdwMonitorCapabilities, ref pdwSupportedColorTemperatures);
Debug.WriteLine(pdwMonitorCapabilities);
Debug.WriteLine(pdwSupportedColorTemperatures);
int lastWin32Error = Marshal.GetLastWin32Error();
NativeStructures.MC_DISPLAY_TECHNOLOGY_TYPE type = NativeStructures.MC_DISPLAY_TECHNOLOGY_TYPE.MC_SHADOW_MASK_CATHODE_RAY_TUBE;
var monitorTechnologyType = NativeCalls.GetMonitorTechnologyType(pPhysicalMonitorArray[monitorNumber].hPhysicalMonitor, ref type);
Debug.WriteLine(type);
lastWin32Error = Marshal.GetLastWin32Error();
}
public bool SetBrightness(short brightness,int monitorNumber)
{
var brightnessWasSet = NativeCalls.SetMonitorBrightness(pPhysicalMonitorArray[monitorNumber].hPhysicalMonitor, (short)brightness);
if (brightnessWasSet)
Debug.WriteLine("Brightness set to " (short)brightness);
int lastWin32Error = Marshal.GetLastWin32Error();
return brightnessWasSet;
}
public BrightnessInfo GetBrightnessCapabilities(int monitorNumber)
{
short current = -1, minimum = -1, maximum = -1;
bool getBrightness = NativeCalls.GetMonitorBrightness(pPhysicalMonitorArray[monitorNumber].hPhysicalMonitor,ref minimum,ref current,ref maximum);
int lastWin32Error = Marshal.GetLastWin32Error();
return new BrightnessInfo { minimum = minimum, maximum = maximum, current = current};
}
public void DestroyMonitors()
{
var destroyPhysicalMonitors = NativeCalls.DestroyPhysicalMonitors(pdwNumberOfPhysicalMonitors, pPhysicalMonitorArray);
int lastWin32Error = Marshal.GetLastWin32Error();
}
public uint GetMonitors()
{
return pdwNumberOfPhysicalMonitors;
}
}
}