VC does not have a ready-made function to select a folder, but this is often used, how to do?
Automatic hands-on, clothed!
Using SHBrowseForFolder, the code is as follows:
#include
int Selfolder (HWND hparent, CString &strfolder)
{
Strfolder.empty ();
Lpmalloc Lpmalloc;
if (:: Shgetmalloc (&lpmalloc)! = NOERROR) return 0;
Char Szdisplayname[_max_path];
Char Szbuffer[_max_path];
Browseinfo Browseinfo;
Browseinfo.hwndowner = hparent;
Browseinfo.pidlroot = NULL; Set Root at Desktop
Browseinfo.pszdisplayname = szDisplayName;
Browseinfo.lpsztitle = "Select a folder";
Browseinfo.ulflags = bif_returnfsancestors| Bif_returnonlyfsdirs;
BROWSEINFO.LPFN = NULL;
Browseinfo.lparam = 0;
Lpitemidlist lpitemidlist;
if ((Lpitemidlist =:: SHBrowseForFolder (&browseinfo)) = NULL)
{
Get the path of the selected folder from the Item ID list.
if (:: SHGetPathFromIDList (Lpitemidlist, Szbuffer))
{
At this point, Szbuffer contains the path the user chose.
if (Szbuffer[0] ==´\0´) return 0;
We have a path in szbuffer! Return it.
Strfolder = Szbuffer;
return 1;
}
else return 1; Strresult is empty
Lpmalloc->free (lpitemidlist);
Lpmalloc->release ();
}
return 1;
}
Call:
void Cmusic1dlg::onok ()
{
Todo:add Extra Validation here
CString str;
HWND m_hwnd = GetSafeHwnd ();
Selfolder (M_HWND,STR);
M_list. AddString (str);
Cdialog::onok ();
}
//------------------------------------------------------------------------------------------------------
//_________________________________________________________________
Encapsulation of the Select Folder dialog box
We often need to use the "Select Folder" dialog box, the corresponding API has been very useful, but a little cumbersome, so I specifically encapsulated it, and strive to step in place.
The functions are encapsulated as follows:
/*****************************************************************
* * Function Name: GetPath
* * Input: None
* * Output: CString strpath
* * strpath non-empty, indicating the folder path selected by the user
* * strpath is empty, indicating that the user clicked the "Cancel" key, deselect
* * Feature Description: Displays the Select Folder dialog box, which lets the user select a folder
****************************************************************/
CString GetPath ()
{
CString strpath = "";
Browseinfo binfo;
ZeroMemory (&binfo, sizeof (binfo));
Binfo.hwndowner = m_hwnd;
Binfo.lpsztitle = _t ("Please select path:");
Binfo.ulflags = Bif_returnonlyfsdirs;
Lpitemidlist lpdlist; Idlist to save the returned information
Lpdlist = SHBrowseForFolder (&binfo); Show selection dialog box
if (lpdlist! = NULL)//user pressed the OK button
{
TCHAR chpath[255]; The string used to store the path
SHGetPathFromIDList (Lpdlist, Chpath);//convert the Project ID list to a string
strpath = Chpath; Convert a string of type TCHAR to a string of type CString
}
return strpath;
}
You only need to use the following code when calling:
CString strpath = GetPath ();
Strpath the folder path selected for the user. If the user clicks the Cancel key of the dialog, strpath is the empty string ("");
VC How to get the path of a folder