giorgiosf

Environment: C++ VisualStudio 2005 under XP

Type of code: MFC with /crl

The Framework function File | Open works normally and opens the standard FileDialog with the new EXPLORE format.

I have another menu option: File | Load Module that is used to select and load an additional data file. I used CFileDialog for this purpose, but the first time I select this menu option the dialog does not show; the second and following times it works normally.

Article ID 131225 (Nov 15, 2006) describes a similar situation (but in reference to Win95) and the recommended solution consists in disabling the flag OFN_EXPLORE before the call dlg.DoModal().

By doing that in my code, the dialog does indeed show at the first trial, but it is an ugly version of the file dialog. I would be glad to use the EXPLORE format. Can anybody help me on this

Incidentally: the documentation reads that the OPENFILENAME structure is in the member m_ofn of CFileDialog. While this structure is still accessible, actually now it is pointed by m_pOFN. Either one of these members have the same effect, however.

Thanks to all.



Re: Visual C++ General CFileDialog

Nishant Sivakumar

Could you show the relevant code snippets please




Re: Visual C++ General CFileDialog

giorgiosf

Here it is. As you can see, I tried several options. By the way - correction: also the ugly style of File Dialog does not pop up the first time.

void CemDoc:SurprisenFileModule()

{

TCHAR szPathName[_MAX_PATH+1]; // = _T("");

CFileDialog dlg(TRUE);

// dlg.m_pOFN->lStructSize = sizeof(OPENFILENAME); //OPENFILENAME_SIZE_VERSION_400; //

// dlg.m_pOFN->Flags =

// OFN_DONTADDTORECENT |

// OFN_FILEMUSTEXIST |

// OFN_LONGNAMES |

// OFN_ENABLEHOOK |

// OFN_EXPLORER;

dlg.m_pOFN->Flags |= (OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST);

dlg.m_pOFN->Flags &= ~OFN_EXPLORER;

dlg.m_pOFN->lpstrFilter = _T("External Modules\0*.exe;*.E80\0\0\0");

dlg.m_pOFN->lpstrCustomFilter = NULL;

dlg.m_pOFN->lpstrFile = szPathName;

dlg.m_pOFN->nMaxFile = _MAX_PATH;

dlg.m_ofn.lpstrTitle = _T("Load External Module");

if(IDOK==dlg.DoModal())

{

// ...

}

}

(sorry, the indentation disappeared)

Regards.





Re: Visual C++ General CFileDialog

Ben Anderson MSFT

Maybe you are trying to add an old style (pre explorer style) hook Try removing just the "OFN_ENABLEHOOK" style. You of course will lose your old style hook.




Re: Visual C++ General CFileDialog

giorgiosf

As you can see, the OFN_ENABLEHOOK is currently disabled in the code I provided to you.

Anyhow:

1. this flag is inserted automatically by the CFileDialog contructor (I read the source code)

2. DoModal asserts if this flag is not used

3. I am not trying to use any special hook - just the default provided by MFC, which works fine with File | Open. My code is in fact almost a replica of the code that the framework uses when it processes OnFileOpen (again, I read the MFC source code)

Thanks.




Re: Visual C++ General CFileDialog

Simple Samples

That KB article is saying that if you have:

existing code which customizes these dialogs with custom templates

then the code will fail as described for Windows 95.

If you are not customizing CFileDialog, then ignore that KB article; it is totally irrelevant. If you are customizing CFileDialog, then you are using the new style to do that and you can ignore that KB article. That KB article is saying that existing code (code that is now many years old) that customized the old-style dialog needs to have a one-line modification in order to work properly. That one-line modification causes the dialog to use the old-style dialog, consistent with the way the old code was designed to work. That KB article is in the July 1999 edition of the MSDN and was probably a few years old then.






Re: Visual C++ General CFileDialog

giorgiosf

Thank you Simple Samples, in fact I had already recognized that the KB article was of no use for me.

Unfortunately your clarification does not provide a solution to my problem, though.

Regards





Re: Visual C++ General CFileDialog

Nishant Sivakumar

What happens if you don't set any of the m_pOFN members at all and just call DoModal That will help ascertain if you are setting something incorrectly.




Re: Visual C++ General CFileDialog

giorgiosf

I fixed it!

Incredible as it may seem, the initialization did not like the setting dlg.m_pOFN->lpstrFile = szPathName;

CFileDialog contains an internal buffer m_szFileName (sized _MAX_PATH as required) that it uses to store the selected file, and my attempt to replace it with my own buffer did not work.

However, if instead of an external TCHAR buffer (as was my original code) you use a CString it works fine:

CString strFile;

....

dlg.m_pOFN->lpstrFile = strFile.GetBuffer(_MAX_PATH);

....

is OK.

I cannot offer an explanation for this strange behaviour, nor for the reason why it failed only the first attempt.

Thanks to all.





Re: Visual C++ General CFileDialog

Nishant Sivakumar

You were presumably passing an uninitialized character array earlier on. That's why it kept failing.





Re: Visual C++ General CFileDialog

Simple Samples

You should copy from szPathName to dlg.m_pOFN->lpstrFile using strcpy.






Re: Visual C++ General CFileDialog

Simple Samples

giorgiosf wrote:

Thank you Simple Samples, in fact I had already recognized that the KB article was of no use for me.

Unfortunately your clarification does not provide a solution to my problem, though.

Regards

I am sorry I could not help more but note that there was not enough information in the original question to have solved the problem. My intent was to say to ignore the KB article and that more information was needed.