CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Jun 2012
    Posts
    6

    [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:

    1. 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.
    2. Otherwise, if lpstrFile contains a path, that path is the initial directory.
    3. Otherwise, if lpstrInitialDir is not NULL, it specifies the initial directory.
    4. If lpstrInitialDir is NULL and the current directory contains any files of the specified filter types, the initial directory is the current directory.
    5. Otherwise, the initial directory is the personal files directory of the current user.
    6. 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!

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: CFileDialog. Overriding default behavior of selecting the initial directory

    Quote Originally Posted by andryuhat View Post
    There was an "impovement" since Windows 7 in algorithm for selecting the initial directory, which is described here OPENFILENAME structure. Briefly:

    Windows 7:

    1. 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.
    2. Otherwise, if lpstrFile contains a path, that path is the initial directory.
    3. Otherwise, if lpstrInitialDir is not NULL, it specifies the initial directory.
    4. If lpstrInitialDir is NULL and the current directory contains any files of the specified filter types, the initial directory is the current directory.
    5. Otherwise, the initial directory is the personal files directory of the current user.
    6. 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.

  3. #3
    Join Date
    Jun 2012
    Posts
    6

    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?..

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  5. #5
    Join Date
    Jun 2012
    Posts
    6

    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.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: CFileDialog. Overriding default behavior of selecting the initial directory

    Quote Originally Posted by andryuhat View Post
    Another constraint is that I need to use old CFileDialog dialog, not Common File Dialogs.
    What would that mean?
    Best regards,
    Igor

  8. #8
    Join Date
    Jun 2012
    Posts
    6

    Re: CFileDialog. Overriding default behavior of selecting the initial directory

    Quote Originally Posted by GCDEF View Post
    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!
    Quote Originally Posted by Igor Vartanov View Post
    What would that mean?
    Since Windows Vista there is new file dialogs called Common file dialogs.

  9. #9
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: CFileDialog. Overriding default behavior of selecting the initial directory

    Quote Originally Posted by andryuhat View Post
    [...]
    Since Windows Vista there is new file dialogs called Common file dialogs.
    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.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  10. #10
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    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.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  11. #11
    Join Date
    Jun 2012
    Posts
    6

    Re: CFileDialog. Overriding default behavior of selecting the initial directory

    Quote Originally Posted by ovidiucucu View Post
    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.

    Quote Originally Posted by ovidiucucu View Post
    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..

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: CFileDialog. Overriding default behavior of selecting the initial directory

    Quote Originally Posted by andryuhat View Post
    Thanks for reply! Sorry, my mistake, I meant Common Item Dialog as mentioned in the first sentence in that article.



    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..
    Did you try what I suggested?

  13. #13
    Join Date
    Jun 2012
    Posts
    6

    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
  •  





Click Here to Expand Forum to Full Width

Featured