Due to various applications, we need to call the system's Open File dialog box or open the Folder dialog box, or both. This situation has been resolved, the close-up of this blog post.
1. The common way to open a file dialog box is to use the CFileDialog of the system. Another way is to use the OPENFILENAME structure and getopenfilename () This function, you can implement a single-file or multiple-select files, the code is as follows:
Need to introduce header file # include "CommDlg.h"
[CPP]View Plaincopy
- TCHAR Szbuffer[max_path] = {0};
- OpenFileName ofn= {0};
- Ofn.lstructsize = sizeof (OFN);
- Ofn.hwndowner = m_hwnd;
- Ofn.lpstrfilter = _t ("EXE file (*.exe) \0*.exe\0 All Files (*. *) \0*.*\0"); The file suffix to select
- Ofn.lpstrinitialdir = _t ("D:\\Program Files"); The default file path
- Ofn.lpstrfile = Szbuffer; //buffer for storing files
- Ofn.nmaxfile = sizeof (szbuffer)/sizeof (*szbuffer);
- Ofn.nfilterindex = 0;
- Ofn. Flags = Ofn_pathmustexist | Ofn_filemustexist | Ofn_explorer; //Flag if it is multiple choice to add Ofn_allowmultiselect
- BOOL Bsel = GetOpenFileName (&OFN);
This allows you to open the Select File dialog box. You can select the files you want. Szbuffer is the file path that is stored for the selection.
1.1 Multi-choice
The following code demonstrates how to get the file name that is selected when GetOpenFileName allows multiple selections
#include <windows.h>int WINAPI WinMain (hinstance hinstance,hinstance hprevinstance,lpstr lpcmdline,int NSHOWCMD )
{
OpenFileName ofn;
TCHAR Szopenfilenames[80*max_path];
TCHAR Szpath[max_path];
TCHAR Szfilename[80*max_path];
Tchar* p;
int nlen = 0; ZeroMemory (&ofn, sizeof (OFN));
Ofn. Flags = Ofn_explorer | Ofn_allowmultiselect;
ofn.lstructsize = sizeof (OFN);
Ofn.lpstrfile = Szopenfilenames;
Ofn.nmaxfile = sizeof (szopenfilenames);
Ofn.lpstrfile[0] = '/0 ';
Ofn.lpstrfilter = TEXT ("All Files (*. *)/0*.*/0"); if (GetOpenFileName (&OFN))
{
Copy the first file name to Szpath, i.e.:
If only one file is selected, copy to the last '/'
If more than one file is selected, copy to the first null character
Lstrcpyn (szpath, Szopenfilenames, Ofn.nfileoffset);
When only one file is selected, the following null character is required.
There is no difference in the case of selecting one and more files
szpath[Ofn.nfileoffset] = '/0 ';
Nlen = Lstrlen (szpath);
if (szpath[nlen-1]! = '//')//If multiple files are selected, you must add '//'
{
Lstrcat (szpath, TEXT ("//"));
}
p = szopenfilenames + ofn.nfileoffset; Move the pointer to the first file
ZeroMemory (szFileName, sizeof (szFileName));
while (*P)
{
Lstrcat (szFileName, szpath); Add a path to the file name
Lstrcat (szFileName, p); Plus file name
Lstrcat (szFileName, TEXT ("/n")); Line break
p + = Lstrlen (p) +1; Move to the next file
}
MessageBox (NULL, szFileName, TEXT ("MultiSelect"), MB_OK);
}
}
2. Open the Folder dialog box method:
Header file to be introduced: #include "Shlobj.h"
[CPP]View Plaincopy
- TCHAR Szbuffer[max_path] = {0};
- Browseinfo bi;
- ZeroMemory (&bi,sizeof (browseinfo));
- Bi.hwndowner = NULL;
- Bi.pszdisplayname = Szbuffer;
- Bi.lpsztitle = _t ("Select Folder directory from below:");
- Bi.ulflags = bif_returnfsancestors;
- Lpitemidlist idl = SHBrowseForFolder (&BI);
- if (NULL = = IDL)
- {
- return;
- }
- SHGetPathFromIDList (Idl,szbuffer);
The above code can open a Select Folder dialog box.
3. Select the call to the File or Folder dialog box:
[CPP]View Plaincopy
- TCHAR Szbuffer[max_path] = {0};
- Browseinfo bi;
- ZeroMemory (&bi,sizeof (browseinfo));
- Bi.hwndowner = NULL;
- Bi.pszdisplayname = Szbuffer;
- Bi.lpsztitle = _t ("Select File or folder from below:");
- Bi.ulflags = Bif_browseincludefiles;
- Lpitemidlist idl = SHBrowseForFolder (&BI);
- if (NULL = = IDL)
- {
- return;
- }
- SHGetPathFromIDList (Idl,szbuffer);
Using the code above, you can select a file or folder as needed.
4. Create a Browse Folder dialog box that creates a new folder and specifies that the initial path be selected
Since what I need in real work is a Browse Folder dialog box with the new Folder feature and the initial selected path, consider this requirement as an extension application, because the dialog box style is determined by the ulflags tag, and the dialog box style given in the system header file SHLOBJ.h header file has the following types
Browsing for directory.
#define BIF_RETURNONLYFSDIRS 0x0001//For finding a folder to start document searching
#define Bif_dontgobelowdomain 0x0002//For starting the Find computer
#define Bif_statustext 0x0004
#define Bif_returnfsancestors 0x0008
#define Bif_editbox 0x0010
#define Bif_validate 0x0020//insist on valid result (or CANCEL)
#define Bif_browseforcomputer 0x1000//browsing for Computers.
#define Bif_browseforprinter 0x2000//browsing for Printers
#define Bif_browseincludefiles 0x4000//browsing for everything
Do not meet my needs of the style, through the CSDN found in fact there is a support for the new Folder feature style value 0x40, usually on the network given macros for Bif_newdialogstyle and Bif_usenewui, because do not know in which header file, So we can define these two macros ourselves in the code, as follows
#define Bif_newdialogstyle 0x40
#define BIF_USENEWUI (bif_newdialogstyle| Bif_editbox)
So the first problem solved, then how to let the dialog box has the initial selected folder path, I initially thought through the pidlroot, the result hit rebuff, originally set the initial selected folder path, is through the magic callback function to achieve, In other words, you call SHBrowseForFolder is also like you call the CDialog:: DoModal () function, the specific dialog inside the similar initialization, select the operation of the different implementation of the LPFN this callback function to achieve.
The code for this simple extension is given below
#define Bif_newdialogstyle 0x40
int CALLBACK Browsecallbackproc (HWND hwnd,uint umsg,lparam lparam,lparam lpdata)
{
if (umsg = = bffm_initialized)
{
SendMessage (hwnd, Bffm_setselection, TRUE, lpdata);
}
return 0;
}
void Csisappendmiddlg::onbuttonsisdes ()
{
Todo:add your control notification handler code here
Browseinfo bi;
Char Buffer[max_path];
Initialize the entry parameter bi start
Bi.hwndowner = NULL;
Bi.pidlroot =null;//Initialization of the established root directory is not easy
Bi.pszdisplayname = buffer;//The dialog box cannot be displayed if this parameter is NULL
Bi.lpsztitle = "Select sis target file path";
Bi.ulflags = bif_editbox| Bif_newdialogstyle;
CFileFind Finder;
if (finder. FindFile (m_csisdes) ==false)
{
Bi.lparam = 0;
BI.LPFN = NULL;
}
Else
{
Bi.lparam = (Long) (M_csisdes.getbuffer (M_csisdes.getlength ()));//initialization path, shape (_t ("C:\\symbian"));
BI.LPFN = Browsecallbackproc;
}
Finder. Close ();
Bi.iimage=idr_mainframe;
Initialize the entry parameter bi end
Lpitemidlist pidlist = SHBrowseForFolder (&BI);//Invoke Display selection dialog box
if (pidlist)
{
SHGetPathFromIDList (Pidlist, Buffer);
Get folder path into buffer
M_csisdes = buffer;//Saves the path in a CString object
}
UpdateData (FALSE);
Free memory Used
IMalloc * IMalloc = 0;
if (SUCCEEDED (Shgetmalloc (&imalloc)))
{
Imalloc->free (pidlist);
Imalloc->release ();
}
}
Well, the simple extension is here, and as many of the great gods need to do a further expansion, follow this line of thought. After all, I have now come to this step, hehe.
C + + file (clip) Selection dialog box