INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Usefull Functions & Procedures

How to see entire network - similar to GETDIR() by Eguy
Posted: 10 Apr 02

Using the Windows Shell API to Display a GetFolder Dialog
First Published in FoxTalk November 1999 by Andrew Coates

FoxPro has a native function that allows a user to select a folder - GETDIR().
The problem with this function is that it doesn't allow the user to navigate outside the local drives (or those network shares that are mapped to local drive letters. Increasingly, users need to be able to specify a folder regardless of whether they've got it mapped or not. The Windows shell has an appropriate interface for this, for example the treeview in the left pane of the Windows Explorer.

Windows API provides the interface calls you need to make your application look and feel like a true Windows program.

* Program....: GETFOLDER.PRG
* Version....: 1.0
* Author.....: Andrew Coates
* Date.......: September 3, 1999
* Notice.....: Copyright ¬ 1999 Civil Solutions
*   All Rights Reserved.
* Compiler...: Visual FoxPro 06.00.8492.00 for Windows
* Abstract...: Displays a windows SHBrowseForFolder()
*   dialog and returns the path selected by the user
* Receives...: Optional prompt to display at the top
*   of the dialog and optional handle to the calling
*   window
* Returns....: String of path chosen (no trailing \)
* Requires...: struct.vcx (including convert.fll)
* Changes....:

lParameters tcPrompt, tnhWndOwner

* make sure the struct class is available
if ! 'struct' $ lower(set('classlib'))
  set classlib to struct addi
endif

* these #DEFINEs allow you to control what's displayed
* and what's returned. See the Shell API documentation
* for a full description

* For finding a folder to start document searching
#DEFINE BIF_RETURNONLYFSDIRS 0x1
* For starting the Find Computer
#DEFINE BIF_DONTGOBELOWDOMAIN 0x2
#DEFINE BIF_STATUSTEXT 0x4
#DEFINE BIF_RETURNFSANCESTORS 0x8
* Browsing for Computers.
#DEFINE BIF_BROWSEFORCOMPUTER 0x1000
* Browsing for Printers
#DEFINE BIF_BROWSEFORPRINTER 0x2000
* Browsing for Everything
#DEFINE BIF_BROWSEINCLUDEFILES 0x4000

* used to initialise a buffer passed by reference
#DEFINE MAX_PATH 260

* API declarations
* Frees memory allocated to the ID list returned by
* SHBrowseForFolder
Declare CoTaskMemFree in ole32.dll INTEGER hMem

* Displays rthe browse dialog
Declare Integer SHBrowseForFolder in shell32.dll ;
  STRING lpbi

* Decodes the path from the ID number returned from
* SHBrowseForFolder
Declare Integer SHGetPathFromIDList in shell32.dll ;
  INTEGER pidList, STRING @ lpBuffer

* make sure the location of convert.fll is in the path
if ! './FLLS' $ upper(set('path'))
  local lcPath
  lcPath = set('path')
  lcPath = lcPath + iif(empty(lcPath), '', ';') + './flls'
  set path to &lcPath.
endif

* get the parameters into local variables with
* sensible default values
local lnhWndOwner, lcPrompt

if vartype(tnhWndOwner) # 'N'
  lnhWndOwner = 0
else
  lnhWndOwner = tnhWndOwner
endif

if vartype(tcPrompt) # 'C'
  lcPrompt = 'Locate'
else
  lcPrompt = tcPrompt
endif

* declare the local variables used to call the API
local iNull, lpIDList, lResult, sPath, udtBI

iNull = 0

* create a struct-based class (defined below)
local loBrowseInfo
loBrowseInfo = createobject('Browseinfo')

* set the parameters of the structure
With loBrowseInfo
  .hWndOwner = lnhWndOwner
  .lpszTitle = lcPrompt
  .ulFlags = BIF_RETURNONLYFSDIRS
EndWith

* convet the structure to a string
udtBI = loBrowseInfo.GetString()

* call the API
lpIDList = SHBrowseForFolder(udtBI)
 
sPath = ''

* if something was returned
If lpIDList # 0
  * initialise a buffer for the decoded path
  sPath = repl(chr(0), MAX_PATH)
  * decode the path
  lResult = SHGetPathFromIDList(lpIDList, @ sPath)
  * free the memory allocated to the IDList
  =CoTaskMemFree(lpIDList)

  * strip the trailing chr(0)s
  iNull = At(chr(0), sPath)
  If iNull # 0
    sPath = Left(sPath, iNull - 1)
  EndIf

EndIf

* return the decoded path
return sPath


DEFINE CLASS BrowseInfo as Struct

* This is the C Struct definition
*!*  typedef struct _browseinfo {
*!*    HWND hwndOwner;
*!*    LPCITEMIDLIST pidlRoot;
*!*    LPSTR pszDisplayName;
*!*    LPCSTR lpszTitle;
*!*    UINT ulFlags;
*!*    BFFCALLBACK lpfn;
*!*    LPARAM lParam;
*!*    int iImage;
*!*  } BROWSEINFO, *PBROWSEINFO, *LPBROWSEINFO;

hWndOwner = 0
pIDLRoot = 0
pszDisplayName = 0
lpszTitle = ''
ulFlags = BIF_RETURNONLYFSDIRS
lpfnCallback = 0
lParam = 0
iImage = 0
cMembers = "HWND l:hWndOwner, " + ;
  "LPCITEMIDLIST l:pIDLRoot, " + ;
  "LPSTR l:pszDisplayName, " + ;
  "LPCSTR pz:lpszTitle, " + ;
  "UINT ul:ulFlags, " + ;
  "BFFCALLBACK l:lpfnCallback, " + ;
  "LPARAM l:lParam, " + ;
  "int l:iImage"

ENDDEFINE

Hope this helps!
Ed



Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close