-
Notifications
You must be signed in to change notification settings - Fork 19
/
FormSkinsDisk.pas
313 lines (229 loc) · 9.95 KB
/
FormSkinsDisk.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
UNIT FormSkinsDisk;
{=============================================================================================================
Gabriel Moraru
2024.05
See Copyright.txt
--------------------------------------------------------------------------------------------------------------
Universal skin loader. Loads skins from disk (vsf file)
DON'T ADD IT TO ANY DPK!
To use it:
Application.ShowMainForm:= FALSE; // Necessary so the form won't flicker during skin loading at startup
MainForm.Visible:= FALSE;
LoadLastSkin (during application initialization)
MainForm.Show;
Skins should be present in the 'System\skins' folder
On first run, set the DefaultSkin to an existing file (no path) like: 'Graphite Green.vsf'. Leave it empty if you want the default Windows theme to load
Call TfrmSkinDisk.ShowEditor to show this form.
Info:
If you want to make your code aware of the selected VCL style, you can use StyleServices.GetStyleColor, StyleServices.GetStyleFontColor, and StyleServices.GetSystemColor in Vcl.Themes unit.
It does not work with MDI forms (the MDI children are not correctly painted). However, it works if I apply the skins directly from the IDE.
-------------------------------------------------------------------------------------------------------------
Skins folder:
c:\MyProjects\Packages\VCL Styles utils\Styles\
c:\Users\Public\Documents\Embarcadero\Studio\20.0\Styles\
c:\Users\Public\Documents\Embarcadero\Studio\21.0\Styles\
Tester:
c:\MyProjects\Packages\VCL Styles Tools\FrmSkins tester\
More:
https://subscription.packtpub.com/book/application_development/9781783559589/1/ch01lvl1sec10/changing-the-style-of-your-vcl-application-at-runtime
KNOWN BUGS:
XE7
TStyleManager.IsValidStyle always fails if Vcl.Styles is not in the USES list!!
http://stackoverflow.com/questions/30328644/how-to-check-if-a-style-file-is-already-loaded
caFree
procedure Tfrm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:= caFree;
Delphi bug: Don't use caFree: https://quality.embarcadero.com/browse/RSP-33140
end;
Solution:
https://stackoverflow.com/questions/70840792/how-to-patch-vcl-forms-pas
=============================================================================================================}
INTERFACE
{$DENYPACKAGEUNIT ON} {Prevents unit from being placed in a package. https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Packages_(Delphi)#Naming_packages }
USES
Winapi.Windows, System.SysUtils, Vcl.StdCtrls, Vcl.Controls, Vcl.ExtCtrls, System.Classes, Vcl.Forms;
TYPE
TfrmSkinDisk = class(TForm)
lBox: TListBox;
lblTop: TLabel;
pnlBottom: TPanel;
pnlBtm: TPanel;
btnOK: TButton;
btnSkinEditor: TButton;
lblMoreSkinsTrial: TLabel;
procedure FormCreate (Sender: TObject);
procedure FormDestroy (Sender: TObject);
procedure lBoxClick (Sender: TObject);
procedure lblTopClick (Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: Char);
procedure btnSkinEditorClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure btnOKClick(Sender: TObject);
private
FOnDefaultSkin: TNotifyEvent;
procedure PopulateSkins;
public
class procedure CreateFormModal; static;
class procedure CreateForm(Nottify: TNotifyEvent= NIL); static;
published
property OnDefaultSkin: TNotifyEvent read FOnDefaultSkin write FOnDefaultSkin;
end;
CONST
wwwSkinDesinger = 'https://www.bionixwallpaper.com/downloads/Skin_Designer/index.html';
procedure LoadLastSkin(CONST DefaultSkin: string= ''); { On first run, set the DefaultSkin to an existing file (no path) like: 'Graphite Green.vsf'. Leave it empty if you want the default Windows theme to load }
IMPLEMENTATION {$R *.dfm}
USES
ccColors, cTranslate, cmINIFileQuick, {uLinks,} Vcl.Themes, cbAppData, csExecuteShell,
cvIniFile, IOUtils, ccIO, ccCore, cbDialogs; { VCL.Styles is mandatory here}
CONST
DefWinTheme= 'Windows default theme';
VAR
LastSkin: string; { Disk short file name (not full path) for the current loaded skin }
{-----------------------------------------------------------------------------------------------------------------------
UTILS
-----------------------------------------------------------------------------------------------------------------------}
function GetSkinDir: string;
begin
Result:= AppData.SysDir+ 'skins\';
end;
function LoadSkinFromFile(CONST DiskShortName: string): Boolean;
VAR Style : TStyleInfo;
begin
Result:= FileExists(GetSkinDir+ DiskShortName);
if Result then
if TStyleManager.IsValidStyle(GetSkinDir+ DiskShortName, Style)
then
if NOT TStyleManager.TrySetStyle(Style.Name, FALSE)
then
begin
TStyleManager.LoadFromFile(GetSkinDir+ DiskShortName);
TStyleManager.SetStyle(Style.Name);
end
else Result:= FALSE
else
MesajError('Style is not valid: '+ GetSkinDir+ DiskShortName);
end;
procedure LoadLastSkin(CONST DefaultSkin: string= '');
begin
LastSkin:= cmINIFileQuick.ReadString('LastSkin', DefaultSkin); { This is a relative path so the skin can still be loaded when the application is moved to a different folder }
if LastSkin = ''
then LastSkin:= DefaultSkin;
if (LastSkin > '')
AND (LastSkin <> DefWinTheme) { DefWinTheme represents the default Windows theme/skin. In other words don't load any skin file. Let Win skin the app }
then LoadSkinFromFile(LastSkin);
end;
{-----------------------------------------------------------------------------------------------------------------------
SHOW EDITOR
-----------------------------------------------------------------------------------------------------------------------}
{ THERE IS A BUG THAT CRASHES THE PROGRAM WHEN I CLOSE THIS WINDOW (after applying a skin) }
class procedure TfrmSkinDisk.CreateFormModal;
VAR frmEditor: TfrmSkinDisk;
begin
AppData.CreateFormHidden(TfrmSkinDisk, frmEditor);
if Translator <> NIL
then Translator.LoadFormTranlation(frmEditor);
{ Closed by mrOk/mrCancel. Set to caFree. }
frmEditor.ShowModal; { Bug: IF I use ShowModal, after applying a new skin, the window will loose its 'modal' attribute! }
end;
{ Show non modal, for testing against bug: Crash when closing the "Skins" form.
Fixed by keeping the form open!
Remember to call FreeAndNil(frmEditor) }
class procedure TfrmSkinDisk.CreateForm(Nottify: TNotifyEvent= NIL);
VAR frmEditor: TfrmSkinDisk;
begin
AppData.CreateFormHidden(TfrmSkinDisk, frmEditor);
frmEditor.OnDefaultSkin:= Nottify;
if Translator <> NIL
then Translator.LoadFormTranlation(frmEditor);
frmEditor.Show; { Bug: IF I use ShowModal, after applying a new skin, the window will lose its 'modal' attribute! }
end;
{-----------------------------------------------------------------------------------------------------------------------
CREATE
-----------------------------------------------------------------------------------------------------------------------}
procedure TfrmSkinDisk.FormCreate(Sender: TObject);
begin
LoadForm(Self);
PopulateSkins;
lblTop.Hint:= 'Skin files are located in '+ GetSkinDir;
end;
procedure TfrmSkinDisk.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:= caFree;
{Action:= caFree; Delphi bug: https://quality.embarcadero.com/browse/RSP-33140. Fixed in delphi 11 }
end;
procedure TfrmSkinDisk.FormDestroy(Sender: TObject);
begin
SaveForm(Self);
if NOT AppData.Initializing
then cmINIFileQuick.WriteString ('LastSkin', LastSkin); { We don't save anything if the start up was improper! }
end;
procedure TfrmSkinDisk.btnOKClick(Sender: TObject);
begin
Close; // Not needed when I show the form modal
end;
{-------------------------------------------------------------------------------------------------------------
Populate skins
-------------------------------------------------------------------------------------------------------------}
procedure TfrmSkinDisk.lblTopClick(Sender: TObject);
begin
PopulateSkins;
end;
procedure TfrmSkinDisk.PopulateSkins;
VAR
s, FullFileName: string;
begin
lBox.Clear;
lBox.Items.Add(DefWinTheme); { This corresponds to Windows' default theme }
lblTop.Hint:= GetSkinDir;
if NOT DirectoryExists(GetSkinDir) then
begin
lblTop.Caption:= 'The skin directory could not be located! '+ GetSkinDir+ CRLF+ 'Add skins then click here to refresh the list.';
lblTop.Color:= clRedBright;
lblTop.Transparent:= FALSE;
EXIT;
end;
{ Display all *.vsf files }
for FullFileName in TDirectory.GetFiles(GetSkinDir, '*.vsf') DO
begin
s:= ExtractFileName(FullFileName);
lBox.Items.Add(s);
end;
lBox.ItemIndex := lBox.Items.IndexOf(TStyleManager.ActiveStyle.Name);
end;
procedure TfrmSkinDisk.lBoxClick(Sender: TObject);
begin
if lBox.ItemIndex < 0 then EXIT;
lBox.Enabled:= FALSE; { Prevent user to double click (because of Application.ProcessMessages below) }
TRY
LastSkin:= lBox.Items[lBox.ItemIndex];
if LastSkin= DefWinTheme then
begin
TStyleManager.SetStyle('Windows');
LastSkin:= DefWinTheme;
if Assigned(FOnDefaultSkin) then FOnDefaultSkin(Self);
end
else
if LoadSkinFromFile(LastSkin) then
begin
{ Bug fix for: http://stackoverflow.com/questions/30328924/form-losses-modal-attribute-after-changing-app-style?noredirect=1#comment48752692_30328924
Seems to have been fixed in Alexandria (from which I copied the patch. }
Application.ProcessMessages;
BringToFront;
end;
FINALLY
lBox.Enabled:= TRUE;
END;
end;
procedure TfrmSkinDisk.btnSkinEditorClick(Sender: TObject);
begin
if FileExists(AppData.SysDir+ 'SkinDesigner.exe')
then ExecuteShell(AppData.SysDir+ 'SkinDesigner.exe')
else ExecuteURL(wwwSkinDesinger);
end;
procedure TfrmSkinDisk.FormKeyPress(Sender: TObject; var Key: Char);
begin
if Ord(Key) = VK_RETURN then Close;
if Ord(Key) = VK_ESCAPE then Close;
end;
end.