Author: Eugene Mayevski
It is possible to specify initial folder in Open/Save dialogs. However sometimes it
is necessary to change current folder, for example, in responce to change of
filter.
Answer:
Windows doesn't offer a way to change current dialog. However there is a very handy
approach to this problem - imitate user actions. Below is the sample code of how
the folder is changed when filter is changed.
procedure SaveDialogTypeChange is a handler for OnTypeChange event.
Depending on the type different folders are selected. This piece of code saves the
value of file name edit box, puts a new folder name there, then imitates click of
OK button and then restores contents of the edit box.
Add Dlgs to "uses" clause of your unit.
1
2 procedure TMainForm.SaveDialogTypeChange(Sender: TObject);
3 var
4 S, S1: string;
5 EditHandle: THandle;
6 startp,
7 endp: DWORD;
8 begin
9 s := '';
10 if SaveDialog.FilterIndex = 2 then
11 begin
12 s := 'c:\program files';
13 end
14 else if SaveDialog.FilterIndex = 3 then
15 begin
16 s := 'd:\program files';
17 end;
18 if s <> '' then
19 begin
20 EditHandle := GetDlgItem(GetParent(SaveDialog.Handle), edt1);
21 if EditHandle <> 0 then
22 begin
23 SetLength(S1, GetWindowTextLength(EditHandle) + 1);
24 GetWindowText(EditHandle, PChar(S1), Length(S1));
25 SetLength(S1, StrLen(PChar(S1)));
26 SendMessage(EditHandle, EM_GETSEL, Integer(@StartP), Integer(@EndP));
27 SetWindowText(EditHandle, PChar(S));
28 SendMessage(GetParent(SaveDialog.Handle), WM_COMMAND, 1,
29 GetDlgItem(GetParent(SaveDialog.Handle), IDOK));
30 if Length(S1) > 0 then
31 if S1[Length(S1)] = #10 then
32 Delete(S1, Length(S1), 1);
33 SetWindowText(EditHandle, PChar(S1));
34 SendMessage(EditHandle, EM_SETSEL, StartP, EndP);
35 end;
36 end;
37 end;
|