-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathufunctions.pas
More file actions
205 lines (178 loc) · 5.58 KB
/
ufunctions.pas
File metadata and controls
205 lines (178 loc) · 5.58 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
unit ufunctions;
{$mode delphi}{$H+}
interface
uses
Windows, Classes, IniFiles;
const
GLOBAL_SECTION = 'Global';
RUNONSTARTUP_VALUE = 'RunOnStartup';
LOOPVIDEOS_VALUE = 'LoopVideos';
function GetScreenshotFileName(const VidFile: string): string;
procedure SplitString(const Delimiter: char; const Str: string;
const ListOfStrings: TStrings);
function GetMonitorName(const Hnd: HMONITOR): string;
function GetCurrentUser(): string;
function InitializeConfig(): boolean;
function RECTToString(const R: TRect): string;
function IsWin7(): boolean;
function IsAeroEnabled(): boolean;
var
Config: TIniFile;
implementation
uses
SysUtils, Forms, Multimon, ActiveX, ComObj, Variants, Character;
function TrimLeadingZeros(const S: string): string;
var
I, L: integer;
begin
L := Length(S);
I := 1;
while (I < L) and (S[I] = '0') do
Inc(I);
Result := Copy(S, I);
end;
function GetScreenshotFileName(const VidFile: string): string;
begin
Result := IncludeTrailingBackslash(ExtractFilePath(Application.ExeName)) +
ChangeFileExt(ExtractFileName(VidFile), '.png');
end;
function GetMonitorName(const Hnd: HMONITOR): string;
const
WbemUser = '';
WbemPassword = '';
WbemComputer = 'localhost';
wbemFlagForwardOnly = $00000020;
type
TMonitorInfoEx = record
cbSize: DWORD;
rcMonitor: TRect;
rcWork: TRect;
dwFlags: DWORD;
szDevice: array[0..CCHDEVICENAME - 1] of AnsiChar;
end;
var
DispDev: TDisplayDevice;
monInfo: TMonitorInfoEx;
SL: TStringList;
FSWbemLocator: olevariant;
FWMIService, FWMIService2: olevariant;
FWbemObjectSet: olevariant;
FWbemObject: olevariant;
oEnum: IEnumvariant;
Query: WideString;
DeviceId: string;
iValue: longword;
I: integer;
begin
Result := '';
monInfo.cbSize := sizeof(monInfo);
SL := TStringList.Create();
try
if GetMonitorInfo(Hnd, @monInfo) then
begin
DispDev.cb := SizeOf(DispDev);
EnumDisplayDevices(@monInfo.szDevice, 0, @DispDev, 0);
Result := StrPas(DispDev.DeviceString);
SplitString('\', DispDev.DeviceID, SL);
if ((SL.Count = 4) and (SL[0] = 'MONITOR')) then
begin
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer(WbemComputer,
'root\CIMV2', WbemUser, WbemPassword);
FWMIService2 := FSWbemLocator.ConnectServer(WbemComputer,
'root\WMI', WbemUser, WbemPassword);
Query := 'SELECT * FROM Win32_PnPEntity WHERE ClassGUID = ''' +
SL[2] + ''' AND DeviceID LIKE ''DISPLAY\\' + SL[1] + '\\' +
TrimLeadingZeros(SL[3]) + '%''';
FWbemObjectSet := FWMIService.ExecQuery(Query, 'WQL', wbemFlagForwardOnly);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
if (oEnum.Next(1, FWbemObject, iValue) = 0) then
begin
DeviceId := StringReplace(VarToUnicodeStr(FWbemObject.DeviceId),
'\', '\\', [rfReplaceAll]);
FWbemObject := Unassigned;
Query :=
'SELECT UserFriendlyName FROM WmiMonitorId WHERE Active = True AND InstanceName LIKE "'
+ DeviceId + '%"';
FWbemObjectSet := FWMIService2.ExecQuery(Query, 'WQL', wbemFlagForwardOnly);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
if (oEnum.Next(1, FWbemObject, iValue) = 0) then
begin
Result := '';
for I := VarArrayLowBound(FWbemObject.UserFriendlyName, 1)
to VarArrayHighBound(FWbemObject.UserFriendlyName, 1) do
Result := Result + TCharacter.ConvertFromUtf32(
UCS4Char(integer(FWbemObject.UserFriendlyName[I])));
Result := Trim(Result);
end;
end;
end;
end;
finally
SL.Free();
end;
end;
procedure SplitString(const Delimiter: char; const Str: string;
const ListOfStrings: TStrings);
begin
ListOfStrings.Clear();
ListOfStrings.Delimiter := Delimiter;
ListOfStrings.StrictDelimiter := True;
ListOfStrings.DelimitedText := Str;
end;
function GetCurrentUser(): string;
var
nSize: DWord;
begin
nSize := 1024;
SetLength(Result, nSize);
if GetUserName(PChar(Result), nSize) then
SetLength(Result, nSize - 1)
else
Result := '';
end;
function InitializeConfig(): boolean;
begin
Config := TIniFile.Create(IncludeTrailingBackslash(
ExtractFilePath(Application.ExeName)) + 'config.ini', True);
// Populate the configuration file with defualt values.
Result := Config.SectionExists(GLOBAL_SECTION);
if (not Result) then
begin
Config.WriteBool(GLOBAL_SECTION, RUNONSTARTUP_VALUE, True);
Config.WriteBool(GLOBAL_SECTION, LOOPVIDEOS_VALUE, True);
end;
end;
function RECTToString(const R: TRect): string;
begin
Result := Format('(%d, %d) (%d, %d)', [R.Left, R.Top, R.Right, R.Bottom]);
end;
function IsWin7(): boolean;
begin
Result := ((Win32MajorVersion = 6) and (Win32MinorVersion = 1));
end;
function IsAeroEnabled: boolean;
type
TDwmIsCompositionEnabledFunc = function(out pfEnabled: BOOL): HRESULT; stdcall;
var
IsEnabled: BOOL;
ModuleHandle: HMODULE;
DwmIsCompositionEnabledFunc: TDwmIsCompositionEnabledFunc;
begin
Result := False;
if Win32MajorVersion >= 6 then
begin
ModuleHandle := LoadLibrary('dwmapi.dll');
if ModuleHandle <> 0 then
try
@DwmIsCompositionEnabledFunc := GetProcAddress(ModuleHandle, 'DwmIsCompositionEnabled');
if Assigned(DwmIsCompositionEnabledFunc) then
if DwmIsCompositionEnabledFunc(IsEnabled) = S_OK then
Result := IsEnabled;
finally
if (ModuleHandle <> 0) then
FreeLibrary(ModuleHandle);
end;
end;
end;
end.