Search results for 'BrowseDialog'. 1 post(s) found.

  1. 2007/09/10 Browse for Computers, Folders, Files and Printers
2007/09/10 12:02

Browse for Computers, Folders, Files and Printers


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:


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;


Trackback 7 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject Meridia dosage.

    Tracked from Meridia weight loss meds. 2009/01/16 13:45 delete

    Meridia meridia best prices on the net. Meridia without prescriptions. Discount meridia.

  2. Subject Viagra effects.

    Tracked from Herbal viagra. 2009/03/07 11:09 delete

    Viagra. Buy viagra online. Viagra online. Cheap viagra. Viagra description. Viagra london. Viagra sale. Discount viagra.

  3. Subject Buy percocet online.

    Tracked from Percocet. 2009/03/17 10:00 delete

    Compare brand name and generic percocet. Buy percocet online. Generic percocet. Percocet. Long term percocet withdrawal symptoms. Percocet dangers of abuse. How do i get off percocet.

  4. Subject different money making ideas

    Tracked from moneyideas 2010/01/25 08:51 delete

    moneyideas

  5. Subject different money making ideas

    Tracked from moneyideas 2010/01/28 22:10 delete

    moneyideas

  6. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 06:33 delete

    moneyideas

  7. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:38 delete

    moneyideas