-
October 31st, 2013, 11:55 AM
#1
[RESOLVED] CFileDialog. Overriding default behavior of selecting the initial directory
There was an "impovement" since Windows 7 in algorithm for selecting the initial directory, which is described here OPENFILENAME structure. Briefly:
Windows 7:
- If lpstrInitialDir has the same value as was passed the first time the application used an Open or Save As dialog box, the path most recently selected by the user is used as the initial directory.
- Otherwise, if lpstrFile contains a path, that path is the initial directory.
- Otherwise, if lpstrInitialDir is not NULL, it specifies the initial directory.
- If lpstrInitialDir is NULL and the current directory contains any files of the specified filter types, the initial directory is the current directory.
- Otherwise, the initial directory is the personal files directory of the current user.
- Otherwise, the initial directory is the Desktop folder.
The problem that this behavior is not what users of my program expect. Another constraint is that I need to use old CFileDialog dialog, not Common File Dialogs. I've tried to use advises described on StackOverflow and on MSDN. This solution by EllisMiller works perfectly:
specifying a full path (including filename) in lpstrFile. The filename of course shows up in the filename box which is annoying. I ended up using a filename of "." and adding a bit of code to clear the filename combobox once the dialog is open.
BUT I can't figure how to clear the filename combobox. I've tried to add hook procedure, enumerate windows and clear text, but this didn't work for me. So, my question is: how can I clear text in the filename combobox of CFileDialog? Thanks in advance for any help!
-
October 31st, 2013, 12:59 PM
#2
Re: CFileDialog. Overriding default behavior of selecting the initial directory
Originally Posted by andryuhat
There was an "impovement" since Windows 7 in algorithm for selecting the initial directory, which is described here OPENFILENAME structure. Briefly:
Windows 7:
- If lpstrInitialDir has the same value as was passed the first time the application used an Open or Save As dialog box, the path most recently selected by the user is used as the initial directory.
- Otherwise, if lpstrFile contains a path, that path is the initial directory.
- Otherwise, if lpstrInitialDir is not NULL, it specifies the initial directory.
- If lpstrInitialDir is NULL and the current directory contains any files of the specified filter types, the initial directory is the current directory.
- Otherwise, the initial directory is the personal files directory of the current user.
- Otherwise, the initial directory is the Desktop folder.
The problem that this behavior is not what users of my program expect. Another constraint is that I need to use old CFileDialog dialog, not Common File Dialogs. I've tried to use advises described on StackOverflow and on MSDN. This solution by EllisMiller works perfectly:
BUT I can't figure how to clear the filename combobox. I've tried to add hook procedure, enumerate windows and clear text, but this didn't work for me. So, my question is: how can I clear text in the filename combobox of CFileDialog? Thanks in advance for any help!
I'd probably look into deriving a class from CFileDialog and overriding whatever behaviors you want to change.
-
October 31st, 2013, 01:23 PM
#3
Re: CFileDialog. Overriding default behavior of selecting the initial directory
Ok, but I already have class derived from CFileDialog and still I can't do what I want. Any examples maybe?..
-
October 31st, 2013, 01:26 PM
#4
Re: CFileDialog. Overriding default behavior of selecting the initial directory
No. I don't know the inner workings of CFileDialog, but that's the approach I'd take. Trace the code when it runs and find out where it's doing something other than you want, and override that.
-
October 31st, 2013, 01:36 PM
#5
Re: CFileDialog. Overriding default behavior of selecting the initial directory
Well.. I've tried to add callback function using member lpfnHook of OPENFILENAME, then enumerate through windows and clear text. It didn't work. Then I've tried to get combobox by ID, using IDs from <Dlgs.h> and set text in OnInitDialog and OnInitDone methods. It didn't work. I've tried to use SetWindowsHookEx and catch message that (that was my assumption) will set text to combobox (because I've assumed that message WM_SETTEXT will be used). It didn't work. So I really need any suggestions, how new text can be set.
-
October 31st, 2013, 01:41 PM
#6
Re: CFileDialog. Overriding default behavior of selecting the initial directory
Step into the CFileDialog code and see where it's setting your initial directory whatever the problem is you're trying to fix. Write your own function in your own class and change the behavior to whatever you want. Without doing it myself, I can't give you anything more specific than that.
-
October 31st, 2013, 02:32 PM
#7
Re: CFileDialog. Overriding default behavior of selecting the initial directory
Originally Posted by andryuhat
Another constraint is that I need to use old CFileDialog dialog, not Common File Dialogs.
What would that mean?
Best regards,
Igor
-
October 31st, 2013, 03:49 PM
#8
Re: CFileDialog. Overriding default behavior of selecting the initial directory
Originally Posted by GCDEF
Step into the CFileDialog code and see where it's setting your initial directory whatever the problem is you're trying to fix. Write your own function in your own class and change the behavior to whatever you want. Without doing it myself, I can't give you anything more specific than that.
Ok, thanks for your help!
Originally Posted by Igor Vartanov
What would that mean?
Since Windows Vista there is new file dialogs called Common file dialogs.
-
November 1st, 2013, 02:47 AM
#9
Re: CFileDialog. Overriding default behavior of selecting the initial directory
Originally Posted by andryuhat
First note that old-style/non-Vista File Open/Save As dialogs are also in the Common Dialogs category, i.e. often used dialog boxes, which can be easily shown by calling specific Windows API functions (e.g. GetOpenFileName and GetSaveFileName).
Click: Open and Save As Dialog Boxes.
-
November 1st, 2013, 04:03 AM
#10
Re: CFileDialog. Overriding default behavior of selecting the initial directory
Anyway, for setting a custom initial directory in an old-style File Dialog, there is not any dancing on the rope necessary.
Just set lpstrInitialDir member of OPENFILENAME structure.
Example:
Code:
BOOL bVistaStyle = FALSE;
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, NULL, this, 0, bVistaStyle);
OPENFILENAME& ofn = dlg.GetOFN();
ofn.lpstrInitialDir = _T("c:\\test\\samples");
if(IDOK == dlg.DoModal())
{
// Enjoy!
}
No sweat, like a walking in the park...
Moreover, for MFC CFileDialog, that works also if Vista-style is set.
[Later Edit]
I've tested under Windows 7, both for Vista and old-style File Open dialog.
It worked well, as expected.
Last edited by ovidiucucu; November 1st, 2013 at 04:26 AM.
-
November 1st, 2013, 04:36 AM
#11
Re: CFileDialog. Overriding default behavior of selecting the initial directory
Originally Posted by ovidiucucu
First note that old-style/non-Vista File Open/Save As dialogs are also in the Common Dialogs category, i.e. often used dialog boxes, which can be easily shown by calling specific Windows API functions (e.g. GetOpenFileName and GetSaveFileName).
Click: Open and Save As Dialog Boxes.
Thanks for reply! Sorry, my mistake, I meant Common Item Dialog as mentioned in the first sentence in that article.
Originally Posted by ovidiucucu
Anyway, for setting a custom initial directory in an old-style File Dialog, there is not any dancing on the rope necessary.
Just set lpstrInitialDir member of OPENFILENAME structure.
Example:
Code:
BOOL bVistaStyle = FALSE;
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, NULL, this, 0, bVistaStyle);
OPENFILENAME& ofn = dlg.GetOFN();
ofn.lpstrInitialDir = _T("c:\\test\\samples");
if(IDOK == dlg.DoModal())
{
// Enjoy!
}
No sweat, like a walking in the park...
Moreover, for MFC CFileDialog, that works also if Vista-style is set.
[Later Edit]
I've tested under Windows 7, both for Vista and old-style File Open dialog.
It worked well, as expected.
Thanks for reply, but this method doesn't work always. You can look at this thread, OPENFILENAME lpstrInitialDir behavior change in Windows 7? How to avoid?. Guys have such problem as I have..
-
November 1st, 2013, 07:07 AM
#12
Re: CFileDialog. Overriding default behavior of selecting the initial directory
Originally Posted by andryuhat
Did you try what I suggested?
-
November 1st, 2013, 08:23 AM
#13
Re: CFileDialog. Overriding default behavior of selecting the initial directory
Thanks to everyone, the problem was in my code! Sorry for troubling!
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
|