Download demo project - 17 KbDownload source files - 5 Kb
I recently saw Wilfried Roemer's article "A
Drop-Down-Listbox for Drives" and liked how it looked. It used the shell function SHGetFileInfo to display
the volume labels and the same icons as Explorer does. In my mind, little touches like that, where an app presents
data as Windows itself does, shows care and a good understanding of the OS's features on the part of the programmers.
So, inspired by that article, I developed a similar drive selecter, but this time using a list view control. An
example of what the control can do is shown above:
My drive picker has the following features:
- Displays the same names and icons as in Explorer. (Notice that the control above is showing the custom icons
I set for my C: and D: drives.)
- Configurable to show hard drives, removable drives, CDs, network drives, RAM drives, or any combination.
- Shows small or large icons
- Uses the checkbox style of the list control to allow multiple drives to be selected.
The CDrivePickerListCtrl was written with MSVC 6.0 SP2 on Win 98. I have also tested it in Unicode on NT 4.
Using the drive picker in a dialog
When using the control in a dialog, you follow the normal procedure of associating a member variable of your
dialog class with the list control using ClassWizard. Then in your header file, change CListCtrl to CDrivePickerListCtrl.
After the first call to
DoDataExchange() (normally this is done in
CDialog::OnInitDialog()), you can control the drive picker list via that member variable.
The initialization function you call to populate the list automatically sets up the list view styles, so you don't
have to set them up yourself in the dialog editor.
Setting up the list
There is just one function for initializing the list:
void InitList ( int nIconSize, DWORD dwFlags )
This function fills the list control. nIconSize can be 16 or 32, which will make the list display small or large
icons respecitvely. dwFlags controls what type of drives are shown in the list. The flags are:
DDS_DLIL_HARDDRIVES : hard drives
DDS_DLIL_CDROMS : CD-ROM drives
DDS_DLIL_REMOVABLES : other removable drives (floppies, Zips, etc.)
DDS_DLIL_NETDRIVES : mapped and connected network drives
DDS_DLIL_RAMDRIVES : RAM drives
DDS_DLIL_ALL_REMOVABLE : CD-ROMs and other removable drives
DDS_DLIL_ALL_LOCAL_DRIVES : all drives except network drives
DDS_DLIL_ALL_DRIVES : all drives on the system.
You can also set which drives are checked with the
void SetSelection ( const DWORD dwDrives )<BR>
void SetSelection ( LPCTSTR szDrives )
The first version treats
dwDrives as flags: bit 0 determines the check
state for drive A: (0 meaning unchecked, 1 meaning checked), bit 1 determines drive B:'s state, and so on. The
second function takes a list of drive letters (for example, "ACDX"). The listed drives are checked, while
all others are unchecked.
Retrieving the selected drives
CDrivePickerListCtrl provides three functions for getting the selected drives. The first one returns the number
of drives that are selected.
BYTE GetNumSelectedDrives() const
The other functions are analogous to
SetSelection(); they return the selected
drives in either a DWORD or a string.
void GetSelectedDrives ( DWORD* pdwSelectedDrives ) const<BR>
void GetSelectedDrives ( LPTSTR szSelectedDrives ) const
The first version returns the selected drives packed into a DWORD. The second version returns a string that
contains the letters (always in uppercase) of the selected drives
You may notice that the five types of drives you can show in the list correspond to the return values from
GetDriveType(), which is used internally by the control when enumerating drives. During
my testing on Win 98,
GetDriveType() returned DRIVE_FIXED for RAM
drives, instead of the expected DRIVE_RAMDISK. I do not know if this is an API bug, or a 98 bug, or a
documentation bug; I was unable to find mention of it in Microsoft's knowledge base. For the time being, I have
not taken steps to work around this discrepancy until I try the code out on other versions of Windows.
I have included a sample project above that illustrates how to use CDrivePickerListCtrl. As always, suggestions and bug reports are welcome.