Browse for Computers, Folders, Files and Printers

CK1820 
Created at Sep 10, 2007 03:02:40 
51   0   0   0  
By using the SHBrowseForFolder Windows API function and Delphi we can invoke a Windows system dialog used to browse for files and folders on users hard drive as well as network computers 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:




















BIF_BROWSEFORCOMPUTER Only returns computers. If the user selects anything other than a computer, the OK button is grayed.
BIF_BROWSEFORPRINTER Only returns printers. If the user selects anything other than a printer, the OK button is grayed.
BIF_RETURNONLYFSDIRS Only returns file system directories. If the user selects folders that aren't part of the file system, the OK button is grayed.
BIF_BROWSEINCLUDEFILES The browse dialog will display files as well as folders


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 Share on Facebook Share on X

◀ PREVIOUS
How to draw Transparent Text on bitmap
▶ NEXT
Checking If File Is In Use
  Comments 0
Login for comment
SIMILAR POSTS

Checking If File Is In Use (created at Sep 10, 2007)

Convert a mapped drive to a full UNC path (created at Sep 10, 2007)

Does my CD-ROM drive contain an audio CD? (created at Sep 10, 2007)

Delete files with the ability to UNDO (created at Sep 10, 2007)

Delete folders recursively (created at Sep 10, 2007)

From/to the 8.3 (short) format to/from the long format (created at Sep 10, 2007)

Get File 'Last Modified' attribute (created at Sep 10, 2007)

How to Split and Merge Files (created at Sep 10, 2007)

Path shortener: c:ABC...DEF.ghi (created at Sep 10, 2007)

Reading a directory content (created at Sep 10, 2007)

Convert a BMP to a JPG (created at Sep 10, 2007)

Convert TColor to Hex & Hex to TColor (created at Sep 10, 2007)

Cut a rectangle from an Image to Clipboard (created at Sep 10, 2007)

How to capture Windows Desktop to Bitmap (created at Sep 10, 2007)

How to Convert Pixels to Millimeters (created at Sep 10, 2007)

How to convert an ICO to a BMP (created at Sep 10, 2007)

How to draw rotated text (created at Sep 10, 2007)

How to draw transparent text on Windows Desktop (created at Sep 10, 2007)

Extract an icon from an Windows application and paint on a Form (created at Sep 10, 2007)

How to convert HIcon to TIcon ? (created at Sep 10, 2007)

Get Cursor Image (draw it on a Canvas) (created at Sep 10, 2007)

Grayscaling a bitmap ? (created at Sep 10, 2007)

Implementing a lasso drawing technique (created at Sep 10, 2007)

Is Point in Polygon? (created at Sep 10, 2007)

Paint a Form with a tiled bitmap image (created at Sep 10, 2007)

Rotating Text (created at Sep 10, 2007)

Show *any* graphics as Glyph on a SpeedButton (created at Sep 10, 2007)

TColor to HTML color (created at Sep 10, 2007)

TDesktopCanvas - draw on Windows Desktop (created at Sep 10, 2007)

TImage.Bitmap fade out (created at Sep 10, 2007)

OTHER POSTS IN THE SAME CATEGORY

How to Convert Pixels to Millimeters (created at Sep 10, 2007)

How to capture Windows Desktop to Bitmap (created at Sep 10, 2007)

Cut a rectangle from an Image to Clipboard (created at Sep 10, 2007)

Convert TColor to Hex & Hex to TColor (created at Sep 10, 2007)

Convert a BMP to a JPG (created at Sep 10, 2007)

Reading a directory content (created at Sep 10, 2007)

Path shortener: c:ABC...DEF.ghi (created at Sep 10, 2007)

How to Split and Merge Files (created at Sep 10, 2007)

Get File 'Last Modified' attribute (created at Sep 10, 2007)

From/to the 8.3 (short) format to/from the long format (created at Sep 10, 2007)

Delete folders recursively (created at Sep 10, 2007)

Delete files with the ability to UNDO (created at Sep 10, 2007)

Does my CD-ROM drive contain an audio CD? (created at Sep 10, 2007)

