CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Jan 2009
    Posts
    399

    File extension setup on CFileDialog

    My intention is to setup an scecific file extension on CFileDialog ... For that, I derived a class from CFileDialog, named CFileDialogExt. I have 5 different file types, like this:
    Code:
    LPTSTR szFilter = _T("Bitmap Files (*.bmp)|*.bmp|Gif Files (*.gif)|*.gif|Jpeg Files (*.jpg)|*.jpg|Png Files (*.png)|*.png|Tiff Files (*.tiff)|*.tiff||");
    and the call is simple:
    Code:
    CFileDialogExt dlg(FALSE, NULL, NULL, OFN_FILEMUSTEXIST, szFilter); // <-- Saving operation
    Ok, in order to catch file type changing, I override CFileDialogExt::OnTypeChange(), just ike that:
    Code:
    void CFileDialogExt::OnTypeChange()
    {
       // TODO: Add your specialized code here and/or call the base class
    
       CFileDialog::OnTypeChange();
    
       CString sExt(GetFileExt());
    
       switch(GetOFN().nFilterIndex)
       {
       case 1:
          sExt.Format(_T("bmp"));
          break;
       case 2:
          sExt.Format(_T("gif"));
          break;
       case 3:
          sExt.Format(_T("jpg"));
          break;
       case 4:
          sExt.Format(_T("png"));
          break;
       case 5:
          sExt.Format(_T("tiff"));
          break;
       }
    
       SetDefExt((LPCSTR)(LPCTSTR)sExt);
    }
    but the SetDefExt does work delayed with one step ... here is the behavior:

    I type "test" as file name. I chose "gif" extension. Nothing happen, the file name is the same.
    I chose "jpg". The file name is changing as "test.gif".
    I chose "png". The file name is changing as "test.jpg".

    Do you know what I mean ? The file name is changing by the previous file extension choose.

    I attach a test project to have a sample of what I wrote here ... Is there a solution to setup the right file name at the user file type choosing ?

    Thank you.

    TestFD.zip

  2. #2
    Join Date
    Mar 2001
    Posts
    2,529

    Re: File extension setup on CFileDialog

    Have you considered that it may just be off by 1?

    Especially since it appears to be always selecting the previous element.

    That is a common issue since arrays start with element 0 but selectors start at 1.

    Try just making your case 1 case 0 and subtract 1 from all other cases.
    Code:
       switch(GetOFN().nFilterIndex)
       {
       case 0:
          sExt.Format(_T("bmp"));
          break;
       case 1:
          sExt.Format(_T("gif"));
          break;
       case 2:
          sExt.Format(_T("jpg"));
          break;
       case 3:
          sExt.Format(_T("png"));
          break;
       case 4:
          sExt.Format(_T("tiff"));
          break;
       }
    Last edited by ahoodin; October 21st, 2015 at 09:34 AM.
    ahoodin
    To keep the plot moving, that's why.

  3. #3
    Join Date
    Jan 2009
    Posts
    399

    Re: File extension setup on CFileDialog

    I do that, the same behavior ...
    TestFD2.zip

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: File extension setup on CFileDialog

    Quote Originally Posted by ahoodin View Post
    [...]
    That is a common issue since arrays start with element 0 but selectors start at 1.
    [...]
    Generally true, but in this case the zero index is used for preserving the filter pattern chosen by the user.
    See nFilterIndex and lpstrCustomFilter members of OPENFILENAME structure.

    @mesajflaviu

    I've tested the OP sample under VS2010 and it worked well.
    Maybe it's a bug in older VS verion which you are using. I don't know.

    Here is the implementation of OnTypeChange event (for Vista style) in VS2010:

    Code:
    STDMETHODIMP CFileDialog::XFileDialogEvents::OnTypeChange(IFileDialog *)
    {
    	METHOD_PROLOGUE(CFileDialog, FileDialogEvents)
    	UINT uIdx = 0;
    	(static_cast<IFileDialog*>(pThis->m_pIFileDialog))->GetFileTypeIndex(&uIdx);
    	pThis->m_ofn.nFilterIndex = static_cast<DWORD>(uIdx);
    	pThis->OnTypeChange();
    	return S_OK;
    }
    Also, here is the VS2010 implementation of CFileDialog::SetDefExt:
    Code:
    void CFileDialog::SetDefExt(LPCSTR lpsz)
    {
    	ASSERT(::IsWindow(m_hWnd));
    	if (m_bVistaStyle == TRUE)
    	{
    		CStringW strExt(lpsz);
    		HRESULT hr = (static_cast<IFileDialog*>(m_pIFileDialog))->SetDefaultExtension(strExt.GetString());
    		ENSURE(SUCCEEDED(hr));
    	}
    	else
    	{
    		ASSERT(m_ofn.Flags & OFN_EXPLORER);
    		GetParent()->SendMessage(CDM_SETDEFEXT, 0, (LPARAM)lpsz);
    	}
    }
    Please have a look in what you have and compare. Is something different or missing there?

    Notes:
    1. CFileDialog with "Vista" style (see bVistaStyle parameter of CFileDialog constructor) doesn't use GetOpenFileName and GetSaveFileName functions but IFileOpenDialog and IFileSaveDialog interfaces. So, this case, do not expect to find all OPENFILENAME members (e.g. lpstrDefExt) like in case of the old "Explorer" style.
    2. CString sExt(GetFileExt()); des not use in this case (see #1).
    3. Never "convert" strings like this: (LPCSTR)(LPCTSTR)strExt! Use ATL/MFC Conversion Classes instead.
      Example:
      Code:
              SetDefExt(CT2A(strExt));
    Last edited by ovidiucucu; October 22nd, 2015 at 03:29 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    Jan 2009
    Posts
    399

    Re: File extension setup on CFileDialog

    I searched on VS2008 source code, and here is what I have founded:
    Code:
    STDMETHODIMP CFileDialog::XFileDialogEvents::OnTypeChange(IFileDialog *)
    {
    	METHOD_PROLOGUE(CFileDialog, FileDialogEvents)
    	UINT uIdx = 0;
    	(static_cast<IFileDialog*>(pThis->m_pIFileDialog))->GetFileTypeIndex(&uIdx);
    	pThis->m_ofn.nFilterIndex = static_cast<DWORD>(uIdx);
    	pThis->OnTypeChange();
    	return S_OK;
    }
    and
    Code:
    void CFileDialog::SetDefExt(LPCSTR lpsz)
    {
    	ASSERT(::IsWindow(m_hWnd));
    	if (m_bVistaStyle == TRUE)
    	{
    		CStringW strExt(lpsz);
    		HRESULT hr = (static_cast<IFileDialog*>(m_pIFileDialog))->SetDefaultExtension(strExt.GetString());
    		ENSURE(SUCCEEDED(hr));
    	}
    	else
    	{
    		ASSERT(m_ofn.Flags & OFN_EXPLORER);
    		GetParent()->SendMessage(CDM_SETDEFEXT, 0, (LPARAM)lpsz);
    	}
    }
    seem to be the same ... and I have tried the following code in CFileDialogExt::OnTypeChange:
    Code:
    void CFileDialogExt::OnTypeChange()
    {
    	// TODO: Add your specialized code here and/or call the base class
    
    	CFileDialog::OnTypeChange();
    
    	CString sExt(GetFileExt());
    	UINT nIndex = 0;
    	IFileDialog* p = GetIFileSaveDialog();
    	p->GetFileTypeIndex(&nIndex);
    	TRACE(">>>%d\n", nIndex);
    
    	switch(nIndex)
    	{
    	case 1:
    		sExt.Format(_T("bmp"));
    		break;
    	case 2:
    		sExt.Format(_T("gif"));
    		break;
    	case 3:
    		sExt.Format(_T("jpg"));
    		break;
    	case 4:
    		sExt.Format(_T("png"));
    		break;
    	case 5:
    		sExt.Format(_T("tiff"));
    		break;
    	}
    
    	CStringW strExt(sExt);
    	p->SetDefaultExtension(strExt);
    	p->Release();
    }
    no change, the same behavior .... I have to dig in ... strange ...
    Last edited by mesajflaviu; October 23rd, 2015 at 10:30 AM.

  6. #6
    Join Date
    Jan 2009
    Posts
    399

    Re: File extension setup on CFileDialog

    I have found a workaround, I typed here, in case that everyone need it:
    Code:
    void CFileDialogExt::OnTypeChange()
    {
    	// TODO: Add your specialized code here and/or call the base class
    
    	CString sFileName(GetFileName());
    	if(sFileName.IsEmpty())
    	{
    		CFileDialog::OnTypeChange();
    		return;
    	}
    
    	CString sExt(GetFileExt());
    
    	switch(GetOFN().nFilterIndex)
    	{
    	case 1:
    		sExt.Format(_T("bmp"));
    		break;
    	case 2:
    		sExt.Format(_T("gif"));
    		break;
    	case 3:
    		sExt.Format(_T("jpg"));
    		break;
    	case 4:
    		sExt.Format(_T("png"));
    		break;
    	case 5:
    		sExt.Format(_T("tiff"));
    		break;
    	}
    
    //	SetDefExt((LPCSTR)(LPCTSTR)sExt);
    
    	int nDotIndex = sFileName.ReverseFind('.');
    	if(-1 != nDotIndex)
    	{
    		int nDiff = sFileName.GetLength() - nDotIndex;
    		if(nDotIndex == (sFileName.GetLength() - nDiff))
    			sFileName = sFileName.Left(sFileName.GetLength() - nDiff);
    	}
    
    	CString sTemp;
    	sTemp.Format(_T("%s.%s"), sFileName, sExt);
    	IFileDialog* pIFD = GetIFileSaveDialog();
    	CStringW sFileNameW(sTemp);
    	pIFD->SetFileName(sFileNameW);
    	pIFD->Release();
    }
    TestFD_final.zip
    Last edited by mesajflaviu; October 30th, 2015 at 04:15 AM.

  7. #7
    Join Date
    Nov 2010
    Posts
    7

    Re: File extension setup on CFileDialog

    I have a simular problem with OnTypeChange:

    In a CDialog is it possible to add a file with a lot of different extensions.
    The complete list of extensions is:
    Des files (*.des)|*.des|
    Gml files (*.gml; *.xml)|*.gml; *.xml|
    Dwg files (*.dwg)|*.dwg|
    Shape files (*.shp)|*.shp|
    Raster Files (*.ras)|*.ras|
    GeoTiff Files (*.tif)|*.tif|
    Ecw Files (*.ecw)|*.ecw|
    BAC Magic Files (*.bac)|*.bac|

    From the registry I get the extension of the last selected file and I start a new CFileDialog with the last extension as first option and the path where the last file with this extension was selected.

    If the user choose an other extension, i would like to change the path where the last file with this extension was selected.

    I think, I have to resolve this in the OnTypeChange()? But I don't have any idea.

    Anybody has?

    Wbr,
    Kees Braaksma
    Netherlands

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: File extension setup on CFileDialog

    Quote Originally Posted by cwbraaksma View Post
    I...
    From the registry I get the extension of the last selected file and I start a new CFileDialog with the last extension as first option and the path where the last file with this extension was selected.

    If the user choose an other extension, i would like to change the path where the last file with this extension was selected.

    I think, I have to resolve this in the OnTypeChange()? But I don't have any idea.

    Anybody has?
    Yes! just try to implement it and test if it works.
    Victor Nijegorodov

  9. #9
    Join Date
    Nov 2010
    Posts
    7

    Re: File extension setup on CFileDialog

    Quote Originally Posted by VictorN View Post
    Yes! just try to implement it and test if it works.
    I treied to options, but both don't work!

    void CSelectDataFile::OnTypeChange()
    {
    CFileDialog::OnTypeChange();
    // simple change of directory
    m_ofn.lpstrInitialDir = "C:\\D\\Klanten";
    // option 1
    NMHDR nmh;
    nmh.hwndFrom = *this;
    nmh.idFrom = GetDlgCtrlID();
    nmh.code = CDN_FOLDERCHANGE;
    SendMessage(WM_NOTIFY, GetDlgCtrlID(), (LPARAM)&nmh);
    // Option 2
    Invalidate();
    RedrawWindow();
    OnInitDialog();
    }

    Any idea to solve? What do i wrong?
    Wbr,
    Kees Braaksma

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: File extension setup on CFileDialog

    1. Define "don't work".
    2. Could you post the test project (in .zip archive excluding Debug, Release, ipch folders?
    Victor Nijegorodov

  11. #11
    Join Date
    Nov 2010
    Posts
    7

    Re: File extension setup on CFileDialog

    Quote Originally Posted by VictorN View Post
    1. Define "don't work".
    2. Could you post the test project (in .zip archive excluding Debug, Release, ipch folders?
    SelectDataFile.zip

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: File extension setup on CFileDialog

    Quote Originally Posted by cwbraaksma View Post
    I asked you to post the test project, to be able to debug/test what your code is doing!
    And not the three incomplete cpp/h files!
    Victor Nijegorodov

  13. #13
    Join Date
    Nov 2010
    Posts
    7

    Re: File extension setup on CFileDialog

    Dear VictorN,

    Sorry for a realy small 'project'. But the code is part of an ocx of 5 MB.
    I can try to create a seperate programm to test it, but that takes time.

    Wbr,
    Kees

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: File extension setup on CFileDialog

    Quote Originally Posted by cwbraaksma View Post
    Dear VictorN,

    Sorry for a realy small 'project'. But the code is part of an ocx of 5 MB.
    I can try to create a seperate programm to test it, but that takes time.

    Wbr,
    Kees
    We need only a very small test project reproducing your problem! Just to test/debug it and help you.
    Nothing more.
    Victor Nijegorodov

  15. #15
    Join Date
    Nov 2010
    Posts
    7

    Re: File extension setup on CFileDialog

    Dear VictorN,

    Herewith a project with a simple dialog with a button to add a file.

    I hope you can help me!!

    Wbr,
    Kees

    AddFile.zip

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured