This experiment is divided into two parts: Part 1: select a directory to select a folder, but the specific files in the folder are not displayed. In MFC programming, we often need to select the location of the file, where the directory of a file is located, and we do not want to manually add it, so we can use the button to pop up and select a folder, I made a pop-up folder and got a small program. Record it. If you forget it one day, you can check it easily. If you do not see it, you may be fooled by saying:
If you want to use the pop-up folder selection dialog box, you have to learn: browseinfo structure and lpitemidlist structure. If you do not understand these two structures, it cannot be implemented, I will not go into detail here when I search for these two architectures on the Baidu Encyclopedia:
The Code is as follows: Void cdbtest8_11_1dlg: onselectfile ()
{
// Todo: add your control notification handler code here
Updatedata (true); // assign the string in the edit box to an associated variable in the edit box.
Char szdir [max_path]; //
Browseinfo Bi;
Itemidlist * pidl;
Bi. hwndowner = This-> m_hwnd; // handle in the pop-up dialog box
Bi. pidlroot = NULL; // The initial root directory during browsing. null indicates the desktop directory.
Bi. pszdisplayname = szdir; // used to store the selected directory address
Bi. lpsztitle = "select a directory"; // The prompt dialog box function
Bi. ulflags = bif_statustext | bif_returnonlyfsdirs; // only the file system directory is returned.
Bi. lpfn = NULL; // callback function address, which can be null
Bi. lparam = 0; // a parameter pointer of the callback function, which can be 0
Bi. iimage = 0; // The image associated with the selected directory
Pidl = shbrowseforfolder (& BI); // call the displayed dialog box
If (pidl = NULL) return;
If (! Shgetpathfromidlist (pidl, szdir) // get the folder path and put it in szdir.
Return;
Else
M_strpathname = szdir; // The selected directory is displayed in the editing box.
Updatedata (false); // refresh the edit box
} Part 2: Display files and display them in the editing box Void cdbtest8_11_1dlg: onselectfile ()
{ Cfiledialog DLG (true, null, null, ofn_hidereadonly | ofn_overwriteprompt, ". DBF | *. DBF | all files (*. *) | *.*");
DWORD maxfile = 2562; // maximum number of characters in the buffer zone
DLG. m_ofn.nmaxfile = maxfile; // buffer size
Char * Pc = new char [maxfile];
DLG. m_ofn.lpstrfile = pc; // receives the selected file
DLG. m_ofn.lpstrfile [0] = NULL; If (DLG. domodal () = idok)
{
Cstring dbdir;
Dbdir = DLG. getpathname (); // obtain the selected file location and display it in the editing box.
M_strpathname = dbdir;
Updatedata (false );
}
Delete PC ;//
} |