Home > Windows API > Using DlgDirList, DlgDirListComboBox, CB_DIR, LB_DIR, DlgDirSelect, DlgDirSelectEx, DlgDirSelectComboBox, DlgDirSelectComboBoxEx set of functions

Using DlgDirList, DlgDirListComboBox, CB_DIR, LB_DIR, DlgDirSelect, DlgDirSelectEx, DlgDirSelectComboBox, DlgDirSelectComboBoxEx set of functions

DlgDirList, DlgDirSelect, DlgDirListComboBox, CB_DIR and LB_DIR are some functions and event ids that helps us in making our normal list and combo boxes more powerful by enabling directory and file listing capabilities with just one function call or one event.

Take a look at this screenshot before proceeding, the exe is here(save as exe), easy to implement hence full source not shared.

DlgDirList and DlgDirListComboBox Demo

DlgDirList and DlgDirListComboBox Demo

So here is a list of these API’s. I’ll be taking them one by one, have fun!

  1. DlgDirList
  2. DlgDirListComboBox
  3. LB_DIR (Equivalent to DlgDirList)
  4. CB_DIR (Equivalent to DlgDirListComboBox)
  5. DlgDirSelect
  6. DlgDirSelectEx (More powerful than DlgDirSelect)
  7. DlgDirSelectComboBox
  8. DlgDirSelectComboBoxEx (More power than DlgDirSelectComboBoxEx)

DlgDirList

Here is an example which dumps all the files and folders in C: drive to a listbox.

DlgDirList( _T( "C:" ), 
            IDC_LIST_FILE_LIST,
            IDC_EDIT_DIRPATH,
            DDL_READWRITE | DDL_DIRECTORY );

If you notice the third parameter of DlgDirList looks quite strange isn’t it? It takes an edit control id but actually MSDN tells that we should give a static control id, I was testing to see whether it will work with edit controls too. So why is this parameter given? After you call this function, the given edit/static control will have their text set as “C:”. 😉

DlgDirListComboBox

And here is another one which dumps all the files in C: drive to a combo box.

DlgDirListComboBox( _T( "C:" ),
                    IDC_COMBO_FILE_LIST,
                    IDC_EDIT_DIRPATH,
                    DDL_READWRITE | DDL_DIRECTORY );

Explanation for third parameter is the same as for DlgDirList.

LB_DIR

Similar to DlgDirList. Here is an e.g. on how to fire this event.

SendDlgItemMessage( IDC_LIST_FILE_LIST,
                    LB_DIR,
                    DDL_READWRITE | DDL_HIDDEN | DDL_DIRECTORY | DDL_SYSTEM,
                    reinterpret_cast<lparam>( "C:*.*" ));

Note that we’ve got to specify wild card characters to make this event succeed. DlgDirList and its peers do this by themselves so we needn’t do it. So if we specify *.txt instead of *.* only directories and text files will be listed. 🙂

This also unveals the main power of these API’s. We can write quite useful file system search tools using these API’s. I’ve uploaded a demo, do take a look!

CB_DIR

Similar to LB_DIR, just change the id to combo control id and the event name to CB_DIR.

DlgDirSelect

So what does DlgDirSelect do? Retrieves the current selection from a list box. It assumes that the list box has been filled by the DlgDirList member function and that the selection is a drive letter, a file, or a directory name.

An e.g. :  Lets suppose there is a directory called [Documents and settings] in our list and it’s selected, then DlgDirSelect returns “Documents and Settings”.

DlgDirSelect( szPath, IDC_LIST_FILE_LIST );

Note that DlgDirSelect does not do much of an error checking.

DlgDirSelectEx

Note that if you using functions DlgDirList, DlgDirListComboBox then directory names will be wrapped in square brackets, for e.g. [Program Files].  DlgDirSelectEx removes these square brackets.

Some rules for DlgDirSelectEx

  1. Must be a single selection list box, if not the buffer given does not change.
  2. If selection is a directory then return value is non zero, else zero.
  3. Removes square brackets from directory names making them ready for insertion to any path.

An e.g. : Lets suppose there is a directory called [Documents and settings] in our list and it’s selected, then DlgDirSelectEx returns “Documents and Settings”.

DlgDirSelectEx( GetSafeHwnd(),
                szBuff,
                _MAX_PATH,
                IDC_LIST_FILE_LIST );

DlgDirSelectComboBox

Similar to DlgDirSelect

DlgDirSelectComboBoxEx

Similar to DlgDirSelectEx. Rules also are same.

Advertisements
  1. August 16, 2008 at 8:05 am

    You are welcome James.

  2. James Cowan
    August 16, 2008 at 6:49 am

    Thanks for this. I’ve been struggling badly. Now I’ve
    got something smarter to try.
    My Respects!
    Jim

  3. James Cowan
    August 16, 2008 at 6:47 am

    Thanks for this. I’ve been struggling badly. Now I’ve got something smarter to try.
    My Respects!
    Jim

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: