Click here to Skip to main content
15,885,985 members
Articles / Desktop Programming / MFC
Article

A Drive Picker List Control

Rate me:
Please Sign up or sign in to vote.
4.67/5 (14 votes)
29 Nov 19993 min read 137K   1.5K   44   11
A control that shows drive names and icons like Explorer
  • Download demo project - 17 Kb
  • Download source files - 5 Kb
  • Sample Image

    Introduction

    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 SetSelection() functions:

    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

    Notes

    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.

    Demo project

    I have included a sample project above that illustrates how to use CDrivePickerListCtrl. As always, suggestions and bug reports are welcome.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Software Developer (Senior) VMware
    United States United States
    Michael lives in sunny Mountain View, California. He started programming with an Apple //e in 4th grade, graduated from UCLA with a math degree in 1994, and immediately landed a job as a QA engineer at Symantec, working on the Norton AntiVirus team. He pretty much taught himself Windows and MFC programming, and in 1999 he designed and coded a new interface for Norton AntiVirus 2000.
    Mike has been a a developer at Napster and at his own lil' startup, Zabersoft, a development company he co-founded with offices in Los Angeles and Odense, Denmark. Mike is now a senior engineer at VMware.

    He also enjoys his hobbies of playing pinball, bike riding, photography, and Domion on Friday nights (current favorite combo: Village + double Pirate Ship). He would get his own snooker table too if they weren't so darn big! He is also sad that he's forgotten the languages he's studied: French, Mandarin Chinese, and Japanese.

    Mike was a VC MVP from 2005 to 2009.

    Comments and Discussions

     
    Generalinterfacing Pin
    jiya198419-Mar-06 6:02
    jiya198419-Mar-06 6:02 
    GeneralWindows XP Pin
    Tracey26-Jun-03 18:53
    Tracey26-Jun-03 18:53 
    GeneralRe: Windows XP Pin
    Michael Dunn28-Jun-03 6:24
    sitebuilderMichael Dunn28-Jun-03 6:24 
    GeneralRe: Windows XP Pin
    Tracey28-Jun-03 8:39
    Tracey28-Jun-03 8:39 
    QuestionPic? Pin
    Swinefeaster28-Mar-03 11:20
    Swinefeaster28-Mar-03 11:20 
    GeneralProbs to populate this list control within as seperate dialog Pin
    CBock21-Sep-02 3:56
    sussCBock21-Sep-02 3:56 
    GeneralTry 'GetDriveTypeEx()' !!! Pin
    Kastellanos Nikos29-Aug-01 2:04
    Kastellanos Nikos29-Aug-01 2:04 
    QuestionProgress bar in list control ? Pin
    neda23-Sep-00 15:14
    neda23-Sep-00 15:14 
    AnswerRe: Progress bar in list control ? Pin
    Michael Dunn24-Sep-00 10:31
    sitebuilderMichael Dunn24-Sep-00 10:31 
    GeneralRe: Progress bar in list control ? Pin
    kite13-Oct-02 22:48
    kite13-Oct-02 22:48 
    GeneralHelp me... Pin
    gsreenu14-Sep-00 16:43
    gsreenu14-Sep-00 16:43 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.