-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmonitorHandler.pas
398 lines (342 loc) · 12.9 KB
/
monitorHandler.pas
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
unit monitorHandler;
interface
uses
Messages, Classes, Windows, NVAPI, Forms, Dialogs;
const
DXVA2 = 'Dxva2.dll';
type
/// NVidia DVC Info
NV_DISPLAY_DVC_INFO = record
version: UINT;
currentLevel: Integer;
minLevel: Integer;
maxLevel: Integer;
end;
/// NVidia Driver info
NV_DISPLAY_DRIVER_VERSION = record
version: Integer;
drvVersion: Integer;
bldChangeListNum: Integer;
szBuildBranchString: array[0..64] of char;
szAdapterString: array[0..64] of char;
end;
/// Describes a monitor's color temperature.
PMC_COLOR_TEMPERATURE = ^TMC_COLOR_TEMPERATURE;
TMC_COLOR_TEMPERATURE = (
MC_COLOR_TEMPERATURE_UNKNOWN, // Unknown temperature.
MC_COLOR_TEMPERATURE_4000K, // 4,000 kelvins (K).
MC_COLOR_TEMPERATURE_5000K, // 5,000 K.
MC_COLOR_TEMPERATURE_6500K, // 6,500 K.
MC_COLOR_TEMPERATURE_7500K, // 7.500 K.
MC_COLOR_TEMPERATURE_8200K, // 8,200 K.
MC_COLOR_TEMPERATURE_9300K, // 9,300 K.
MC_COLOR_TEMPERATURE_10000K, // 10,000 K.
MC_COLOR_TEMPERATURE_11500K // 11,500 K.
);
MC_COLOR_TEMPERATURE = TMC_COLOR_TEMPERATURE;
/// Specifies whether to get or set the vertical or horizontal position of a
/// monitor's display area.
PMC_POSITION_TYPE = ^TMC_POSITION_TYPE;
TMC_POSITION_TYPE = (
MC_HORIZONTAL_POSITION, // Horizontal position.
MC_VERTICAL_POSITION // Vertical position.
);
MC_POSITION_TYPE = TMC_POSITION_TYPE;
/// Specifies whether to get or set the width or height of a monitor's
/// display area.
PMC_SIZE_TYPE = ^TMC_SIZE_TYPE;
TMC_SIZE_TYPE = (
MC_WIDTH,
MC_HEIGHT
);
MC_SIZE_TYPE = TMC_SIZE_TYPE;
/// Identifies monitor display technologies.
PMC_DISPLAY_TECHNOLOGY_TYPE = ^TMC_DISPLAY_TECHNOLOGY_TYPE;
TMC_DISPLAY_TECHNOLOGY_TYPE = (
MC_SHADOW_MASK_CATHODE_RAY_TUBE, // Shadow-mask cathode ray tube (CRT).
MC_APERTURE_GRILL_CATHODE_RAY_TUBE, // Aperture-grill CRT.
MC_THIN_FILM_TRANSISTOR, // Thin-film transistor (TFT) display.
MC_LIQUID_CRYSTAL_ON_SILICON, // Liquid crystal on silicon (LCOS) display.
MC_PLASMA, // Plasma display.
MC_ORGANIC_LIGHT_EMITTING_DIODE, // Organic light emitting diode (LED) display.
MC_ELECTROLUMINESCENT, // Electroluminescent display.
MC_MICROELECTROMECHANICAL, // Microelectromechanical display.
MC_FIELD_EMISSION_DEVICE // Field emission device (FED) display.
);
MC_DISPLAY_TECHNOLOGY_TYPE = TMC_DISPLAY_TECHNOLOGY_TYPE;
/// Specifies whether to set or get a monitor's red, green, or blue drive.
PMC_DRIVE_TYPE = ^TMC_DRIVE_TYPE;
TMC_DRIVE_TYPE = (
MC_RED_DRIVE, // Red drive.
MC_GREEN_DRIVE, // Green drive.
MC_BLUE_DRIVE // Blue drive.
);
MC_DRIVE_TYPE = TMC_DRIVE_TYPE;
/// Specifies whether to get or set a monitor's red, green, or blue gain.
PMC_GAIN_TYPE = ^TMC_GAIN_TYPE;
TMC_GAIN_TYPE = (
MC_REG_GAIN, // Red gain.
MC_GREEN_GAIN, // Green gain.
MC_BLUE_GAIN // Blue gain.
);
MC_GAIN_TYPE = TMC_GAIN_TYPE;
/// Describes a Virtual Control Panel (VCP) code type.
PMC_VCP_CODE_TYPE = ^TMC_VCP_CODE_TYPE;
TMC_VCP_CODE_TYPE = (
MC_MOMENTARY, /// Momentary VCP code. Sending a command of this type
/// causes the monitor to initiate a self-timed operation
/// and then revert to its original state. Examples
/// include display tests and degaussing.
MC_SET_PARAMETER /// Set parameter VCP code. Sending a command of this
/// type changes some aspect of the monitor's operation.
);
MC_VCP_CODE_TYPE = TMC_VCP_CODE_TYPE;
function SetMonitorContrast (
hMonitor: THandle; dwNewBrightness: DWORD): BOOL; stdcall;
external 'Dxva2.dll' name 'SetMonitorContrast';
function GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor: THandle;
pdwNumberOfPhysicalMonitors: PDWORD): Boolean; stdcall;
external 'Dxva2.dll' name 'GetNumberOfPhysicalMonitorsFromHMONITOR';
function GetPhysicalMonitorsFromHMONITOR(hMonitor: THandle;
dwPhysicalMonitorArraySizwe: DWORD; pPhysicalMonitorArray: Pointer): Boolean; stdcall;
external 'Dxva2.dll' name 'GetPhysicalMonitorsFromHMONITOR';
function DestroyPhysicalMonitors(dwPhysicalMonitorArraySize: DWORD;
pPhysicalMonitorArray: Pointer): Boolean; stdcall;
external 'Dxva2.dll' name 'DestroyPhysicalMonitors';
// highlevelmonitorconfigurationapi.h
/// DegaussMonitor: Degausses a monitor. Degaussing improves a monitor's image quality and color
/// fidelity by demagnitizing the monitor.
/// Result: TRUE if succeds, FALSE if fails, more info GetLastError.
/// GetMonitorCapabilities flag dependency: MC_CAPS_DEGAUSS
/// Response time: 50ms.
/// Remark: Should not be called frequently because it won't improve quality more than expected.
function DegaussMonitor(AMonitor: THandle): BOOL; stdcall;
external DXVA2 name 'DegaussMonitor';
/// GetMonitorBrightness:
/// Retrieves a monitor's minimum, maximum, and current brightness settings.
/// Result: TRUE if succeds, FALSE if fails, more info GetLastError.
/// GetMonitorCapabilities flag dependency: MC_CAPS_BRIGHTNESS
/// Response time: 40ms.
function GetMonitorBrightness (
hMonitor: THandle; pdwMinimumBrightness : PDWORD;
pdwCurrentBrightness : PDWORD; pdwMaximumBrightness : PDWORD): BOOL; stdcall;
external 'Dxva2.dll' name 'GetMonitorBrightness';
/// GetMonitorCapabilities:
/// Retrieves the configuration capabilities of a monitor. Call this function
/// to find out which high-level monitor configuration functions are supported
/// by the monitor.
/// Result: TRUE if succeds, FALSE if fails, more info GetLastError.
///
function GetMonitorCapabilities(
hMonitor: THandle; pdwMonitorCapabilities : PDWORD;
pdwSupportedColorTemperatures : PDWORD
): BOOL; stdcall;
external DXVA2 name 'GetMonitorCapabilities';
/// GetMonitorColorTemperature:
/// Retrieves a monitor's current color temperature.
function GetMonitorColorTemperature(
hMonitor: THandle;
pctCurrentColorTemperature: PMC_COLOR_TEMPERATURE
): BOOL; stdcall;
external DXVA2 name 'GetMonitorColorTemperature';
/// GetMonitorContrast:
/// Retrieves a monitor's minimum, maximum, and current contrast settings.
function GetMonitorContrast (
hMonitor: THandle; pdwMinimumBrightness : PDWORD;
pdwCurrentBrightness : PDWORD; pdwMaximumBrightness : PDWORD): BOOL; stdcall;
external DXVA2 name 'GetMonitorContrast';
/// GetMonitorDisplayAreaPosition:
/// Retrieves a monitor's minimum, maximum, and current horizontal or
/// vertical position.
/// Result: TRUE if succeds, FALSE if fails, more info GetLastError.
function GetMonitorDisplayAreaPosition (
hMonitor: THandle; ptPositionType: MC_POSITION_TYPE;
pdwMinimumPosition: PDWORD; pdwCurrentPosition: PDWORD;
pdwMaximumPosition: PDWORD
): BOOL; stdcall;
external DXVA2 name 'GetMonitorDisplayAreaPosition';
/// GetMonitorDisplayAreaSize:
/// Retrieves a monitor's minimum, maximum, and current width or height.
/// Result: TRUE if succeds, FALSE if fails, more info GetLastError.
function GetMonitorDisplayAreaSize (
hMonitor: THandle; stSizeType: MC_SIZE_TYPE;
pdwMinimumWidthOrHeight: PDWORD; pdwCurrentWidthOrHeight: PDWORD;
pdwMaximumWidthOrHeight: PDWORD
): BOOL; stdcall;
external DXVA2 name 'GetMonitorDisplayAreaSize';
/// GetMonitorRedGreenOrBlueDrive:
/// Retrieves a monitor's red, green, or blue drive value.
/// Result: TRUE if succeds, FALSE if fails, more info GetLastError.
function GetMonitorRedGreenOrBlueDrive (
hMonitor: THandle; dtDriveType: MC_DRIVE_TYPE;
pdwMinimumDrive: PDWORD; pdwCurrentDrive: PDWORD;
pdwMaximumDrive: PDWORD
): BOOL; stdcall;
external DXVA2 name 'GetMonitorRedGreenOrBlueDrive';
/// GetMonitorRedGreenOrBlueGain:
/// Retrieves a monitor's red, green, or blue gain value.
/// Result: TRUE if succeds, FALSE if fails, more info GetLastError.
function GetMonitorRedGreenOrBlueGain (
hMonitor: THandle; gtGainType: MC_GAIN_TYPE;
pdwMinimumGain: PDWORD; pdwCurrentGain: PDWORD;
pdwMaximumGain: PDWORD
): BOOL; stdcall;
external DXVA2 name 'GetMonitorRedGreenOrBlueGain';
/// GetMonitorTechnologyType:
/// Retrieves the type of technology used by a monitor.
/// Result: TRUE if succeds, FALSE if fails, more info GetLastError.
/// Remarks: GetMonitorCapabilites MC_CAPS_DISPLAY_TECHNOLOGY_TYPE flag
/// if absent function will fail. CRT monitors would fail surely.
function GetMonitorTechnologyType (
hMonitor: THandle; pdtyDisplayTechnologyType: PMC_DISPLAY_TECHNOLOGY_TYPE
): BOOL; stdcall;
external DXVA2 name 'GetMonitorTechnologyType';
/// RestoreMonitorFactoryColorDefaults:
/// Restores a monitor's color settings to their factory defaults.
/// Result: TRUE if succeds, FALSE if fails, more info GetLastError.
/// Dependency flag: GetMonitorCapabilities MC_CAPS_RESTORE_FACTORY_COLOR_DEFAULTS
/// Response time: 5 seconds to return, it is slow.
function RestoreMonitorFactoryColorDefaults (
hMonitor: THandle
): BOOL; stdcall;
external DXVA2 name 'RestoreMonitorFactoryColorDefaults';
/// RestoreMonitorFactoryDefaults:
/// Restores a monitor's settings to their factory defaults.
/// Result: TRUE if succeds, FALSE if fails, more info GetLastError.
/// Dependency flag: GetMonitorCapabilities MC_CAPS_RESTORE_FACTORY_DEFAULTS
/// Response time: 5 seconds to return, it is slow.
function RestoreMonitorFactoryDefaults (
hMonitor: THandle
): BOOL; stdcall;
external DXVA2 name 'RestoreMonitorFactoryDefaults';
/// SaveCurrentMonitorSettings:
/// Saves the current monitor settings to the display's nonvolatile storage.
/// Result: TRUE if succeds, FALSE if fails, more info GetLastError.
/// Response time: About 200 ms to return.
function SaveCurrentMonitorSettings (
hMonitor: THandle
): BOOL; stdcall;
external DXVA2 name 'SaveCurrentMonitorSettings';
/// SetMonitorBrightness:
/// Sets a monitor's brightness value.
/// Result: TRUE if succeds, FALSE if fails, more info GetLastError.
/// Dependency flag: GetMonitorCapabilities MC_CAPS_BRIGHTNESS
/// Response Time: 50ms
function SetMonitorBrightness (
hMonitor: THandle; dwNewBrightness: DWORD): BOOL; stdcall;
external 'Dxva2.dll' name 'SetMonitorBrightness';
type
PHYSICAL_MONITOR = record
hPhysicalMonitor: THandle;
szPhysicalMonitorDescription: array[0..127] of Char;
end;
// TMonitorSetting = record
// Id: THandle;
// Name: array [0..127] of Char;
// Contrast: Integer;
// Brightness: Integer;
// end;
TMonitorSetting = class
private
FName: string;
FVendor: string;
FCapabilities: DWORD;
FSupportedColorTemperatures: DWORD;
public
property Name: string read FName write FName;
property Vendor: string read FVendor write FVendor;
property Capabilities: DWORD read FCapabilities write FCapabilities;
property SupportedColorTemperatures: DWORD read FSupportedColorTemperatures write FSupportedColorTemperatures;
end;
TMonitorSettings = class(TObject)
private
FMsgHandlerHWND: HWND;
FItems: TArray<TMonitorSetting>;
procedure WndMethod(var AMsg: TMessage);
public
constructor Create;
destructor Destroy; override;
property items: TArray<TMonitorSetting> read FItems write FItems;
procedure Append(item: TMonitorSetting);
procedure Remove(index: Integer);
procedure Clear;
function Count: Integer;
procedure UpdateMonitors;
end;
implementation
{ TMonitorSettings }
procedure TMonitorSettings.Append(item: TMonitorSetting);
begin
SetLength(FItems, Length(FItems) + 1);
FItems[High(FItems)] := item;
end;
procedure TMonitorSettings.Clear;
var
LItems: TMonitorSetting;
begin
for LItems in FItems do
LItems.Free;
end;
function TMonitorSettings.Count: Integer;
begin
Result := Length(FItems);
end;
constructor TMonitorSettings.Create;
begin
inherited Create;
UpdateMonitors;
FMsgHandlerHWND := AllocateHWnd(WndMethod);
end;
destructor TMonitorSettings.Destroy;
var
LItems: TMonitorSetting;
begin
for LItems in FItems do
LItems.Free;
DeallocateHWnd(FMsgHandlerHWND);
inherited;
end;
procedure TMonitorSettings.Remove(index: Integer);
var
I: Integer;
begin
if (Count > 0) and (index < Count) then
begin
FItems[index].Free;
for I := index + 1 to Count - 1 do
FItems[I - 1] := FItems[I];
SetLength(FItems, Count - 1);
end;
end;
// Updates monitor status
procedure TMonitorSettings.UpdateMonitors;
var
LCount: Integer;
I: Integer;
LItem: TMonitorSetting;
LCapabilities, LSupportedColorTemperatures: DWORD;
begin
LCount := Screen.MonitorCount;
Clear;
for I := 0 to LCount - 1 do
begin
LItem := TMonitorSetting.Create;
try
GetMonitorCapabilities(Screen.Monitors[I].Handle, @LCapabilities, @LSupportedColorTemperatures);
Append(LItem);
finally
// LItem.Free;
end;
end;
end;
procedure TMonitorSettings.WndMethod(var AMsg: TMessage);
begin
if AMsg.msg = WM_DISPLAYCHANGE then
begin
// UpdateMonitors;
// OutputDebugStringA(PAnsiChar('Hey'));
end
else
AMsg.Result := DefWindowProc(FMsgHandlerHWND, AMsg.Msg, AMsg.WParam, AMsg.LParam);
end;
end.