We often need to use the "select folder" dialog box. The corresponding API is very useful, but it is a little troublesome. So I encapsulated it specifically and tried to get it in one step.
The function is encapsulated as follows:
/*************************************** **************************
** Function name: getpath
** Input: None
** Output: cstring strpath
** The strpath is not empty, indicating the folder path selected by the user
** If strpath is empty, the user clicks the "cancel" key to cancel the selection.
** Function Description: The "select folder" dialog box is displayed, allowing users to select folders.
**************************************** ************************/
Cstring getpath ()
{
Cstring strpath = "";
Browseinfo binfo;
Zeromemory (& binfo, sizeof (binfo ));
Binfo. hwndowner = m_hwnd;
Binfo. lpsztitle = _ T ("select the path :");
Binfo. ulflags = bif_returnonlyfsdirs;
Lpitemidlist lpdlist; // The idlist used to save the returned information
Lpdlist = shbrowseforfolder (& binfo); // display the selection dialog box
If (lpdlist! = NULL) // The user presses the OK button.
{
Tchar chpath [255]; // string used to store the path
Shgetpathfromidlist (lpdlist, chpath); // converts the Project ID list to a string.
Strpath = chpath; // convert a tchar string to a cstring.
}
Return strpath;
}
You only need to use the followingCode:
Cstring strpath = getpath ();
Then, strpath is the selected folder path. If you click the cancel key in the dialog box, strpath is a null string ("");