C + + file (clip) Selection dialog box

Source: Internet
Author: User

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
  1. TCHAR Szbuffer[max_path] = {0};
  2. OpenFileName ofn= {0};
  3. Ofn.lstructsize = sizeof (OFN);
  4. Ofn.hwndowner = m_hwnd;
  5. Ofn.lpstrfilter = _t ("EXE file (*.exe) \0*.exe\0 All Files (*. *) \0*.*\0"); The file suffix to select
  6. Ofn.lpstrinitialdir = _t ("D:\\Program Files"); The default file path
  7. Ofn.lpstrfile = Szbuffer; //buffer for storing files
  8. Ofn.nmaxfile = sizeof (szbuffer)/sizeof (*szbuffer);
  9. Ofn.nfilterindex = 0;
  10. Ofn. Flags = Ofn_pathmustexist | Ofn_filemustexist | Ofn_explorer; //Flag if it is multiple choice to add Ofn_allowmultiselect
  11. 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 (&AMP;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
  1. TCHAR Szbuffer[max_path] = {0};
  2. Browseinfo bi;
  3. ZeroMemory (&bi,sizeof (browseinfo));
  4. Bi.hwndowner = NULL;
  5. Bi.pszdisplayname = Szbuffer;
  6. Bi.lpsztitle = _t ("Select Folder directory from below:");
  7. Bi.ulflags = bif_returnfsancestors;
  8. Lpitemidlist idl = SHBrowseForFolder (&BI);
  9. if (NULL = = IDL)
  10. {
  11. return;
  12. }
  13. 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
  1. TCHAR Szbuffer[max_path] = {0};
  2. Browseinfo bi;
  3. ZeroMemory (&bi,sizeof (browseinfo));
  4. Bi.hwndowner = NULL;
  5. Bi.pszdisplayname = Szbuffer;
  6. Bi.lpsztitle = _t ("Select File or folder from below:");
  7. Bi.ulflags = Bif_browseincludefiles;
  8. Lpitemidlist idl = SHBrowseForFolder (&BI);
  9. if (NULL = = IDL)
  10. {
  11. return;
  12. }
  13. 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

Related Article

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.