Have you ever written an application which asks the user to select a directory? Have you ever wanted to provide them a nice, neat way to do that without forcing them to manually type in the path? Well, now you can!! I have created two controls that provide you the bacic functionality to make a dialog like the one above. Yes, this is a screen shot of my demo application that uses the controls I created. It was patterned off of the "Choose Directory" dialog that appears when you select a directory when creating a new project in Microsoft* Visual C++ 5.0. (To see this for yourself, choose "File | New" from the menu, then click on the "..." button to the right of the "Location" edit box.)
The controls I implemented are:
This project is somewhat similar to the one in the article posted by Tom Werner Halvorsrød on January 08 1999, but it has a completely different look and feel and is intended to provide a neat, orderly way for users to be able to select a complete, correct path name. It improves on the listing of logical drives on the system by providing the volume label (if it exists), and it provides a hierarchical view of the path from the root to the current directory. By the addition of a read-only edit box, the entire path is displayed in plain text for easy reading.
All the directions on including these components into your project are also included in the file Readme.txt in the source archive.
First, include the appropriate files for the controls you want to use. CDirectoryTree is implemented in files DirectoryTree.cpp and DirectoryTree.h, and CDriveCombo is implemented in files DriveCombo.cpp and DriveCombo.h. You will also have to include the four file folder bitmaps from my demo project, or you will have to provide four bitmaps of your own design. The bitmaps from this project are named closed.bmp, closedSel.bmp, open.bmp, and openSel.bmp.
Next, create your dialog with the controls you want to use. Using Class Wizard, create member variables for those controls using the appropriate class. If you want to have an edit box or static text to display the path as a readable string, include an edit control on the dialog and create a member variable for it. For example:
CDirectoryTree m_DirTree; CDriveCombo m_DriveList; CEdit m_DirName;
// Set up the image list for the tree control CImageList * pImageList; pImageList = new CImageList(); pImageList->Create(16, 16, ILC_COLOR, 4, 4); // Load the bitmaps. This must be done before calling Initialize() // on the CDirectoryTree object. // NOTE: IDB_FIRST_TREE_ICON = ID of the first bitmap resource, // IDB_LASE_TREE_ICON = ID of the last bitmap resource. // Of course, you can choose any method you want to build this list. CBitmap bBitmap; for (int nID = IDB_FIRST_TREE_ICON; nID <= IDB_LAST_TREE_ICON; nID++) { bBitmap.LoadMappedBitmap(nID); pImageList->Add(&bBitmap, (COLORREF)0x000000); bBitmap.DeleteObject(); } m_DirTree.SetBitmapList(pImageList); // If the bitmaps were loaded in a non-standard order, call this function. // Otherwise, the default order will be used. m_DirTree.SetBitmapOrder(1,3,2,0);
m_DriveList.Initialize(); m_DirTree.Initialize(); m_DirName.SetWindowText(m_DirTree.GetCurrentDir());
To be notified that the user has selected a new drive, use the Class Wizard to map a function to the CBN_SELCHANGE message. You can use all the standard CComboBox functions to retrieve the letter of the drive chosen by the user. When the user selects a new drive from the CDriveCombo control, the new drive letter must be communicated to the CDirectoryTree control and the control must be reinitialized. Call the function SetCurrentDrive(char), passing in the letter of the new drive, to communicate the change to the control. Then, call Initialize() to reinitialize the control. For example:
// Handler for the CBN_SELCHANGE message void CMyDialog::OnSelchangeDriveCombo() { int nSelected = 0; CString szText; // Determine the new drive letter and test to see if it's valid. nSelected = m_DriveList.GetCurSel(); if (!m_DriveList.IsDriveReady(nSelected)) { // The drive wasn't valid!! Revert to the previous one m_DriveList.ResetDrive(m_DirTree.GetCurrentDrive()); return; } // Get the drive letter and communicate it to the tree. m_DriveList.GetLBText(nSelected, szText); m_DirTree.SetCurrentDrive(szText[0]); // Re-initialize the tree to show the new drive's directories. m_DirTree.Initialize(); m_DirName.SetWindowText(m_DirTree.GetCurrentDir()); }
// Implementation of PreTranslateMessage() BOOL CChooseDirDlg::PreTranslateMessage(MSG* pMsg) { if (WM_USER_PATHCHANGED == pMsg->message) { OnPathChanged(); return TRUE; } return CDialog::PreTranslateMessage(pMsg); } // Handler for the user-defined message WM_USER_PATHCHANGED void CChooseDirDlg::OnPathChanged() { m_DirName.SetWindowText(m_DirTree.GetCurrentDir()); }
Download demo executable - 7 KB
Download source - 24 KB
Date Posted: January 25, 1999
Add Comment
INSTANT EXPERTS!Savvy IT Pros report on SupportSource. Take a look inside the largest vault of multi-vendor computing specs ever assembled. Click here for an In-depth Report,. Learn about Free & Open Source software at the Bazaar Conference & Expo!,December 14-16, 1999 in New York City! Click here for more information. For tons of Windows technology resources, check out the Windows Programming directory. Go to Fatbrain for the NEWEST and BEST technical books for IT professionals. Get Linux Utilities for ONLY $34.95 at EarthWeb Direct.