Browse for Computers, Folders, Files and Printers | |||||||||
| |||||||||
First, let's look at what SHBrowseForFolder needs. Here's the function declaration: function SHBrowseForFolder(var BrowseInfo: TBrowseInfo): PItemIDList; stdcall; We pass in a complicated record type BrowseInfo to initialize and customize the Browse For Folder dialog box. We get back an item ID list (let's say: location of the selected folder, not to confuse to much). Now we'll see how to fill in a record structure with information that initializes the Browse for Folder dialog box, then call SHBrowseForFolder to display the dialog box. BrowseInfo structure Two of the main elements of BrowseInfo are the lpszTitle and ulFlags fields. The dialog box displays the contents of lpszTitle in a static text control above the treeview. The ulFlags element sets the value which determines what the dialog displays and allows the user to select. We can specify zero or more of the flags in order to make the dialog box much more useful than just browsing for folders. Some of the flags that can be specified to enhance the Browse For Folders dialog box are:
Delphi code When we put all this in a Delphi function that will create the structure, initialize it, and call SHBrowseForFolder() to display the dialog box, we get something like: uses ShellAPI, ShlObj; ... function BrowseDialog(const Title: string; const Flag: integer): string; var lpItemID : PItemIDList; BrowseInfo : TBrowseInfo; DisplayName : array[0..MAX_PATH] of char; TempPath : array[0..MAX_PATH] of char; begin Result:=''; FillChar(BrowseInfo, sizeof(TBrowseInfo), #0); with BrowseInfo do begin hwndOwner := Application.Handle; pszDisplayName := @DisplayName; lpszTitle := PChar(Title); ulFlags := Flag; end; lpItemID := SHBrowseForFolder(BrowseInfo); if lpItemId <> nil then begin SHGetPathFromIDList(lpItemID, TempPath); Result := TempPath; GlobalFreePtr(lpItemID); end; end; The BrowseDialog function takes two parameters: Title and Flag. Title represents the text that appears above the treeview (lpszTitle field of the BrowseInfo record). Flag parameter is used to fill the ulFlags field. The function can now be simply called (to display the folder selected by the user) like: procedure TfrMain.btnBrowseClick(Sender: TObject); var sTitle, sFolder: string; iFlag : integer; begin sTitle:='Choose a ' + rgBrowseFor.Items[rgBrowseFor.ItemIndex]; case rgBrowseFor.ItemIndex of 0: iFlag := BIF_RETURNONLYFSDIRS; 1: iFlag := BIF_BROWSEINCLUDEFILES; 2: iFlag := BIF_BROWSEFORCOMPUTER; 3: iFlag := BIF_BROWSEFORPRINTER; end; sFolder := BrowseDialog(sTitle, iFlag); if sFolder <> '' then edSelected.text := sFolder else edSelected.text := 'Nothing selected'; end; Tags: BrowseInfo CreateFile Delphi Delphi Folder Navigation Dialog PItemIDList SHBrowseForFolder SHGetPathFromIDList | |||||||||
| |||||||||
| |||||||||
Login for comment |