| TShBrowse | ||||||||||||||||||||
| Delphi component that encapsulates the ShBrowseForFolder API function call (displaying a standard Windows dialog box that enables the user to select a shell folder, network computer, printer or a file). | ||||||||||||||||||||
Article submitted by Alan Lloyd for the Delphi Programming Quickies Contest.
When the TShBrowse object is created, a default caption and user message are defined. ShBrowseForFolder calls a user defined function when it responds to user action. The parameters of this callback function are (hWnd : THandle; uMsg : integer; lParam, lpData : DWord). The user code must respond by doing whatever is necessary and appropriate to the message and message data (in lParam).
Note that the lpData parameter is a value returned by windows of whatever value is specified in the BrowseInfo.lParam element when calling ShBrowseForFolder. (Note that the BrowseInfo.lParam has no relation to the callback lParam. BrowseInfo.lParam is returned as callback lpData).
The callback function is a general function not a method. If we want to use a method (to engage OO conventions) we must know what instance of what object has the callback method. This we do by passing Self (a reference to the current instance) in the BrowseInfo.lParam element. This reference is returned by the callback in lpData and is typecast to a TShBrowse object to direct the callback to the instance method.
![]() To obtain the user folder selection the Execute method is called. This returns false if the user has cancelled the dialog. When the dialog is initialised it callbacks with a BFFM_INITIALIZED message. This is the time to set the position of the dialog box, the captions, and the initial folder. Using the dialog box window handle provided by the call back hWnd parameter, the dialog window size is obtained. SystemParametersInfo allows us to get the desktop size and calculate the dialog box position which we then set. When a user selection of a folder is made (but before clicking the OK button) the callback is called with a BFFM_SELCHANGED message. This allows us to run the FolderCheck function (if provided) to check if the selction is valid and enable th OK button. When the OK button is clicked then the ShBrowseForFolder API returns with a pointer to an Id list (this will be nil if the Cancel button was clicked). We read the list pointed to by calling ShGetPathFromIDList and place this in the Folder property. The selected item icon index is placed in the SelIconIndex property. The memory used by the pointer to the Id list is freed using the shell's IMalloc interface (the shell itself will free the Imalloc interface, we only use that interface to free the memory). If required the UNC form of the selected folder is available via the UNC property. Typical usage
Note: TShBrowse is not _actually_ a component, although one could descend it from TComponent and add a Register procedure. But one might then have to fiddle with ownership. I think I tried it and it choked somewhere. And anyway I usually create on the fly these sort of things which have no visual inheritance. Properties and methodsconstructor CreateInitiates the supporting and wrapping functionality of TshBrowse which descends from Tobject. You could descedn from Tcomponent and add a Register function to enable it to be placed on the Component Pallette function Execute : boolean; Called to display the shell if they are greater than 0. As is usual with object classes they will initiate at with a default value of zero when the object instance is created. propertty Caption : string property Folder : string property FolderCheck : TFolderCheck property InitFolder : string property Left : integer property Options : TShBrowseOptions property SelIconIndex : integer property Top : integer property UNCFolder : string property UserMessage : string Use at willIf you have any questions or comments, please post to the Delphi Programming Forum. |
||||||||||||||||||||

