We often need to use the "Select Folder" dialog, the corresponding API is already very easy, but it is too much trouble, so I have set it out to put it in place.
The function package is as follows: / **************************************************** ********************** Function name: getPath ** input: No ** output: cstring strpath ** StrPath is non-empty, indicating the folder path selected by the user ** StrPath is empty, indicating that the user clicks the "Cancel" button, deselects ** function description: Display the "Select Folder" dialog, let the user choose the folder ************* *********************************************************** /
CString GetPath () {CString strPath = ""; BROWSEINFO bInfo; ZeroMemory (& bInfo, sizeof (bInfo)); bInfo.hwndOwner = m_hWnd; bInfo.lpszTitle = _T ( "Please select the path:"); bInfo.ulFlags = BIF_RETURNONLYFSDIRS; LPITEMIDLIST LPDLIST; // Used to save the idlist lpdlist = shbrowseForfolder (& binfo); // Display Selection dialog if (lpdlist! = Null) // User Press OK button {Tchar ChPath [255]; // Storage path String SHGETPATHFROMIDLIST (LPDLIST, ChPATH); // Transform items to string strpath = chpath; // converts TCHAR type strings to strings of cstring type} return strpath;}
You only need to use the following code when calling: cstring strpath = getPath (); then StrPath is the folder path selected by the user. If the user clicks the cancel key of the dialog, StrPath is an empty string ("");