locked
CFileDialog Multiple file types problem RRS feed

  • Question

  • Hi

    I'm using the CFileDialog class to prompt users to save an image. I want to give the user the opportunity to save the file in different formats. For example - BMP, JPEG and PNG. I  have set up the filter corectly but I cannot see a way of entering more than one file extension in the 3rd argument. Is there any way to set the default file extension to be the same as what the user selected from the filter? e.g. if the user saves the image in JPEG format it will automatically append ".jpg" to the filename. I see Microsoft applications such as Paint and Wordpad have this feature and I was wondering how they achieve it.

    Thank you to anybody who can help.


    static char szFilter[] = "Bitmap (*.bmp)|*.bmp|PNG (*.png)|*.png|JPEG (*.jpg)|*.jpg||";
    CFileDialog dlg( FALSE, NULL, NULL, OFN_OVERWRITEPROMPT | OFN_EXTENSIONDIFFERENT, szFilter, this); activeDlg = &dlg; if( dlg.DoModal() == IDOK ){ ...}

     

     

     

    Monday, March 7, 2011 11:21 PM

All replies

  • I  have set up the filter corectly but I cannot see a way of entering more than one file extension in the 3rd argument. Is there any way to set the default file extension to be the same as what the user selected from the filter?

    You did not set any default file extension in the 2nd parameter. If you i.e. create your dialog the followed way with "BMP" as default extension

    CString str;
    static char szFilter[]=...
    CFileDialog dlg( FALSE, _T("BMP"), NULL, OFN_OVERWRITEPROMPT | OFN_EXTENSIONDIFFERENT, szFilter, this);
    if( dlg.DoModal() == IDOK )
    {
    str=dlg.GetFileName();
    str=dlg.GetPathName();
    }
    than a call of GetPathName would return the inserted name with path and active extension. If you change the extension in the dropown filter list than the extension is also changed in the filename and path.
    • Proposed as answer by D.Sreedhar Tuesday, March 8, 2011 6:07 AM
    Tuesday, March 8, 2011 5:58 AM
  • That's exactly how I want it to function. I didn't know you to put a default file extension. Thank you for your help.
    Tuesday, March 8, 2011 4:44 PM