1 | unit ACLShellUtilityUnit;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | // Win32 only
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Forms;
|
---|
9 |
|
---|
10 | // Retrieves the "My Documents" folder
|
---|
11 | function GetPersonalFolder: string;
|
---|
12 |
|
---|
13 | // Encapsulates Windows shell folder browser.
|
---|
14 | // Unlike the Delphi SelectDirectory function, this
|
---|
15 | // takes an owner form so that you cannot select the
|
---|
16 | // owner form!
|
---|
17 | function BrowseForFolder( const Owner: TForm;
|
---|
18 | const Caption: string;
|
---|
19 | const Root: WideString;
|
---|
20 | out Directory: string): Boolean;
|
---|
21 |
|
---|
22 | // Launch explorer for the specified directory
|
---|
23 | procedure Explore( Directory: string;
|
---|
24 | TreeView: boolean = true );
|
---|
25 |
|
---|
26 | implementation
|
---|
27 |
|
---|
28 | uses
|
---|
29 | ShlObj, ActiveX, Windows,
|
---|
30 | Dialogs,
|
---|
31 | ACLFileUtility, ACLUtility;
|
---|
32 |
|
---|
33 | function GetPersonalFolder: string;
|
---|
34 | var
|
---|
35 | pIDList: PItemIDList;
|
---|
36 | Path: array[ 0..MAX_PATH ] of char;
|
---|
37 | begin
|
---|
38 | SHGetSpecialFolderLocation( HWND_DESKTOP,
|
---|
39 | CSIDL_PERSONAL,
|
---|
40 | pIDList );
|
---|
41 | SHGetPathFromIDList( pIDList, path );
|
---|
42 | Result := path;
|
---|
43 | end;
|
---|
44 |
|
---|
45 | function BrowseForFolder( const Owner: TForm;
|
---|
46 | const Caption: string;
|
---|
47 | const Root: WideString;
|
---|
48 | out Directory: string): Boolean;
|
---|
49 | var
|
---|
50 | BrowseInfo: TBrowseInfo;
|
---|
51 | Buffer: PChar;
|
---|
52 | RootItemIDList, ItemIDList: PItemIDList;
|
---|
53 | ShellMalloc: IMalloc;
|
---|
54 | IDesktopFolder: IShellFolder;
|
---|
55 | Eaten, Flags: LongWord;
|
---|
56 | begin
|
---|
57 | Result := False;
|
---|
58 | Directory := '';
|
---|
59 | FillChar(BrowseInfo, SizeOf(BrowseInfo), 0);
|
---|
60 | if (ShGetMalloc(ShellMalloc) = S_OK) and (ShellMalloc <> nil) then
|
---|
61 | begin
|
---|
62 | Buffer := ShellMalloc.Alloc(MAX_PATH);
|
---|
63 | try
|
---|
64 | SHGetDesktopFolder(IDesktopFolder);
|
---|
65 | IDesktopFolder.ParseDisplayName(Application.Handle, nil,
|
---|
66 | POleStr(Root), Eaten, RootItemIDList, Flags);
|
---|
67 | with BrowseInfo do
|
---|
68 | begin
|
---|
69 | hwndOwner := Owner.Handle;
|
---|
70 | pidlRoot := RootItemIDList;
|
---|
71 | pszDisplayName := Buffer;
|
---|
72 | lpszTitle := PChar(Caption);
|
---|
73 | ulFlags := BIF_RETURNONLYFSDIRS;
|
---|
74 | end;
|
---|
75 | ItemIDList := ShBrowseForFolder(BrowseInfo);
|
---|
76 | Result := ItemIDList <> nil;
|
---|
77 | if Result then
|
---|
78 | begin
|
---|
79 | ShGetPathFromIDList(ItemIDList, Buffer);
|
---|
80 | ShellMalloc.Free(ItemIDList);
|
---|
81 | Directory := Buffer;
|
---|
82 | end;
|
---|
83 | finally
|
---|
84 | ShellMalloc.Free(Buffer);
|
---|
85 | end;
|
---|
86 | end;
|
---|
87 | end;
|
---|
88 |
|
---|
89 | procedure Explore( Directory: string;
|
---|
90 | TreeView: boolean );
|
---|
91 | var
|
---|
92 | Parameters: string;
|
---|
93 | begin
|
---|
94 | if TreeView then
|
---|
95 | Parameters := '/e,' + Directory
|
---|
96 | else
|
---|
97 | Parameters := Directory;
|
---|
98 |
|
---|
99 | if not RunProgram( 'Explorer',
|
---|
100 | Parameters ) then
|
---|
101 | begin
|
---|
102 | ShowMessage( 'Could not run Windows Explorer: '
|
---|
103 | + GetLastAPIErrorString );
|
---|
104 | end;
|
---|
105 | end;
|
---|
106 |
|
---|
107 | end.
|
---|