-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUnit1.pas
More file actions
252 lines (205 loc) · 4.92 KB
/
Copy pathUnit1.pas
File metadata and controls
252 lines (205 loc) · 4.92 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
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
unit Unit1;
interface
uses
Winapi.Windows,
Winapi.Messages,
System.Generics.Collections,
System.SysUtils,
System.Variants,
System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.Menus,
Vcl.ExtCtrls, Vcl.StdCtrls, Vcl.Mask;
type
TRunFile = class
public
Path : string;
end;
TRunFileList = TObjectList<TRunFile>;
TForm1 = class(TForm)
TrayIcon1: TTrayIcon;
TrayPopup: TPopupMenu;
Configure1: TMenuItem;
N1: TMenuItem;
Exit1: TMenuItem;
PathEdit: TLabeledEdit;
ChangeTimer: TTimer;
SaveBtn: TButton;
procedure ChangeTimerTimer(Sender: TObject);
procedure Configure1Click(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure PathEditChange(Sender: TObject);
procedure PopupItemClick(Sender: TObject);
procedure TrayIcon1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure TrayPopupPopup(Sender: TObject);
private
Files : TRunFileList;
ShortcutPath : string;
Updating : Boolean;
procedure BuildPopup;
procedure ClearPopup;
procedure UpdatePath;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
System.IOUtils,
System.Win.Registry,
WinApi.ShellAPI;
const
DefaultItemCount = 3;
RegRoot = 'Software\Zencode\TrayLink';
procedure TForm1.BuildPopup;
procedure AddDirectory(Path : string; Parent : TMenuItem; Index : Integer);
var
sr : TSearchRec;
p : string;
f : TRunFile;
mi : TMenuItem;
begin
if FindFirst(TPath.Combine(Path, '*.*'), faAnyFile, sr) = 0 then begin
repeat
if (sr.Name = '.') or (sr.Name = '..') then
continue;
p := TPath.Combine(ShortcutPath, sr.Name);
mi := TMenuItem.Create(TrayPopup);
if ExtractFileExt(sr.Name) = '.lnk' then begin
mi.Caption := Copy(sr.Name, 1, Length(sr.Name) - 4);
end else begin
mi.Caption := sr.Name;
end;
Parent.Insert(Index, mi);
if sr.Attr = faDirectory then begin
AddDirectory(p, mi, 0);
end else begin
f := TRunFile.Create;
f.Path := p;
Files.Add(f);
mi.Tag := NativeInt(f);
mi.OnClick := PopupItemClick;
end;
Inc(Index);
until FindNext(sr) <> 0;
FindClose(sr);
end;
end;
begin
Files.Clear;
if Length(ShortCutPath) = 0 then
Exit;
AddDirectory(ShortcutPath, TrayPopup.Items, 0);
end;
procedure TForm1.ChangeTimerTimer(Sender: TObject);
begin
ChangeTimer.Enabled := False;
SaveBtn.Enabled := False;
ClearPopup;
UpdatePath;
end;
procedure TForm1.ClearPopup;
var
mi : TMenuItem;
begin
while TrayPopup.Items.Count > DefaultItemCount do begin
mi := TrayPopup.Items[0];
TrayPopup.Items.Delete(0);
mi.Free;
end;
Files.Clear;
end;
procedure TForm1.Configure1Click(Sender: TObject);
begin
Updating := True;
PathEdit.Text := ShortcutPath;
Updating := False;
Show;
end;
procedure TForm1.Exit1Click(Sender: TObject);
begin
Exit1.Tag := 1;
Close;
end;
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
if Exit1.Tag <> 1 then begin
Hide;
CanClose := False;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
reg : TRegistry;
begin
Files := TRunFileList.Create;
reg := TRegistry.Create(KEY_READ);
try
reg.RootKey := HKEY_CURRENT_USER;
if reg.OpenKeyReadOnly(RegRoot) then begin
if reg.ValueExists('Path') then
ShortcutPath := reg.ReadString('Path');
end;
finally
reg.Free;
end;
TrayIcon1.Visible := True;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
TrayIcon1.Visible := False;
Files.Free;
end;
procedure TForm1.PathEditChange(Sender: TObject);
begin
if Updating then
Exit;
ChangeTimer.Enabled := False;
ChangeTimer.Enabled := True;
SaveBtn.Enabled := True;
end;
procedure TForm1.PopupItemClick(Sender: TObject);
var
f : TRunFile;
begin
f := TRunFile((Sender as TMenuItem).Tag);
ShellExecute(0, 'open', PChar(f.Path), nil, nil, SW_SHOW);
end;
procedure TForm1.TrayIcon1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
begin
SetForegroundWindow(Handle);
TrayPopup.Popup(X, Y);
PostMessage(Handle, WM_NULL, 0, 0);
end;
end;
procedure TForm1.TrayPopupPopup(Sender: TObject);
begin
if TrayPopup.Items.Count = DefaultItemCount then
BuildPopup;
end;
procedure TForm1.UpdatePath;
var
reg : TRegistry;
begin
ShortcutPath := PathEdit.Text;
reg := TRegistry.Create(KEY_WRITE);
try
reg.RootKey := HKEY_CURRENT_USER;
if not reg.OpenKey(RegRoot, True) then
RaiseLastOSError;
reg.WriteString('Path', ShortcutPath);
finally
reg.Free;
end;
end;
end.