Select a folder in the MFC dialog box

Source: Internet
Author: User

Refer to http://www.zhangyun.net /? P = 84 brief introduction to itemidlist

 

There is a project. The basic operation is to select a folder and operate on the files in the folder. The classes and controls that can select folders (directories) are not found in MFC, so you have to use a fixed path in the source code. Every time you change a folder, you must modify the source code and re-compile it. If you don't want to get lazy, you can search the Internet and find a solution.
In the dialog box, add a button. The code for this button is as follows (debug in Win2000 SP4 + vc6.0 ):
Void cgetdirtestdlg: onbrowse ()
{
// Todo: add your control notification handler code here
Cstring STR;
Browseinfo Bi;
Char name [max_path];
Zeromemory (& BI, sizeof (browseinfo); // sets all parameters to 0, that is, null.
Bi. hwndowner = getsafehwnd (); // call the parent window handle of the dialog box.
Bi. pszdisplayname = Name; // used to save the memory address of the user-selected directory string. The default size of the buffer is the defined max_path constant macro.
Bi. lpsztitle = "select folder"; // The displayed Text of the browser folder dialog box is used to display the functions, functions, and objectives of the browser folder dialog box.

// Bi. ulflags = bif_usenewui;
Bi. ulflags = bif_returnfsancestors;
Lpitemidlist IDL = shbrowseforfolder (& BI); // The shbrowseforfolder is used to display a dialog box for users to select folders, the returned value is the path identifier pointer pointing to the selected folder that corresponds to the selected root directory address. // Lpitemidlist contains the ID information of each item
If (IDL = NULL)
Return;
Shgetpathfromidlist (IDL, str. getbuffer (max_path ));
Str. releasebuffer ();

M_strpath = STR; // The cstring variable corresponding to the edit box in the dialog box. Save and display the selected path.
If (Str. getat (Str. getlength ()-1 )! = '//')
M_strpath + = "//";
Updatedata (false); // used to refresh the cedit control corresponding to m_strpath to display the selected folder path
}

**************************************** **************************************** ***********
Msdn knowledge
**************************************** **************************************** ***********
In Visual C ++ (VC), The browseinfo structure contains important information about the selected directory.

(1) browseinfo Structure

● Definition

Typedef struct_browseinfo

{

Hwnd hwndowner;

Lpcitemidlist pidlroot;

Lpstr pszdisplayname;

Lpcstr lpsztitle;

Uint ulflags;

Bffcallback lpfn;

Lparam;

Int iimage;

} Browseinfo, * pbrowseinfo, * lpbrowseinfo;

● Member Variables

Hwndowner: parent form handle in the Browse folder dialog box.

Pidlroot: Address of the itemidlist structure, which contains the initial root directory during browsing. Only the specified directory and Its subdirectories are displayed in the browsing folder dialog box. The member variable can be null, and the desktop directory will be used at this time.

Pszdisplayname: used to save the memory address of the user-selected directory string. The default size of the buffer is the defined max_path constant macro.

Lpsztitle: The text displayed in the Browse folder dialog box. It is used to display the functions, functions, and objectives of the Browse folder dialog box.

Ulflags: this flag describes the options in the dialog box. It can be 0 or any combination of the following constants:

Bif_browseforcomputer: return the computer name. The "OK" button in the dialog box is dimmed unless you select a computer name in the browser.

Bif_browseforprinter: returns the printer name. The "OK" button is dimmed unless a printer name is selected.

Bif_browseincludefiles: the browser displays the Directory and files.

Bif_dontgobelowdomain: In the tree window, the network directory structure under the domain name is not included.

Bif_editbox: the browser dialog box contains an edit box in which you can enter the name of the selected item.

Bif_returnfsancestors: return a node of the file system. The "OK" button can be used only when the selected nodes are meaningful.

Bif_returnonlyfsdirs: returns only the directory of the file system. For example, in the Browse folder dialog box, the "OK" button is available when any directory is selected, when "my computer", "Network Neighbor", and other non-meaningful nodes are selected, the "OK" button is dimmed.

Bif_statustext: the dialog box contains a status area. Send a message to the dialog box to set the status text for the callback function.

Bif_validate: this flag is ignored when the bif_editbox flag is not present. If the name entered in the edit box is invalid, the browser Dialog Box sends the bffm_validatefailed message to the callback function.

Lpfn: Address of the callback function of the browser dialog box defined by the application. When an event occurs in the dialog box, the callback function is called in the dialog box. This parameter can be null.

Lparam: a parameter pointer passed to the callback function in the dialog box.

Iimage: The image associated with the selected directory. This image will be specified as the index value in the system image list.

 

 


Details:
For the shgetpathfromidlist function, the string length and structure length can be modified, resulting in a shortcut error.

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.

Here is the resolution of a shortcut file: [transferred from www.bitscn.com]

 

 

Browseinfo

 

Typedef struct _ browseinfo {

Hwnd hwndowner;

Lpcitemidlist pidlroot;

Lpstr pszdisplayname;

Lpcstr lpsztitle;

Uint ulflags;

Bffcallback lpfn;

Lparam;

Int iimage;

} Browseinfo, * pbrowseinfo, * lpbrowseinfo;

Contains parameters for the shbrowseforfolder function and parameters es Information about the folder selected by the user.

Hwndowner

Handle to the owner window for the dialog box.

Pidlroot

Address of an itemidlist structure specifying the location of the root folder from which to browse. only the specified folder and Its subfolders appear in the dialog box. this member can be null; in that case, the namespace root (the Desktop Folder) is used.

Pszdisplayname

Address of a buffer to receive the display name of the folder selected by the user. The size of this buffer is assumed to be max_path bytes.

Lpsztitle

Address of a null-terminated string that is displayed abve the Tree View Control in the dialog box. This string can be used to specify instructions to the user.

Ulflags

Flags specifying the options for the dialog box. This member can include zero or a combination of the following values:

Bif_browseforcomputer
Only return computers. If the user selects anything other than a computer, the OK button is grayed.
 
Bif_browseforprinter
Only return printers. If the user selects anything other than a printer, the OK button is grayed.
 
Bif_browseincludefiles
The Browse dialog will display files as well as folders.
 
Bif_dontgobelowdomain
Do not include network folders below the domain level in the Tree View control.
 
Bif_editbox
Version 4.71. The Browse dialog events des an edit control in which the user can type the name of an item.
 
Bif_returnfsancestors
Only return File System ancestors. If the user selects anything other than a file system ancestor, the OK button is grayed.
 
Bif_returnonlyfsdirs
Only return File System directories. If the user selects folders that are not part of the file system, the OK button is grayed.
 
Bif_statustext
Include a status area in the dialog box. The callback function can set the status text by sending messages to the dialog box.
 
Bif_validate
Version 4.71. If the user types an invalid name into the edit box, the Browse dipes will call the application's browsecallbackproc with the bffm_validatefailed message. This flag is ignored if bif_editbox is not specified.
 

Lpfn

Address of an application-defined function that the dialog box callwhen an event occurs. For more information, see the browsecallbackproc function. This member can be null.

Lparam

Application-defined value that the dialog box passes to the callback function, if one is specified.

Iimage

Variable to receive the image associated with the selected folder. The image is specified as an index to the system image list.

--------------------------------------------------------------------------------

Shbrowseforfolder

 

Winshellapi lpitemidlist winapi shbrowseforfolder (

Lpbrowseinfo lpbi

);

Displays a dialog box that enables the user to select a shell folder.

· Returns the address of an item identifier list 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.

Lpbi

Address of A browseinfo structure that contains information used to display the dialog box.

The calling application is responsible for freeing the returned item identifier list by using the shell's task allocator.

--------------------------------------------------------------------------------

Itemidlist

 

Typedef struct _ itemidlist {

Shitemid mkid;

} Itemidlist, * lpitemidlist;

Typedef const itemidlist * lpcitemidlist;

Contains a list of item identifiers.

Mkid

List of item identifiers.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/qizilong/archive/2009/06/16/4271981.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.