Shbrowseforfolder is a Windows Shell function that can be viewed in a directory. Its prototype is:
Lpitemidlist shbrowseforfolder (lpbrowseinfolpbi );
The parameter lpbi is a pointer to the structure browseinfo. The structure is defined as follows:
Typedef struct _ browseinfo {
Hwnd hwndowner;
Lpcitemidlist pidlroot; // points to an item identifier list pointer and sets the default directory for browsing.
Lptstr pszdisplayname;
Lpctstr lpsztitle; // you can use this variable to indicate the title of the browser dialog box.
Uint ulflags;
Bffcallback lpfn; // callback function address, that is, the window process based on the browser dialog box
Lparam;
Int iimage;
} Browseinfo, * pbrowseinfo, * lpbrowseinfo; The broseinfo prototype shows that it is difficult to set a default directory. You can set bif_validate in ulflags and set a callback function to process the bffm_initialized message (this message indicates that the dialog box is ready for calling ), when processing the message, sendmessage A bffm_setselection message is sent to the window, and the string of the initial directory is passed as the lparam parameter. the following code sets the initial directory to "C:/program filese "......
If (umsg = bffm_initialized)
{
Sendmessage (hwnd, bffm_setselection, true, (lparam) _ T ("C: // Program Files "));
}
... The following is an example in VC ++. NET 2003.
# Include <shlobj. h> // shbrowseforfolder defined in shlobj. h
# Include <tchar. h>
# Include <windows. h> int callback browsecallbackproc (hwnd, uint umsg, lparam, lparam lpdata)
{
If (umsg = bffm_initialized)
{
Sendmessage (hwnd, bffm_setselection,
True, (lparam) _ T ("C: // Program Files "));
}
Return 0;
} Void showshbrowseforfolderdemodlg ()
{
Tchar m_dir [500];
Browseinfo Bi = {0 };
Bi. ulflags = bif_newdialogstyle | bif_returnonlyfsdirs | bif_validate;
Bi. lpsztitle = _ T ("select the location where the computing result file is saved ");
Bi. lpfn = browsecallbackproc; // specify the callback function address.
Itemidlist * pidl = shbrowseforfolder (& BI );
If (shgetpathfromidlist (pidl, m_dir) = true) // The complete path of the selected directory is stored in the m_dir variable.
{
MessageBox (null, m_dir, "sample", 0 );
}
} Int winapi winmain (hinstance, hinstance hprevinstance, lpstr szcmdline, int icmdshow)
{
Showshbrowseforfolderdemodlg ();
Return 0;
}