Convert a mapped drive to a full UNC path (created at Sep 10, 2007)

Checking If File Is In Use (created at Sep 10, 2007)

How to draw Transparent Text on bitmap (created at Sep 01, 2007)

How Do I Remove The Application Icon From The Taskbar? (created at Aug 25, 2007)

How To Show The Print Dialog And Print Text Files (created at Aug 25, 2007)

How To Make An Animated Application Icon (created at Aug 25, 2007)

How To Pass The Focus To The Next/prior Control (created at Aug 25, 2007)

How To Make Rounded Windows (created at Aug 25, 2007)

How To Get Windows Uptime (created at Aug 25, 2007)

How To Find Out Total And Available Memory (created at Aug 25, 2007)

How To Change The System Time (created at Aug 25, 2007)

How To Get The Windows OS Version (created at Aug 25, 2007)

How To Change The Desktop Wallpaper (created at Aug 25, 2007)

How To Get The Windows Language (created at Aug 25, 2007)

How To Make A Gradient Filled Form (created at Aug 25, 2007)

How can I hide the caption bar on a form? (created at Aug 25, 2007)

How To Send An E-mail By Code (created at Aug 25, 2007)

UPDATES

Creating a Pinterest-Style Card Layout with Bootstrap and Masonry (created at Apr 24, 2024)

Mastering Excel Data Importation in PHP (updated at Apr 24, 2024)

JSON format control in PHP (updated at Apr 24, 2024)

Equal Height Blocks in Bootstrap with JavaScript (created at Apr 22, 2024)

How to convert integer to text string ? (updated at Apr 22, 2024)

Checking similarity between two strings in PHP (updated at Apr 21, 2024)

Create Blob Image in HTML based on the given Text, Width and Height in the Center of the Image without saving file (updated at Apr 21, 2024)

How do I determine the client IP type (IPv4/IPv6) in PHP (updated at Apr 16, 2024)

How do I determine the client IP type in Python - IPv4 or IPv6 (updated at Apr 13, 2024)

Getting Started with PyTorch: A Beginner's Guide to Building Your First Neural Network (updated at Apr 09, 2024)

Predicting Buyer Preferences with PyTorch: A Deep Learning Approach (updated at Apr 09, 2024)

Forecasting the Weather with PyTorch: A Beginner's Guide to Temperature Prediction (created at Apr 09, 2024)

PyTorch example to Forcast Stock Price based on 10 days Dataset (created at Apr 09, 2024)

Mastering Model Persistence: Saving and Loading Trained Machine Learning Models in Python (created at Apr 08, 2024)

Harnessing the Power of Random Forest Algorithm in Python (created at Apr 08, 2024)

Understanding and Implementing K-Nearest Neighbors (KNN) Algorithm in Python (created at Apr 08, 2024)

Forecasting with Linear Regression and KNN Regression in Python (updated at Apr 07, 2024)

What is 302 Found Redirection in HTTP 1.1? (created at Apr 04, 2024)

Mastering Random Forest Regression: A Comprehensive Guide with Python Examples (updated at Apr 01, 2024)

Python Implementation of Linear Regression (updated at Apr 01, 2024)

Mastering Supervised Machine Learning with Python: A Comprehensive Guide (created at Apr 01, 2024)

Mastering AI: A Beginner's Guide to Python Programming and Beyond (created at Apr 01, 2024)

How do I create animated background for Google Meet? (updated at Mar 28, 2024)

Building a Simple DNS Server in Delphi with TTL Support (created at Mar 16, 2024)

How to force cookies, disable php sessid in URL ? (updated at Mar 16, 2024)

Implementing a Versatile DNS Server in PHP: Handling A, AAAA, CNAME, and TXT Records (updated at Mar 16, 2024)

Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records (created at Mar 16, 2024)

Building a Basic DNS Server in PHP/Python: A Beginner's Guide (updated at Mar 15, 2024)

Dynamic DNS Made Easy: Building a Python-Based Solution (created at Mar 15, 2024)

Exploring the Depths of Data Transfer: sendfile vs. kTLS (created at Mar 15, 2024)