-
October 21st, 2015, 04:49 AM
#1
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
-
October 21st, 2015, 09:27 AM
#2
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.
-
October 22nd, 2015, 02:49 AM
#3
Re: File extension setup on CFileDialog
I do that, the same behavior ...
TestFD2.zip
-
October 22nd, 2015, 03:27 AM
#4
Re: File extension setup on CFileDialog
Originally Posted by ahoodin
[...]
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:
- 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.
- CString sExt(GetFileExt()); des not use in this case (see #1).
- 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.
-
October 23rd, 2015, 06:22 AM
#5
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.
-
October 28th, 2015, 03:58 AM
#6
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.
-
September 17th, 2018, 08:33 AM
#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
-
September 17th, 2018, 10:03 AM
#8
Re: File extension setup on CFileDialog
Originally Posted by cwbraaksma
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
-
September 17th, 2018, 01:50 PM
#9
Re: File extension setup on CFileDialog
Originally Posted by VictorN
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
-
September 17th, 2018, 02:11 PM
#10
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
-
September 18th, 2018, 01:27 AM
#11
Re: File extension setup on CFileDialog
Originally Posted by VictorN
1. Define "don't work".
2. Could you post the test project (in .zip archive excluding Debug, Release, ipch folders?
SelectDataFile.zip
-
September 18th, 2018, 02:44 AM
#12
-
September 18th, 2018, 02:54 AM
#13
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
-
September 18th, 2018, 03:06 AM
#14
-
September 18th, 2018, 05:44 AM
#15
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|