Select a folder dialog

zhaozj2021-02-16  51

Select a folder dialog

He Zhidan

1, analysis

1. Pop-up dialog allows users to select a suitable folder (or file).

2. Record the folder or file selected by the user in the INI (or registry), position directly to this folder or file.

3, the user can specify the associated window (generally the edit box). If the content is not written to the registry, the initial folder is the content of the edit box. After the user selects, the user's selection is displayed on the associated window.

2, design

2.1, parent window:

If there is a button B on the dialog box, the user clicks B. The folder dialog box will pop up, then the parent window of the folder dialog is A. Otherwise, the user can click on other buttons on A, causing unpredictable errors.

If the user specifies the parent window, the window specified by the user is the parent window.

If the user is associated with the control, the parent window of the control is the parent window.

If there is a main window, you will be a parent window in the main window, otherwise the parent window is NULL.

2.2, INI (or registry) content:

With two member variables, record the section (m_striniappname) and the attribute name (m_strinikeyname). If the attribute name or the section is empty, you do not save the INI or registry.

The attribute name default is empty. If you want to save INI, you must specify the property name.

The default value of the section is "Folder Started Path"

2.3, title

If the user specifies the title, use the title specified by the user.

If the user specifies the attribute name of the registry, the title is "Please select the appropriate directory for% S:",% s represents the attribute name of the INI.

Otherwise the title is "Please select the appropriate directory:"

If you want the title to be empty, you can specify the title as a space.

2.4 Initial path

If the user specifies the initial path, the path specified by the user is displayed.

If the user stores the last selected path in INI, read it from INI.

If the control is associated, the content of this control is the initial path.

Otherwise the initial path is empty.

2.5, the style is currently only two types:

SD_DIR: Only display folder

SD_DIR_FILE: Displays folders and files

2.6, if the folder is too long, the part is displayed.

That is: the drive letter 1 to 6 "." Last level folder or file

3, realize

Enum flag {sd_dir = 1, SD_DIR_FILE = 3};

Class CDIRECTORYDIALOGOGOG

{

PUBLIC:

CDIRECTORYDIALOG ();

CDIRECTORYDIALOG (CSTRING STRINIKEYNAME);

Virtual ~ cdirectorydialog ();

// Pop up a dialog box to choose a folder

CString ShowDirectoryTree (CWND * PBUDDYWND); // The entrance parameter is associated with the window

CString ShowDirectoryTreeTree (CString Strinidir = "); // The entrance parameter is the first value

// Write or read the value in the registry (or INI)

Void SetDefaultDirectory;

CString getDefaultdirectory ();

HWND M_HWNDOWNER; / / Parent window handle

CString M_STRTILE; / / Title

ENUM FLAG M_ULFLAGS;

// sd_dir, the value is 1, only the folder is displayed; SD_DIR_FILE, the value is 3 display files and folders, the default is only displayed in the folder.

/ / Registry or INI file related information, record the last open folder

CString m_striniappname;

CString m_strinikeyname;

Private:

Void init (); // initiated member variables

Static CSTRING GETSHORTDIR (CSTRING STRDIR, long nmaxlength = 45); // Transform the long path name into short-circuit name

Uint getStyle (Enum flag flag); // transform styles to shbrowseforfolder style

CString getDefaultTitle (); // Get the default title

HWnd getDefaultownerhWnd (); // Get the default window

// Tong call function, called Static Int Callback BrowsecallbackProc (HWND HWND, UINT UMSG, LPARAM LPARAM, LPARAM LPDATA);

}

4. Introduction to the system functions used

1. Call the API function, Winshellapi LpitemidList WinApi ShbrowseForfolder

LPBROWSEINFO LPBI);

The meaning of its parameter lpbi member is (refer to the figure below)

HWndowner: Handle of the parent window.

PIDLROOT: root directory.

PSZDisplayName: The folder selected by the user.

LPSZTITLE: Title

Ulflags: style

LPFN: Tune function

LPARAM: Parameters of the callback function.

IIMAGE: The icon for the user selection folder.

Note: The content of the static text box is not system processing.

5, use

5.1 related to the edit box, do not write into the registry

Void ctestsddlg :: Onbrowse ()

{

CDIRECTORYDIALOG DIRDLG;

Dirdlg.showdirectoryTree (getdlgitem (idc_browse_edit);

}

5.2 Write into the Registry

Void ctestsddlg :: Onbrowse1 ()

{

CDIRECTORYDIALOG DIRDLG ("Key1");

Dirdlg.m_hwndowner = getsafehwnd ();

CString strdir = Dirdlg.ShowdirectoryTree ("");

IF (! strdir.isempty ())

AfxMessageBox ("The file you selected is:" strdir);

}

appendix:

head File

http://www.338888.com/vcshare/testsd/directorydialog.h

Source File

http://www.338888.com/vcshare/testsd/directorydialog.cpp

Demonstration Project:

http://www.338888.com/vcshare/testsd/testsd.rar

转载请注明原文地址:https://www.9cbs.com/read-25708.html

New Post(0)