【Reprint】Use of CFileDialog

CFileDialog is often used, but it is often forgotten. Now it is summarized and sorted out for future query.

example:

CFileDialog openDlg( TRUE, "Material File(*.material)|*.material", NULL,

       OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT ,

       "Material File(*.material)|*.material||", this);

 

 

The prototype of the CFileDialog constructor is as follows:

CFileDialog::CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL,

        LPCTSTR lpszFileName = NULL ,

       DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT ,

       LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL );

 

The meaning of the parameters is as follows:

bOpenFileDialog          is TRUE to display the open dialog, FALSE to display the save dialog file dialog.

lpszDefExt                   specifies the default file extension.

lpszFileName              specifies the default file name.

dwFlags                       specify some specific styles. ( default OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT )

lpszFilter                     specifies the available file types and corresponding extensions.

pParentWnd                is the parent window pointer.

 

Among them, the extension should follow the following format:

"Chart Files (*.xlc)|*.xlc| Worksheet Files (*.xls)|*.xls| Data Files (*.xlc;*.xls)|*.xlc ; *.xls| All Files (*.*)|*.*||";

The file type description and the extension are separated by | , and the extensions of the same type of files can be separated by | , and each file type is separated by | , and the end is indicated by || .

 

CString CFileDialog::GetPathName( ) to get the complete file name, including directory name and extension such as: c:/test/test1.txt

CString CFileDialog::GetFileName( ) to get the full file name, including the extension such as: test1.txt

CString CFileDialog::GetExtName( ) to get the complete file extension, such as: txt

CString CFileDialog::GetFileTitle ( ) to get the full file name, excluding the directory name and extension such as: test1

POSITION CFileDialog::GetStartPosition( ) gets the first file position for multiple files selected.

CString CFileDialog::GetNextPathName( POSITION& pos ) gets the next file location when multiple files are selected, and returns the current file name. But POSITION CFileDialog::GetStartPosition( ) must have been called to get the original POSITION variable.

 

The styles of dwFlags are:

#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    

 

It should be noted that after using CFileDialog , the current path of the program will be set to the path of the selected file.
Therefore , if the program uses local operations such as IO access or database access ,
you need to pay attention to the relative path of your current path . If it is not the original path of your program , remember !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324871633&siteId=291194637