-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.pas
341 lines (312 loc) · 9.34 KB
/
MainForm.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
unit MainForm;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.Menus, Vcl.ExtDlgs,
Vcl.StdCtrls, Vcl.FileCtrl, IniFiles, AboutForm, Registry, Vcl.Themes,
Vcl.Buttons, System.ImageList, Vcl.ImgList;
type
TForm1 = class(TForm)
TrayIcon1: TTrayIcon;
PopupMenu1: TPopupMenu;
Exit1: TMenuItem;
Pause1: TMenuItem;
Timer1: TTimer;
ListBox1: TListBox;
Settings1: TMenuItem;
NextWallpaper1: TMenuItem;
About1: TMenuItem;
DirectoryLabel: TLabel;
DirectoryEdit: TEdit;
IntervalLabel: TLabel;
IntervalEdit: TEdit;
FileOpenDialog1: TFileOpenDialog;
Rescan1: TMenuItem;
StartupCheckBox: TCheckBox;
N1: TMenuItem;
ThemeComboBox: TComboBox;
ThemeLabel: TLabel;
ChangeWallpaperCheckBox: TCheckBox;
ChooseFolderButton: TImage;
procedure FormCreate(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Pause1Click(Sender: TObject);
procedure Settings1Click(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure NextWallpaper1Click(Sender: TObject);
procedure PopupMenu1Popup(Sender: TObject);
procedure About1Click(Sender: TObject);
procedure Rescan1Click(Sender: TObject);
procedure ThemeComboBoxChange(Sender: TObject);
procedure TrayIcon1DblClick(Sender: TObject);
procedure ChooseFolderButtonClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Res: Integer;
options: TSelectDirOpts;
chosenDirectory: string;
FDir: string;
WallpaperName: string;
implementation
{$R *.dfm}
procedure ScanDir(StartDir: String; Mask: string; List: TStrings);
Var
SearchRec: TSearchRec;
Begin
IF Mask = '' then
Mask := '*.*';
IF StartDir[Length(StartDir)] <> '\' then
StartDir := StartDir + '\';
IF FindFirst(StartDir + Mask, faAnyFile, SearchRec) = 0 then
Begin
Repeat
Application.ProcessMessages;
IF (SearchRec.Attr and faDirectory) <> faDirectory then
List.Add(StartDir + SearchRec.Name)
else IF (SearchRec.Name <> '..') and (SearchRec.Name <> '.') then
Begin
List.Add(StartDir + SearchRec.Name + '\');
ScanDir(StartDir + SearchRec.Name + '\', Mask, List);
End;
Until FindNext(SearchRec) <> 0;
FindClose(SearchRec);
End;
end;
procedure SetAutoStart(AppName, AppTitle: string; bRegister: Boolean);
const
RegKey = '\Software\Microsoft\Windows\CurrentVersion\Run';
var
Registry: TRegistry;
begin
Registry := TRegistry.Create;
try
Registry.RootKey := HKEY_CURRENT_USER;
if Registry.OpenKey(RegKey, False) then
begin
if bRegister = False then
Registry.DeleteValue(AppTitle)
else
Registry.WriteString(AppTitle, AppName);
end;
finally
Registry.Free;
end;
end;
procedure SaveSettings();
Var
IniFile: TIniFile;
Begin
IniFile := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
IniFile.WriteString('Main', 'Directory', Form1.DirectoryEdit.Text);
IniFile.WriteString('Main', 'Interval', Form1.IntervalEdit.Text);
IniFile.WriteBool('Main', 'Startup', Form1.StartupCheckBox.Checked);
SetAutoStart(ParamStr(0), 'WallRnd', Form1.StartupCheckBox.Checked);
IniFile.WriteInteger('Main', 'Theme', Form1.ThemeComboBox.ItemIndex);
IniFile.WriteBool('Main', 'ChangeWallpaper',
Form1.ChangeWallpaperCheckBox.Checked);
end;
procedure LoadSettings();
Var
IniFile: TIniFile;
Begin
IniFile := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
Form1.DirectoryEdit.Text := IniFile.ReadString('Main', 'Directory', '');
Form1.IntervalEdit.Text := IniFile.ReadString('Main', 'Interval', '');
Form1.StartupCheckBox.Checked := IniFile.ReadBool('Main', 'Startup', False);
Form1.ThemeComboBox.ItemIndex := IniFile.ReadInteger('Main', 'Theme', 0);
TStyleManager.TrySetStyle(Form1.ThemeComboBox.Text);
Form1.ChangeWallpaperCheckBox.Checked :=
IniFile.ReadBool('Main', 'ChangeWallpaper', False);
Form1.Pause1.Checked := IniFile.ReadBool('Main', 'Timer', False);
Form1.Timer1.Enabled := IniFile.ReadBool('Main', 'Timer', False);
end;
function BoolToStr(const value: Boolean): string;
begin
if value then
Result := 'Enabled'
else
Result := 'Disabled';
end;
procedure TrayIconHint();
Var
IniFile: TIniFile;
begin
IniFile := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
Form1.TrayIcon1.Hint := 'Timer: ' + BoolToStr(Form1.Timer1.Enabled) +
sLineBreak + 'Interval: ' + Form1.IntervalEdit.Text + ' minutes' +
sLineBreak + 'Current image: ' + IniFile.ReadString('Main', 'WallpaperName',
'') + sLineBreak + 'Number of wallpapers: ' +
Form1.ListBox1.Items.Count.ToString;
end;
procedure RandomizeWallpaper();
var
FileExtension: String;
RandomNumber: Integer;
IniFile: TIniFile;
begin
if Form1.ListBox1.Items.Count <> 0 then
begin
RandomNumber := Random(Form1.ListBox1.Items.Count);
FileExtension := ExtractFileExt(Form1.ListBox1.Items[RandomNumber]);
WallpaperName := ExtractFileName(Form1.ListBox1.Items[RandomNumber]);
if (FileExtension = '.jpeg') Or (FileExtension = '.jpg') Or
(FileExtension = '.png') Or (FileExtension = '.bmp') then
begin
IniFile := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0,
pChar(Form1.ListBox1.Items[RandomNumber]), SPIF_SENDWININICHANGE);
IniFile.WriteString('Main', 'WallpaperName', WallpaperName);
end
else
begin
RandomizeWallpaper();
end;
end;
end;
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
Var
IniFile: TIniFile;
begin
CanClose := False;
hide();
SaveSettings();
if (DirectoryEdit.Text <> '') And (IntervalEdit.Text <> '') then
begin
IniFile := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
Timer1.Interval := StrToInt(IntervalEdit.Text) * 60000;
Timer1.Enabled := IniFile.ReadBool('Main', 'Timer', False);
end;
TrayIconHint();
end;
procedure TForm1.FormCreate(Sender: TObject);
Var
IniFile: TIniFile;
begin
LoadSettings();
if (DirectoryEdit.Text <> '') And (IntervalEdit.Text <> '') then
begin
IniFile := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
Application.ShowMainForm := False;
Timer1.Interval := StrToInt(IntervalEdit.Text) * 60000;
Timer1.Enabled := IniFile.ReadBool('Main', 'Timer', False);
ListBox1.Items.Clear;
ScanDir(Form1.DirectoryEdit.Text, '', Form1.ListBox1.Items);
if ChangeWallpaperCheckBox.Checked then
begin
RandomizeWallpaper();
end;
end;
TrayIconHint();
end;
procedure TForm1.NextWallpaper1Click(Sender: TObject);
begin
if (DirectoryEdit.Text <> '') And (IntervalEdit.Text <> '') then
begin
RandomizeWallpaper();
end;
end;
procedure TForm1.Pause1Click(Sender: TObject);
Var
IniFile: TIniFile;
begin
IniFile := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
IniFile.WriteBool('Main', 'Timer', Form1.Pause1.Checked);
if (DirectoryEdit.Text <> '') And (IntervalEdit.Text <> '') then
begin
Timer1.Interval := StrToInt(IntervalEdit.Text) * 60000;
Timer1.Enabled := Pause1.Checked
end;
TrayIconHint();
end;
procedure TForm1.PopupMenu1Popup(Sender: TObject);
Begin
if (DirectoryEdit.Text <> '') And (IntervalEdit.Text <> '') then
begin
Pause1.Enabled := True;
end
else
begin
Pause1.Enabled := False;
end;
end;
procedure TForm1.Rescan1Click(Sender: TObject);
begin
if Form1.ListBox1.Items.Count <> 0 then
begin
ListBox1.Items.Clear;
ScanDir(Form1.DirectoryEdit.Text, '', Form1.ListBox1.Items);
end;
end;
procedure TForm1.Settings1Click(Sender: TObject);
begin
show;
end;
procedure TForm1.ThemeComboBoxChange(Sender: TObject);
begin
TStyleManager.TrySetStyle(Form1.ThemeComboBox.Text);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
RandomizeWallpaper();
TrayIconHint();
end;
procedure TForm1.TrayIcon1DblClick(Sender: TObject);
begin
if (DirectoryEdit.Text <> '') And (IntervalEdit.Text <> '') then
begin
RandomizeWallpaper();
TrayIconHint();
end;
end;
procedure TForm1.About1Click(Sender: TObject);
begin
Form2.show;
end;
procedure TForm1.ChooseFolderButtonClick(Sender: TObject);
begin
if Win32MajorVersion >= 6 then
with TFileOpenDialog.Create(nil) do
try
options := [fdoPickFolders, fdoPathMustExist, fdoForceFileSystem];
DefaultFolder := FDir;
FileName := FDir;
if Execute then
if FileName <> '' then
begin
DirectoryEdit.Text := FileName;
ListBox1.Items.Clear;
ScanDir(Form1.DirectoryEdit.Text, '', Form1.ListBox1.Items);
end;
finally
Free;
end
else if SelectDirectory('Select Directory', ExtractFileDrive(FDir), FDir,
[sdNewUI, sdNewFolder]) then
if FDir <> '' then
begin
DirectoryEdit.Text := FDir;
ListBox1.Items.Clear;
ScanDir(Form1.DirectoryEdit.Text, '', Form1.ListBox1.Items);
end;
end;
procedure TForm1.Exit1Click(Sender: TObject);
var
Res: Integer;
begin
Res := MessageBox(Self.Handle, pChar('Really close the app?'), pChar('Exit'),
MB_YESNO + MB_ICONINFORMATION);
case Res of
IDYES:
begin
Application.Terminate();
end;
end;
end;
end.