CFileDialog Usage in MFC

Source: Internet
Author: User

Use CFileDialog to select a file, use File::fopen Open file error, using the relative address, and Wang debugging for half a day, how to track also did not find errors, so ...

CFileDialog the use of the File selection dialog: First construct an object and provide the corresponding parameters, the constructor prototype is as follows:

Cfiledialog::cfiledialog (BOOL bopenfiledialog, LPCTSTR lpszDefExt = null, LPCTSTR lpszFileName = null, DWORD dwFlags = of n_hidereadonly | Ofn_overwriteprompt, LPCTSTR lpszfilter = null, cwnd* pParentWnd = null), the parameter has the following meanings:

  • Bopenfiledialog true to display the Open dialog box, or False to display the Save Dialog File dialog box.
  • LPSZDEFEXT Specifies the default file name extension.
  • LPSZFILENAME Specifies the default file name.
  • DwFlags indicates some particular style.
  • Lpszfilter is the most important parameter that indicates the type of file to choose from and the corresponding extension. Parameter formats such as:

    "Chart Files (*.XLC) |*.xlc| Worksheet Files (*.xls) |*.xls| Data Files (*.xlc;*.xls) |*.xlc; *.xls| All Files (*. *) |*.*| | "; File type description and extension between | Delimited, can be used between extensions of the same type of file; Split, between each file type | Delimited, End with | | Specified.

  • pParentWnd is the parent window pointer.

The Create File dialog box can use DoModal (), which can be used to get the user's choice after returning:

  • CString Cfiledialog::getpathname () Gets the full file name, including the directory name and extension such as: C:\test\test1.txt
  • CString Cfiledialog::getfilename () Gets the full file name, including the extension such as: Test1.txt
  • CString Cfiledialog::getextname () Gets the full file extension, such as: txt
  • CString Cfiledialog::getfiletitle () Gets the full file name, excluding the directory name and extension such as: test1
  • POSITION cfiledialog::getstartposition () Gets the first file location for cases where multiple files are selected.
  • CString cfiledialog::getnextpathname (position& Pos) Gets the next file location for cases where multiple files are selected, and returns the current file name at the same time. However, you must have already called position cfiledialog::getstartposition () to get the initial position variable.

[Some caution]

Cases:

CFileDialog GetFile (true,null,null,ofn_filemustexist, "Microsoft Excel (*.xls) |*.xls| All Files (*. *) |*.* ");

CFileDialog GetFile (Open File dialog box (TRUE), extension (null), file name (null), style-file must exist (ofn_filemustexist), view file type-excel file, all files ( Microsoft Excel (*.xls) |*.xls| All Files (*. *) |*.*);

style of macro definition

#define OFN_READONLY 0x00000001

#define OFN_OVERWRITEPROMPT 0x00000002

#define OFN_HIDEREADONLY 0x00000004

#define OFN_NOCHANGEDIR 0x00000008

#define OFN_SHOWHELP 0x00000010

#define Ofn_enablehook 0x00000020

#define Ofn_enabletemplate 0x00000040

#define Ofn_enabletemplatehandle 0x00000080

#define Ofn_novalidate 0x00000100

#define OFN_ALLOWMULTISELECT 0x00000200

#define Ofn_extensiondifferent 0x00000400

#define Ofn_pathmustexist 0x00000800

#define Ofn_filemustexist 0x00001000

#define OFN_CREATEPROMPT 0x00002000

#define Ofn_shareaware 0x00004000

#define Ofn_noreadonlyreturn 0x00008000

#define Ofn_notestfilecreate 0x00010000

#define Ofn_nonetworkbutton 0x00020000

#define Ofn_nolongnames 0x00040000//force no long names for 4.x modules

#if (WINVER >= 0x0400)

#define Ofn_explorer 0x00080000//New Look Commdlg

#define Ofn_nodereferencelinks 0x00100000

#define Ofn_longnames 0x00200000//force long names for 3.x modules

#define OFN_ENABLEINCLUDENOTIFY 0x00400000//Send include message to callback

#define Ofn_enablesizing 0x00800000

#endif

#if (_win32_winnt >= 0x0500)

#define Ofn_dontaddtorecent 0x02000000

#define Ofn_forceshowhidden 0x10000000//Show all files including System and hidden files

#endif//(_win32_winnt >= 0x0500)

It should be noted that after using CFileDialog, the current path of the program is set to the path of the selected file;

So, if the program is useful to the IO access or database access and other local operations, you need to pay attention to your current path with a relative path is not the original program path, remember!

cstring GetFilePath ()
{  

CString M_filepath;

GetModuleFileName (null,m_ Filepath.getbuffersetlength (max_path+1), MAX_PATH);

M_filepath.releasebuffer ();

int m_iposindex;

M_iposindex = m_ Filepath.reversefind (' \ \ ');

M_filepath = M_filepath.left (M _iposindex);

return m_filepath;

}


CFileDialog Usage in MFC

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.