Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
183 lines (158 loc) · 5.44 KB
/
Copy pathProgram.cs
File metadata and controls
183 lines (158 loc) · 5.44 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
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
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace BrightnessButtons;
internal static class Program
{
[STAThread]
private static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new BrightnessForm());
}
}
internal sealed class BrightnessForm : Form
{
private const int Step = 5;
private const int Minimum = 50;
private readonly GammaController controller = new();
private readonly Label valueLabel = new();
private int level = 100;
public BrightnessForm()
{
Text = "软件亮度";
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = true;
StartPosition = FormStartPosition.CenterScreen;
ClientSize = new Size(368, 214);
Font = new Font("Microsoft YaHei UI", 10F);
var title = new Label
{
Text = "外接显示器软件亮度",
AutoSize = false,
TextAlign = ContentAlignment.MiddleCenter,
Font = new Font("Microsoft YaHei UI", 14F, FontStyle.Bold),
Bounds = new Rectangle(20, 20, 328, 34),
};
valueLabel.AutoSize = false;
valueLabel.TextAlign = ContentAlignment.MiddleCenter;
valueLabel.Font = new Font("Segoe UI", 28F, FontStyle.Bold);
valueLabel.Bounds = new Rectangle(20, 58, 328, 56);
var downButton = MakeButton("− 调低亮度", new Rectangle(25, 130, 150, 54));
downButton.Click += (_, _) => SetLevel(level - Step);
var upButton = MakeButton("+ 调高亮度", new Rectangle(193, 130, 150, 54));
upButton.Click += (_, _) => SetLevel(level + Step);
Controls.AddRange([title, valueLabel, downButton, upButton]);
try
{
controller.Apply(1d);
UpdateLabel();
}
catch (Exception ex)
{
MessageBox.Show($"无法读取显示器的色彩映射。\n\n{ex.Message}", Text,
MessageBoxButtons.OK, MessageBoxIcon.Error);
Close();
}
}
private Button MakeButton(string text, Rectangle bounds) => new()
{
Text = text,
Bounds = bounds,
Font = new Font("Microsoft YaHei UI", 11F, FontStyle.Bold),
UseVisualStyleBackColor = true,
};
private void SetLevel(int requestedLevel)
{
var newLevel = Math.Clamp(requestedLevel, Minimum, 100);
if (newLevel == level)
return;
try
{
controller.Apply(newLevel / 100d);
level = newLevel;
UpdateLabel();
}
catch (Exception ex)
{
MessageBox.Show($"未能应用亮度。\n\n{ex.Message}", Text,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void UpdateLabel() => valueLabel.Text = $"{level}%";
}
internal sealed class GammaController
{
public void Apply(double factor)
{
var ramp = GammaRamp.Create(factor);
var screens = Screen.AllScreens;
if (screens.Length == 0)
throw new InvalidOperationException("未找到活动显示器。");
foreach (var screen in screens)
{
using var dc = DisplayDc.Open(screen.DeviceName);
if (!NativeMethods.ApplyDeviceGammaRamp(dc.Handle, ramp))
throw new InvalidOperationException($"无法应用 {screen.DeviceName} 的亮度设置。");
}
}
}
internal static class GammaRamp
{
public const int Entries = 256;
public static ushort[] Create(double factor)
{
var result = new ushort[Entries * 3];
for (var valueIndex = 0; valueIndex < Entries; valueIndex++)
{
var value = (ushort)Math.Clamp(Math.Round(valueIndex * 257d * factor), 0, ushort.MaxValue);
for (var channel = 0; channel < 3; channel++)
result[channel * Entries + valueIndex] = value;
}
return result;
}
}
internal sealed class DisplayDc : IDisposable
{
private DisplayDc(IntPtr handle) => Handle = handle;
public IntPtr Handle { get; }
public static DisplayDc Open(string deviceName)
{
var handle = NativeMethods.CreateDC("DISPLAY", deviceName, null, IntPtr.Zero);
if (handle == IntPtr.Zero)
throw new InvalidOperationException($"无法打开显示器 {deviceName}。");
return new DisplayDc(handle);
}
public void Dispose()
{
if (Handle != IntPtr.Zero)
NativeMethods.DeleteDC(Handle);
}
}
internal static class NativeMethods
{
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr CreateDC(string driver, string device, string? output, IntPtr initData);
[DllImport("gdi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteDC(IntPtr hdc);
[DllImport("gdi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetDeviceGammaRamp(IntPtr hdc, IntPtr ramp);
public static bool ApplyDeviceGammaRamp(IntPtr hdc, ushort[] ramp)
{
var bytes = ramp.Length * sizeof(ushort);
var buffer = Marshal.AllocHGlobal(bytes);
try
{
var rawBytes = new byte[bytes];
Buffer.BlockCopy(ramp, 0, rawBytes, 0, bytes);
Marshal.Copy(rawBytes, 0, buffer, bytes);
return SetDeviceGammaRamp(hdc, buffer);
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
}