-
Notifications
You must be signed in to change notification settings - Fork 4
/
unitlib.pas
301 lines (242 loc) · 7.3 KB
/
unitlib.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
unit UnitLib;
interface
uses
Windows, Winapi.ShellAPI, ShFolder,
FMX.Forms, FMX.Types, FMX.StdCtrls, FMX.Graphics, FMX.Platform.Win,
System.SysUtils, System.Classes, System.UITypes, System.IOUtils;
// Paths
function ApplicationPath: string;
function ConfigFileName: string;
// Text functions
function LeftChar(s: string): string;
function LeftStr(s: string): string;
function RightStr(s: string): string;
// Language functions
function GetDefaultLanguage: string;
function GetDefaultList: string;
// Windows system functions
function KeyShiftDown: boolean;
procedure HideAppOnTaskbar(Form: TForm);
function GetDeskWallpaper: WideString;
procedure OpenDesktopControl;
// Fonts
procedure GetFonts(List: TStringList);
// Firemonkey functions
procedure OpenUrl(s: string);
procedure WordWrap(L: TLabel; Max: Single);
// Mutex
function CreateMutex(name: PWideChar): boolean;
procedure CloseMutex;
const
ApplicationName = 'Bible Verse Desktop';
const
Slash = '\';
const
CRLF = #10; // #13 + #10
const
mbRight = TMouseButton.mbRight;
mbLeft = TMouseButton.mbLeft;
mbMiddle = TMouseButton.mbMiddle;
fsBold = TFontStyle.fsBold;
fsItalic = TFontStyle.fsItalic;
fsUnderline = TFontStyle.fsUnderline;
implementation
var
Mutex : THandle;
//-----------------------------------------------------------------------------
// Paths
//-----------------------------------------------------------------------------
function AppDataPath: string; // Roaming
begin
Result := TPath.GetHomePath + Slash + ApplicationName;
if not DirectoryExists(Result) then ForceDirectories(Result);
end;
function ApplicationPath: string;
begin
Result := ExtractFilePath(ParamStr(0));
if Result.Contains('Win32\Debug') then Result := Result.Replace('\Win32\Debug','');
if Result.Contains('Win64\Debug') then Result := Result.Replace('\Win64\Debug','');
end;
function ConfigFileName: string;
begin
Result := AppDataPath + Slash + 'config.cfg';
end;
//-----------------------------------------------------------------------------
// Text functions
//-----------------------------------------------------------------------------
function LeftChar(s: string): string;
begin
Result := '';
if Length(s) < 1 then Exit;
Result := Copy(s,1,1);
end;
function LeftStr(s: string): string;
var
n: integer;
begin
Result := s;
n := Pos(chr(09),s);
if n > 0 then Result := Trim(Copy(s,1,n-1));
end;
function RightStr(s: string): string;
var
n: integer;
begin
Result := '';
n := Pos(chr(09),s);
if n > 0 then Result := Trim(Copy(s,n+1,Length(s)));
end;
//-----------------------------------------------------------------------------
// Language functions
//-----------------------------------------------------------------------------
function GetDefaultLanguage: string;
begin
Result := 'english';
case Lo(GetSystemDefaultLangID) of
LANG_RUSSIAN : Result := 'russian';
LANG_CHINESE : Result := 'chinese-simplified';
end;
end;
function GetDefaultList: string;
begin
Result := 'english-kjv';
case Lo(GetSystemDefaultLangID) of
LANG_RUSSIAN : Result := 'russian';
LANG_CHINESE : Result := 'chinese-simplified';
LANG_FRENCH : Result := 'french';
LANG_GERMAN : Result := 'german';
LANG_UKRAINIAN : Result := 'ukrainian';
LANG_THAI : Result := 'thai';
end;
Result := ApplicationPath + 'lists' + Slash + Result + '.txt';
end;
//-----------------------------------------------------------------------------
// Windows system functions
//-----------------------------------------------------------------------------
function KeyShiftDown: boolean;
begin
Result := (GetKeyState(VK_SHIFT) < 0);
end;
function GetDeskWallpaper: WideString;
var
name: array [0..MAX_PATH] of Char;
const
SPI_GETDESKWALLPAPER = 115;
begin
Result := '';
if SystemParametersInfo(SPI_GETDESKWALLPAPER, MAX_PATH, @name, 0) then Result := WideString(name);
end;
procedure HideAppOnTaskbar (Form : TForm);
var
AppHandle : HWND;
begin
AppHandle := GetParent(FmxHandleToHWND(Form.Handle));
ShowWindow(AppHandle, SW_HIDE);
SetWindowLong(AppHandle, GWL_EXSTYLE, GetWindowLong(AppHandle, GWL_EXSTYLE) or WS_EX_TOOLWINDOW);
end;
//-----------------------------------------------------------------------------
procedure OpenDesktopControl;
begin
ShellExecute(0,'open','rundll32.exe','shell32.dll, Control_RunDLL desk.cpl desk,@Desktop','',SW_SHOW);
end;
//-----------------------------------------------------------------------------
// Firemonkey functions
//-----------------------------------------------------------------------------
procedure OpenUrl(s: String);
begin
ShellExecute(0,'open',PChar(s),'','',SW_SHOW)
end;
//-----------------------------------------------------------------------------
procedure WordWrap(L: TLabel; Max: Single);
var
Text : String;
s : String;
i,k : integer;
c : char;
begin
Text := L.Text;
L.Text := '*'; // '' приводит к ошибке
k := 0;
for i:=1 to Text.Length do
begin
c := Text[i];
if c = ' ' then k := i;
if i = 1 then L.Text := c else L.Text := L.Text + c;
// L.Text := L.Text + c; // вариант до бага
if (L.Width > Max) and (k > 0) then
begin
s := L.Text;
s[k] := CRLF;
L.Text := s;
k := 0;
end;
end;
end;
//-----------------------------------------------------------------------------
// Fonts
//-----------------------------------------------------------------------------
function EnumFontsProc(var LogFont: TLogFont; var TextMetric: TTextMetric; FontType: Integer; Data: Pointer): Integer; stdcall;
var
S: TStrings;
Temp: string;
begin
S := TStrings(Data);
Temp := LogFont.lfFaceName;
if (S.Count = 0) or (AnsiCompareText(S[S.Count-1], Temp) <> 0) then S.Add(Temp);
Result := 1;
end;
procedure GetFonts(List: TStringList);
var
LogFont : TLogFont;
DC : HDC;
begin
DC := GetDC(0);
try
List.Clear;
FillChar(LogFont, sizeof(LogFont), 0);
LogFont.lfCharset := DEFAULT_CHARSET;
EnumFontFamiliesEx(DC, LogFont, @EnumFontsProc, LongInt(List), 0);
List.Sort;
finally
ReleaseDC(0, DC);
end;
end;
//-----------------------------------------------------------------------------
// Mutex
//-----------------------------------------------------------------------------
function GetWinInfo(h: HWND): string;
var
temp : PWideChar;
begin
GetMem(temp,255);
GetWindowText(h,temp,255);
Result := temp;
FreeMem(temp);
end;
{---}
procedure GetWinTree;
var
hw : HWND;
begin
hw := Windows.FindWindow(nil,nil); // Loop through all Top Level Windows
while hw > 32 do
begin
if GetWinInfo(hw) = ApplicationName then
begin
ShowWindow(hw,SW_SHOW);
SetForegroundWindow(hw);
end;
hw:=GetWindow(hw,GW_HWNDNEXT); // Get the next window
end;
end;
function CreateMutex(name: PWideChar): boolean;
begin
Mutex := Windows.CreateMutex(nil, False, name);
Result := GetLastError <> ERROR_ALREADY_EXISTS;
if not Result then GetWinTree;
end;
procedure CloseMutex;
begin
CloseHandle(Mutex);
end;
end.