Click here to Skip to main content
Click here to Skip to main content

A Drive Picker List Control

, 29 Nov 1999
Rate this:
Please Sign up or sign in to vote.
A control that shows drive names and icons like Explorer
  • Download demo project - 17 Kb
  • Download source files - 5 Kb
  • Sample Image

    <!-- Article Starts -->

    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 )
    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
    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

    Share

    About the Author

    Michael Dunn
    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 Pinmemberjiya198419-Mar-06 7:02 
    GeneralWindows XP PinmemberTracey26-Jun-03 19:53 
    Great article! Works great on 2000, but mapped drives do not show up when running this on XP. Must be an issue with GetLogicalDrives() on XP. Do you have a work around?
     
    Thank you
    GeneralRe: Windows XP PinsitebuilderMichael Dunn28-Jun-03 7:24 
    GeneralRe: Windows XP PinmemberTracey28-Jun-03 9:39 
    QuestionPic? PinmemberSwinefeaster28-Mar-03 12:20 
    GeneralProbs to populate this list control within as seperate dialog PinsussCBock21-Sep-02 4:56 
    GeneralTry 'GetDriveTypeEx()' !!! PinmemberKastellanos Nikos29-Aug-01 3:04 
    QuestionProgress bar in list control ? Pinsussneda23-Sep-00 16:14 
    AnswerRe: Progress bar in list control ? PinsussMichael Dunn24-Sep-00 11:31 
    GeneralRe: Progress bar in list control ? PinsussKiTE13-Oct-02 23:48 
    GeneralHelp me... PinsussG.Sreenivasulu14-Sep-00 17:43 

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

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

    | Advertise | Privacy | Terms of Use | Mobile
    Web04 | 2.8.150129.1 | Last Updated 30 Nov 1999
    Article Copyright 1999 by Michael Dunn
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid