Software & Finance





Visual C++ MFC - Open File Dialog and Save File Dialog





In  MFC, File Open and Save Dialog are easy to implement with the MFC class CFileDialog. The Constructor would take a BOOL value of TRUE for open dialog and FALSE for save dialog.

 

You can filter out the file types with lpstrFilter. Look at the code block given below for opening dialog boxes in two modes.

 

{

    CFileDialog dlg(FALSE);

    dlg.m_ofn.nMaxFile = 511;

    dlg.m_ofn.lpstrFilter="XML Files (*.xml)\0*.xml\0Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0";

    dlg.m_ofn.lpstrTitle="Save XML File As";

 

    CString filename;

 

    if(dlg.DoModal() == IDOK)

    {

        filename = dlg.GetPathName(); // return full path and filename

    }

}

 

 

 

{

    CFileDialog dlg(TRUE);

    dlg.m_ofn.nMaxFile = 511;

    dlg.m_ofn.lpstrFilter="XML Files (*.xml)\0*.xml\0All Files (*.*)\0*.*\0\0";

    dlg.m_ofn.lpstrTitle="Open XML File";

 

    CString filename;

 

    if(dlg.DoModal() == IDOK)

    {

        filename = dlg.GetPathName(); // return full path and filename

    }

}