1 Attachment(s)
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.
Attachment 33905
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;
}
1 Attachment(s)
Re: File extension setup on CFileDialog
I do that, the same behavior ... :(
Attachment 33907
Re: File extension setup on CFileDialog
Quote:
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));
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 ...
1 Attachment(s)
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();
}
Attachment 33917
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
Re: File extension setup on CFileDialog
Quote:
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.
Re: File extension setup on CFileDialog
Quote:
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
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?
1 Attachment(s)
Re: File extension setup on CFileDialog
Quote:
Originally Posted by
VictorN
1. Define "don't work".
2. Could you post the test project (in .zip archive excluding Debug, Release, ipch folders?
Attachment 35317
Re: File extension setup on CFileDialog
Quote:
Originally Posted by
cwbraaksma
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! :mad:
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
Re: File extension setup on CFileDialog
Quote:
Originally Posted by
cwbraaksma
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. :wave:
Nothing more. :cool:
1 Attachment(s)
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
Attachment 35319
Re: File extension setup on CFileDialog
OK!
now, please, define what "don't work" and describe the steps to reproduce such a "not working" behavior.
Re: File extension setup on CFileDialog
Hello VictorN,
This are the steps:
Change the directory in csNewDir in OnTypeChange() into an other existing directory on your system.
compile the project
run the programm
press the 'Add File' button
change the extension in the left bottom combobox for the file you want to find for eg shp or dwg
and then goes wrong. I want that he change the directory in the upper right combobox to the directory that is defined in the OnTypeChange() methode.
With best regards,
Kees