본문 바로가기

개발관련/MFC

CFileDialog

in msdn...

explicit CFileDialog(
   BOOL
bOpenFileDialog,
   LPCTSTR lpszDefExt = NULL,
   LPCTSTR lpszFileName = NULL,
   DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
   LPCTSTR lpszFilter = NULL,
   CWnd* pParentWnd = NULL,
   DWORD dwSize = 0
);

Parameters

bOpenFileDialog
Set to TRUE to construct a File Open dialog box or FALSE to construct a File Save As dialog box.
lpszDefExt
The default filename extension. If the user does not include an extension in the Filename edit box, the extension specified by lpszDefExt is automatically appended to the filename. If this parameter is NULL, no file extension is appended.
lpszFileName
The initial filename that appears in the filename edit box. If NULL, no filename initially appears.
dwFlags
A combination of one or more flags that allow you to customize the dialog box. For a description of these flags, see the OPENFILENAME structure in the Platform SDK. If you modify the m_ofn.Flags structure member, use a bitwise-OR operator in your changes to keep the default behavior intact.
lpszFilter
A series of string pairs that specify filters you can apply to the file. If you specify file filters, only selected files will appear in the Files list box. See the Remarks section for more information on how to work with file filters.
pParentWnd
A pointer to the file dialog-box object's parent or owner window.
dwSize
The size of the OPENFILENAME structure. This value is dependent on the operating system version, so MFC can determine the appropriate kind of dialog to create (for example, new Windows 2000 dialogs as opposed to NT4 dialogs).

my source

// read a file
CFile file;
CFileDialog dlg(TRUE,NULL,NULL,OFN_FILEMUSTEXIST, ".txt|*.txt||", NULL);
{
        CHAR psFolder[1024];
        sprintf(psFolder, "%s", AfxGetApp()->GetProfileString("BROWSE", "exec_file"));
        dlg.m_ofn.lpstrInitialDir = psFolder;
}

if(dlg.DoModal() == IDOK){
    file.Open(dlg.GetFileName(),CFile::modeRead | CFile::typeBinary, NULL);
    file.Close();
    {
         CString strPathName = dlg.GetPathName();
         AfxGetApp()->WriteProfileString("BROWSE", "exec_file", strPathName.Mid(0,
         strPathName.ReverseFind('\\')));
    }
}

// write a file
CFileDialog dlg(FALSE, "bin", "iconfile", OFN_FILEMUSTEXIST, ".bin|*.bin||", NULL);
dlg.m_ofn.lpstrTitle = "Save File Shortcut";
{
        CHAR psFolder[1024];
        sprintf(psFolder, "%s", AfxGetApp()->GetProfileString("BROWSE", "save_file"));
        dlg.m_ofn.lpstrInitialDir = psFolder;
}
if (dlg.DoModal() == IDOK) {
        {
              CString strPathName = dlg.GetPathName();
              AfxGetApp()->WriteProfileString("BROWSE", "save_file", strPathName.Mid(0,
              strPathName.ReverseFind('\\')));
        }
}