-
Notifications
You must be signed in to change notification settings - Fork 0
/
DLD.frmMain.pas
109 lines (95 loc) · 2.78 KB
/
DLD.frmMain.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
unit DLD.frmMain;
interface
uses
System.SysUtils,
System.Types,
System.UITypes,
System.Classes,
System.Variants,
FMX.Types,
FMX.Controls,
FMX.Forms,
FMX.Graphics,
FMX.Dialogs,
FMX.Controls.Presentation,
FMX.StdCtrls,
Winapi.Messages;
type
TfrmDisableLaptopDisplay = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
procedure FormDestroy(Sender: TObject);
private
FSWbemLocator: OLEVariant;
FWMIService: OLEVariant;
FPreviousWbemObject: OLEVariant;
FPreviousBrightness: Byte;
private
procedure MoveToSecondaryDisplay;
public
procedure ChangeBrightness;
procedure ReturnBrightness;
end;
var
frmDisableLaptopDisplay: TfrmDisableLaptopDisplay;
implementation
uses
Winapi.ActiveX,
Win.ComObj;
{$R *.fmx}
procedure TfrmDisableLaptopDisplay.FormCreate(Sender: TObject);
begin
MoveToSecondaryDisplay;
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\WMI', '', '');
ChangeBrightness;
end;
procedure TfrmDisableLaptopDisplay.FormDestroy(Sender: TObject);
begin
ReturnBrightness;
end;
procedure TfrmDisableLaptopDisplay.FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
begin
if Key = vkEscape then
Application.Terminate;
end;
procedure TfrmDisableLaptopDisplay.MoveToSecondaryDisplay;
begin
Left := Screen.Displays[0].PhysicalWorkarea.Left;
Top := Screen.Displays[0].PhysicalWorkarea.Top;
end;
procedure TfrmDisableLaptopDisplay.ChangeBrightness;
var
LWbemObjectSet: OLEVariant;
LEnum: IEnumvariant;
LWbemObject: OLEVariant;
LValue: LongWord;
begin
ReturnBrightness;
{ find and store current brightness }
LWbemObjectSet := FWMIService.ExecQuery('SELECT * FROM WmiMonitorBrightness Where Active=True', 'WQL', $00000020);
LEnum := IUnknown(LWbemObjectSet._NewEnum) as IEnumvariant;
while LEnum.Next(1, LWbemObject, LValue) = 0 do
begin
FPreviousBrightness := Byte(LWbemObject.CurrentBrightness);
LWbemObject := Unassigned;
end;
{ update current brightness to 0 }
LWbemObjectSet := FWMIService.ExecQuery('SELECT * FROM WmiMonitorBrightnessMethods Where Active=True', 'WQL', $00000020);
LEnum := IUnknown(LWbemObjectSet._NewEnum) as IEnumvariant;
while LEnum.Next(1, LWbemObject, LValue) = 0 do
begin
LWbemObject.WmiSetBrightness(0, 0);
FPreviousWbemObject := LWbemObject;
LWbemObject := Unassigned;
end;
end;
procedure TfrmDisableLaptopDisplay.ReturnBrightness;
begin
if VarCompareValue(FPreviousWbemObject, Unassigned) <> TVariantRelationship.vrEqual then
begin
FPreviousWbemObject.WmiSetBrightness(0, FPreviousBrightness);
FPreviousWbemObject := Unassigned;
end;
end;
end.