If, for any reason, you would like to enumerate a large number of files from the Windows shell, as for a backup program, you would like to be able to select these file using a familiar user interface. Here is a solution: use a treeview-like component to effect the enumeration. Here, the ShellTreeView component has been tailored for this task.
In the process of developing a file backup program, I needed a component that would allow users of that program to be able to select the set of files to be backed up from a user interface that would look familiar. The immediate answer to this requirement was a treeview-like component exhibiting check boxes next to each of its nodes and allowing the user to click on these check boxes in order to select the files contained in the associated folders of the file system. Since it appeared useful to save these selections to a file and re-use them at a later time, this capability was also implemented.
This component cannot select individual files in a folder. As soon as a node is checked, all the files that it contains are enumerated and put in the list of selected files.
ShellTreeview ExtendedThe ShellTreeview component that is available in Delphi (from version 6) from the "Samples" page of the component palette mimics the left-hand side of the Windows Explorer. It is proposed as a sample component with its source code (located in the ShellCtrls.pas file of the the Demos directory) but without any documentation (Rudy's Delphi Corner fills this vacuum in part).
In the Windows shell, a folder is a collection of items in the shell's namespace. A folder is analogous to a file system directory, and many folders are, in fact, directories. However, there are also other types of folders, such as remote computers, storage devices, the desktop folder, the Control Panel, the Printers folder, and the Fonts folder. In addition, in the XP and Vista Explorer, .zip and .cab compressed files are shown and treated as folders.
The component is a treeview window that is connected to the Windows shell and displays a hierarchical view of the system's shell folders. In this view, each node is is a one-to-one correspondence with a folder of the shell.. Every node exhibits an icon and a label that bears the name of the folder. By double clicking a node, the user can show or hide a list of the sub-folders. Alternatively, the user can click the [+] or [-] buttons next to the node. This is the standard behaviour of the ShellTreeView component.
This component is the base class of the TGtroCheckShellTreeView component For this component, the following capabilities needed to be added:
- adding check boxes next to each node; and
- generating a list of files generated by clicking on these check boxes.
In the forthcoming sections, I will show how the component is initialized, how check boxes are added next to the nodes and how the component proceeds with the enumeration of files.
The user interfaceThis base class of this component, called TGtroCustomCheckShellTreeView, was derived from the TCustomShellTreeView component whereas the component itself was derived from this base class. This approach is the same that is used for the components in the VCL and it allows the designer to control the visibllity of the methods and properties of the resulting class.
Now, the TShellTreeView class needs some configuration:
- Set the Root property to rfMyComputer such as to provide a view of the folders on the computer;
- Set ObjectTypes to [otFolder] so that only folders are displayed;
- Remove the .zip and .cab folders from the view because the shell considers them as folders and displays them.
This methodology was derived from the article entitled "Using TFilterComboBox with TShellTreeView"
procedure TGtroCustomCheckShellTreeView.AddFolder(Sender: TObject; AFolder: TShellFolder; var CanAdd: Boolean) ; var FileExt: string; begin FileExt:= ExtractFileExt(AFolder.DisplayName) ; CanAdd:= AFolder.IsFolder and (CompareText('.zip', FileExt) <> 0) and (CompareText('.cab', FileExt) <> 0) ; end;
Next, the component needs supplementary capabilities:
- showing check boxes next to each node;
- detecting where the user clicks;
- updating the component once a user has clicked a check box; and
- producing the list of files.