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.
So here is a list of these API’s. I’ll be taking them one by one, have fun!
- LB_DIR (Equivalent to DlgDirList)
- CB_DIR (Equivalent to DlgDirListComboBox)
- DlgDirSelectEx (More powerful than DlgDirSelect)
- DlgDirSelectComboBoxEx (More power than DlgDirSelectComboBoxEx)
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:”. 😉
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.
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!
Similar to LB_DIR, just change the id to combo control id and the event name to CB_DIR.
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.
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
- Must be a single selection list box, if not the buffer given does not change.
- If selection is a directory then return value is non zero, else zero.
- 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 );
Similar to DlgDirSelect
Similar to DlgDirSelectEx. Rules also are same.