How to change folder in customized file dialog in Vista
Hi,
I'm customizing file dialog, and deriveing a class (say XFileDialog) from
CFileDialog. In my dialog, I add a button "Foo" and the expected behavior is
to change current folder to a specified one, say, C:\.
Now in the handler for "Foo"'s click, I use following workflow to change the
folder:
void XFileDialog::OnFooClick()
{
// Save the string in file dialog's edit box
CString fileTitle = GetFileName();
// "C:\" to FileDialog's edit box
GetParent()->SendMessage(CDM_SETCONTROLTEXT, (WPARAM)m_filenameCtrlId,
(LPARAM)_T("C:\"));
// Simulate a "OK" message
GetParent()->SendMessage(WM_COMMAND, MAKELONG(IDOK, 0));
// Restore the edit box's text
GetParent()->SendMessage(CDM_SETCONTROLTEXT, (WPARAM)m_filenameCtrlId,
(LPARAM)(LPCTSTR)fileTitle);
}
This works well in XP, however, in Vista, it does NOT work as expected.
From my investigation, the problem lies in the "Simulate a "OK" message":
GetParent()->SendMessage(WM_COMMAND, MAKELONG(IDOK, 0));
- XP
The dialog can receive CDN_FOLDERCHANGE notification during executing this
statement, which indicates FileDialog has finished changing the folder to
C:\
- Vista
The dialog does NOT receive CDN_FOLDERCHANGE notification during executing
this statement. However, the dialog does receive this notification when
program runs out of the scope OnFooClick(). That's to say, FileDialog
changes the folder after I restore the edit box's text, this causes the
losing of this text.
So, is there a way to change the folder without changing the edit box's
text?
Thanks,
-Siegfried