forked from PMStanley/PSV-Exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrowseForFolderU.pas
71 lines (65 loc) · 2.46 KB
/
BrowseForFolderU.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
unit BrowseForFolderU;
///////////////////////////////////////////////////////////////////
// likely to have been sourced from
// https://www.cryer.co.uk/brian/delphi/howto_browseforfolder.htm
///////////////////////////////////////////////////////////////////
interface
function BrowseForFolder(const browseTitle: String;
const initialFolder: String = ''; const allowCreate : Boolean = False): String;
implementation
uses Windows, shlobj, Forms;
var
lg_StartFolder: String;
///////////////////////////////////////////////////////////////////
// Call back function used to set the initial browse directory.
///////////////////////////////////////////////////////////////////
function BrowseForFolderCallBack(Wnd: HWND; uMsg: UINT;
lParam, lpData: LPARAM): Integer stdcall;
begin
if uMsg = BFFM_INITIALIZED then
SendMessage(Wnd,BFFM_SETSELECTION,1,Integer(@lg_StartFolder[1]));
result := 0;
end;
///////////////////////////////////////////////////////////////////
// This function allows the user to browse for a folder
//
// Arguments:-
// browseTitle : The title to display on the browse dialog.
// initialFolder : Optional argument. Use to specify the folder
// initially selected when the dialog opens.
//
// Returns: The empty string if no folder was selected (i.e. if the
// user clicked cancel), otherwise the full folder path.
///////////////////////////////////////////////////////////////////
function BrowseForFolder(const browseTitle: String;
const initialFolder: String =''; const allowCreate : Boolean = False): String;
var
browse_info: TBrowseInfo;
folder: array[0..MAX_PATH] of char;
find_context: PItemIDList;
begin
FillChar(browse_info,SizeOf(browse_info),#0);
lg_StartFolder := initialFolder;
browse_info.pszDisplayName := @folder[0];
browse_info.lpszTitle := PChar(browseTitle);
if allowCreate then begin
browse_info.ulFlags := BIF_RETURNONLYFSDIRS or BIF_NEWDIALOGSTYLE;
end else begin
browse_info.ulFlags := BIF_RETURNONLYFSDIRS;
end;
browse_info.hwndOwner := Application.Handle;
if initialFolder <> '' then
browse_info.lpfn := BrowseForFolderCallBack;
find_context := SHBrowseForFolder(browse_info);
if Assigned(find_context) then
begin
if SHGetPathFromIDList(find_context,folder) then
result := folder
else
result := '';
GlobalFreePtr(find_context);
end
else
result := '';
end;
end.