In general opendialog, the folder name is obtained. If you want to achieve the following effect and obtain the selected path, shbrowseforfolder will be used in this case.
In the following example, a path is returned. If no path is selected, "" is returned. If a path is selected, the selected path is returned.
Char * getpath (hwnd, char * pbuffer)
{
Browseinfo BF;
Lpitemidlist lpitem;
Memset (& BF, 0, sizeof browseinfo );
BF. hwndowner = hwnd;
BF. lpsztitle = "select path ";
BF. ulflags = bif_returnonlyfsdirs; // You can select attributes as needed.
Lpitem = shbrowseforfolder (& BF );
If (lpitem = NULL) // if no path is selected, 0 is returned.
Return "";
// If the path is selected, copy the path and return the path length.
Shgetpathfromidlist (lpitem, pbuffer );
Return pbuffer;
}
The following describes the meanings of some values used by this function.
1. browseinfo
It is a structure, prototype is
Typedef struct _ browseinfo {
Hwnd hwndowner; // the handle of the parent form of the pop-up dialog
Lpcitemidlist pidlroot; // pointer to an itemidlist. We will introduce the itemidlist structure later, which can be empty.
Lpstr pszdisplayname; // point to a buffer, which is used to store the user's selected directory. The buffer size is max_path.
Lpcstr lpsztitle; // point to a non-empty string to display the indication information on the tree directory.
Uint ulflags; // specifies the type of the displayed folder.
Bffcallback lpfn; // simply record it as null
Lparam; // when lpfn is not empty, pass the dialogbox value to the callback function lpfn.
Int iimage; // index of the System icon list. This index is obtained when you select a directory.
} Browseinfo, * pbrowseinfo, * lpbrowseinfo;
The possible values of ulflags are:
Bif_browseforcomputer -- only "my computer" is returned. When a directory other than "my computer" is selected, the OK key is dimmed.
Bif_browseforprinter -- only "Printer" is returned. When a directory other than "Printer" is selected, the OK key is dimmed.
Bif_dontgobelowdomain -- does not include "Network neighbors"
Bif_returnfsancestors -- only "my files" are returned. When a directory other than "my files" is selected, the OK key is dimmed.
Bif_returnonlyfsdirs -- same as above
Bif_statustext -- except des a status area in the dialog box. The callback can set the status text by sending messages to the dialog box.
2. itemidlist
Is a structure that specifies the location of the default root folder to be browsed. It can be blank. In this case, the file directory of the Desktop Folder is used by default.
Prototype:
Typedef struct _ itemidlist
{
Shitemid mkid; // list of item identifers
} Itemidlist, * lpitemidlist;
Typedef const itemidlist * lpcitemidlist;
3. shgetpathfromidlist Function
Prototype is
Winshellapi bool winapi shgetpathfromidlist (
Lpcitemidlist pidl,
Lpstr pszpath
);
Remember to use it. Haha ~~
If it is used in the BCB environment, add the header file # I nclude <shellapi. h>
Before the corresponding. cpp include # define no_win32_lean_and_mean
Another explanation:
Shbrowseforfolder
Lpitemidlist dir2pidl (ansistring & Str) { Hresult hres; Ishelllink * PSL; Itemidlist * pidl; Ipersistfile * PPF; // Use the following function to initialize the com Library first, // Set the current thread to single-thread apartment (STA )), // So that the COM library function can be used later Coinitialize (null );
// Obtain the pointer of the ishelllink interface and place it in PSL Hres = cocreateinstance ( Clsid_shelllink, null, clsctx_inproc_server, Iid_ishelllink, (lpvoid *) & PSL ); If (succeeded (hres )) { // Set the target file Psl-> setpath (Str. c_str ()); /// Obtain the itemidlist of the target file Psl-> getidlist (& pidl ); // Obtain the ipersistfile interface pointer and place it in PPF Hres = PSL-> QueryInterface (iid_ipersistfile, (void **) & PPF ); If (succeeded (hres )) { Wchar wsz [max_path]; // <=========== The following three lines of code complete String Conversion to Unicode # Ifdef _ Unicode Wcscpy (wsz, STR ); # Else // convert string to Unicode Multibytetowidechar (cp_acp, 0, str. c_str (),-1, wsz, max_path ); # Endif // Load the shortcut. Hres = PPF-> load (wsz, stgm_read ); If (succeeded (hres )) { /// Obtain the itemidlist of shortcuts Psl-> getidlist (& pidl ); } PPF-> release (); } Psl-> release (); } Couninitialize (); Return pidl; } Short int _ fastcall selectdir (handle HDL, ansistring & S) { Lpmalloc; If (: shgetmalloc (& lpmalloc )! = Noerror) return 0; Char szdisplayname [_ max_path]; Char szbuffer [_ max_path]; Browseinfo; Browseinfo. hwndowner = (void *) (HDL ); Browseinfo. pidlroot = NULL; // set root at Desktop Browseinfo. pszdisplayname = S. c_str (); // szdisplayname; Browseinfo. lpsztitle = "select the installation path "; Browseinfo. ulflags = bif_returnfsancestors | bif_returnonlyfsdirs; Browseinfo. lpfn = NULL; Browseinfo. lparam = 0; 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 ') { Lpmalloc-> free (lpitemidlist); lpmalloc-> release (); Return 0; } // We have a path in szbuffer! Return it. S = szbuffer; Lpmalloc-> free (lpitemidlist); lpmalloc-> release (); Return 1; } Else { S = szbuffer; Lpmalloc-> free (lpitemidlist); lpmalloc-> release (); Return 1; } } Lpmalloc-> free (lpitemidlist); lpmalloc-> release (); Return 1; } Void _ fastcall tform1: button3click (tobject * sender) { Ansistring S; S = edit1-> hint; Selectdir (this-> handle, S ); If (TRIM (s )! = "") { If (s [S. Length ()] = '//') Edit1-> hint = s; Else Edit1-> hint = S + "//"; Edit1-> text = edit1-> hint + "nntech "; }
|
Summary:
A dialog box for selecting folders is displayed.
Syntax
Pidlist_absolute shbrowseforfolder (
Lpbrowseinfo lpbi
);
Parameters
Lpbi
[In] a pointer to the browseinfo structure, which contains information displayed in the folder selection dialog box.
Return Value
Returns a pointer to an item identifier list (pidl) that specifies the location of the selected folder relative to the root of the namespace. if the user chooses the cancel button in the dialog box, the return value is null.
It is possible that the pidl returned is that of a folder before cut rather than a folder. For a full discussion of this case, see the remarks section.
============================ My source code ============ ========================================================== ====
Cstring cdlglocalconfig: getdirectorypath ()
{
Lpitemidlist pidlroot = NULL;
Shgetspecialfolderlocation (m_hwnd, csidl_drives, & pidlroot );
Browseinfo Bi; // required parameter. The following is the initialization of this structure parameter.
Cstring strdisplayname; // used to obtain the path of your binder, which is equivalent to providing a buffer zone.
Bi. hwndowner = getsafehwnd (); // obtain the handle value of the parent window.
Bi. pidlroot = pidlroot; // This variable is obtained above.
Bi. pszdisplayname = strdisplayname. getbuffer (max_path + 1); // obtain the buffer pointer.
Char szlan [32] = {0 };
G_stringlantype (szlan, "folder", "directory ");
Bi. lpsztitle = szlan; // set the title
Bi. ulflags = bif_returnonlyfsdirs; // set the flag
Bi. lpfn = NULL;
Bi. lparam = 0;
Bi. iimage = 0; // The preceding settings are irrelevant parameters. It is best to set them,
Lpitemidlist lpidlist = shbrowseforfolder (& BI); // open the dialog box
Strdisplayname. releasebuffer (); // corresponds to the above getbuffer ()
Char ppath [max_path];
Cstring STR;
If (lpidlist)
{
Shgetpathfromidlist (lpidlist, ppath );
STR = ppath;
}
Return STR;
}
Shgetpathfromidlist
The function is to convert the project flag list to the file system path:
Bool shgetpathfromidlist (
Lpcitemidlist pidl,
Lpstr pszpath
);
Parameters:
Pidl --- specifies the address (desktop) of a project identifier table in a file or directory location relative to the root of the namespace );
Pszpath --- buffer address of the path of the received file system. The size must be at least the length of max_path characters.