Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Change Dir inside CFileDialog

Change Dir inside CFileDialog

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
28 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L leon de boer

    I don't use MFC but as it shims the Win32 I will explain how you do it in raw Win32. The FILEOPEN dialog in common controls take a structure called OPENFILENAME one of it's fields is lpfnHook which allows you to install your own handler. OPENFILENAMEA (commdlg.h) - Win32 apps | Microsoft Docs[^] In your handler in the WM_NOTIFY message you handle the CDN_TYPECHANGE message CDN_TYPECHANGE notification code (Commdlg.h) - Win32 apps | Microsoft Docs[^] So a minimal handler looks like this

    UINT_PTR CALLBACK OpenHookProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
    {
    switch (Msg)
    {
    case WM_NOTIFY:
    {
    LPOFNOTIFY pnh = (LPOFNOTIFY)lParam;
    if (pnh && (pnh->hdr.code == CDN_TYPECHANGE))
    {
    switch (pnh->lpOFN->nFilterIndex)
    {
    case 0:
    // First extension type selected
    break;
    case 1:
    // Second selection type selected
    break;
    }
    }
    break;
    }
    }
    return (0);
    }

    In vino veritas

    U Offline
    U Offline
    User 401087
    wrote on last edited by
    #14

    I already intercept CDN_TYPECHANGE in my OnTypeChange () ...to change the directory what code I have to write where in your example you put the rem // First extension type selected and // Second selection type selected for list in my CFileDialog a different dir? Thank's

    I Try, but I'm not sure :))

    L 1 Reply Last reply
    0
    • CPalliniC CPallini

      You are right, in time I forgot the behavior of such a dialog. Here you may find an hint: https://www.experts-exchange.com/questions/20090938/Changing-location-in-Open-Save-dialog.html[^].

      U Offline
      U Offline
      User 401087
      wrote on last edited by
      #15

      sorry... in expert-exchange : "You can use the DlgDirList() function (of CWnd class). It fills a list box with a file or directory listing. Only thing you should know is the ID of the list control." and I haven't the ID of the list control :(

      I Try, but I'm not sure :))

      CPalliniC 1 Reply Last reply
      0
      • V Victor Nijegorodov

        Have you already customized your CFileDialog?

        U Offline
        U Offline
        User 401087
        wrote on last edited by
        #16

        not yet :(

        I Try, but I'm not sure :))

        1 Reply Last reply
        0
        • U User 401087

          sorry... in expert-exchange : "You can use the DlgDirList() function (of CWnd class). It fills a list box with a file or directory listing. Only thing you should know is the ID of the list control." and I haven't the ID of the list control :(

          I Try, but I'm not sure :))

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #17

          I think the more promising approach shown in the linked page is writing the address of folder in the edit box and then (programmatically) clicking the OK button (check out the final comments).

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • U User 401087

            I already intercept CDN_TYPECHANGE in my OnTypeChange () ...to change the directory what code I have to write where in your example you put the rem // First extension type selected and // Second selection type selected for list in my CFileDialog a different dir? Thank's

            I Try, but I'm not sure :))

            L Offline
            L Offline
            leon de boer
            wrote on last edited by
            #18

            So just change directory then invalidate the dialog so it refreshes, you did all the hard part :-) SetCurrentDirectory function (winbase.h) - Win32 apps | Microsoft Docs[^] Do remember if you want to go back to the original directory after the dialog closes, save the directory before you call the dialog with GetCurrentDirectory and the set it back when you exit.

            In vino veritas

            U 1 Reply Last reply
            0
            • L leon de boer

              So just change directory then invalidate the dialog so it refreshes, you did all the hard part :-) SetCurrentDirectory function (winbase.h) - Win32 apps | Microsoft Docs[^] Do remember if you want to go back to the original directory after the dialog closes, save the directory before you call the dialog with GetCurrentDirectory and the set it back when you exit.

              In vino veritas

              U Offline
              U Offline
              User 401087
              wrote on last edited by
              #19

              I changed the dir with SetCurrent Directory , I invalidated and I did UpdateData(false) for refresh but nothing happens ...

              I Try, but I'm not sure :))

              V 1 Reply Last reply
              0
              • U User 401087

                I changed the dir with SetCurrent Directory , I invalidated and I did UpdateData(false) for refresh but nothing happens ...

                I Try, but I'm not sure :))

                V Offline
                V Offline
                Victor Nijegorodov
                wrote on last edited by
                #20

                I guess that UpdateData has nothing to do with what you are trying to achieve. Could you post your actual code?

                U 1 Reply Last reply
                0
                • V Victor Nijegorodov

                  I guess that UpdateData has nothing to do with what you are trying to achieve. Could you post your actual code?

                  U Offline
                  U Offline
                  User 401087
                  wrote on last edited by
                  #21

                  void MyClass::OnTypeChange() { if(m_ofn.nFilterIndex==2){ // .doc char szPath [MAX_PATH] = {"c:\\DocFiles"}; SetCurrentDirectory(szPath); } else { // .txt char szPath2 [MAX_PATH] = {"c:\\TxtFiles"}; SetCurrentDirectory(szPath2); } Invalidate(); UpdateData(false); // Here I want refresh the CFileDialog with the new filter and path }

                  I Try, but I'm not sure :))

                  V 2 Replies Last reply
                  0
                  • U User 401087

                    void MyClass::OnTypeChange() { if(m_ofn.nFilterIndex==2){ // .doc char szPath [MAX_PATH] = {"c:\\DocFiles"}; SetCurrentDirectory(szPath); } else { // .txt char szPath2 [MAX_PATH] = {"c:\\TxtFiles"}; SetCurrentDirectory(szPath2); } Invalidate(); UpdateData(false); // Here I want refresh the CFileDialog with the new filter and path }

                    I Try, but I'm not sure :))

                    V Offline
                    V Offline
                    Victor Nijegorodov
                    wrote on last edited by
                    #22

                    No, this code won't work. SetCurrentDirectory just sets the current directory. It cannot change the folder selection within the CFileDialog. You have to find the possibility to change the folder selection within the CFileDialog control(s)!

                    U 1 Reply Last reply
                    0
                    • V Victor Nijegorodov

                      No, this code won't work. SetCurrentDirectory just sets the current directory. It cannot change the folder selection within the CFileDialog. You have to find the possibility to change the folder selection within the CFileDialog control(s)!

                      U Offline
                      U Offline
                      User 401087
                      wrote on last edited by
                      #23

                      ok, and what are these controls? can you send me an example code? Thank you

                      I Try, but I'm not sure :))

                      1 Reply Last reply
                      0
                      • U User 401087

                        void MyClass::OnTypeChange() { if(m_ofn.nFilterIndex==2){ // .doc char szPath [MAX_PATH] = {"c:\\DocFiles"}; SetCurrentDirectory(szPath); } else { // .txt char szPath2 [MAX_PATH] = {"c:\\TxtFiles"}; SetCurrentDirectory(szPath2); } Invalidate(); UpdateData(false); // Here I want refresh the CFileDialog with the new filter and path }

                        I Try, but I'm not sure :))

                        V Offline
                        V Offline
                        Victor Nijegorodov
                        wrote on last edited by
                        #24

                        Try instead to overwrite the m_ofn member lpstrFile to contain the full folder path, either "c:\\DocFiles" or "c:\\TxtFiles".

                        U 1 Reply Last reply
                        0
                        • V Victor Nijegorodov

                          Try instead to overwrite the m_ofn member lpstrFile to contain the full folder path, either "c:\\DocFiles" or "c:\\TxtFiles".

                          U Offline
                          U Offline
                          User 401087
                          wrote on last edited by
                          #25

                          sorry, it doesn't work :(

                          I Try, but I'm not sure :))

                          V 1 Reply Last reply
                          0
                          • U User 401087

                            sorry, it doesn't work :(

                            I Try, but I'm not sure :))

                            V Offline
                            V Offline
                            Victor Nijegorodov
                            wrote on last edited by
                            #26

                            Could you post your implementation?

                            U 2 Replies Last reply
                            0
                            • V Victor Nijegorodov

                              Could you post your implementation?

                              U Offline
                              U Offline
                              User 401087
                              wrote on last edited by
                              #27

                              I try: void MyClass::OnTypeChange() { if(m_ofn.nFilterIndex==2){ // .doc char szPath [MAX_PATH] = {"c:\\DocFiles"}; SetCurrentDirectory(szPath); m_ofn.lpstrFile=szPath; } else { // .txt char szPath2 [MAX_PATH] = {"c:\\TxtFiles"}; SetCurrentDirectory(szPath2); m_ofn.lpstrFile=szPath2; } Invalidate(); UpdateData(false); } and void MyClass::OnTypeChange() { if(m_ofn.nFilterIndex==2){ // .doc char szPath [MAX_PATH] = {"c:\\DocFiles\\file.doc"}; SetCurrentDirectory(szPath); m_ofn.lpstrFile=szPath; } else { // .txt char szPath2 [MAX_PATH] = {"c:\\TxtFiles\\file.txt"}; SetCurrentDirectory(szPath2); m_ofn.lpstrFile=szPath2; } Invalidate(); UpdateData(false); } but don't work. N.B. the file.txt and file.doc are existing files on the disk.

                              I Try, but I'm not sure :))

                              1 Reply Last reply
                              0
                              • V Victor Nijegorodov

                                Could you post your implementation?

                                U Offline
                                U Offline
                                User 401087
                                wrote on last edited by
                                #28

                                I try 2 solution: void MyClass::OnTypeChange() { if(m_ofn.nFilterIndex==2){ // .doc char szPath [MAX_PATH] = {"c:\\DocFiles"}; SetCurrentDirectory(szPath); m_ofn.lpstrFile=szPath; } else { // .txt char szPath2 [MAX_PATH] = {"c:\\TxtFiles"}; SetCurrentDirectory(szPath2); m_ofn.lpstrFile=szPath2; } Invalidate(); UpdateData(false); } and void MyClass::OnTypeChange() { if(m_ofn.nFilterIndex==2){ // .doc char szPath [MAX_PATH] = {"c:\\DocFiles\\file.doc"}; //SetCurrentDirectory(szPath); m_ofn.lpstrFile=szPath; } else { // .txt char szPath2 [MAX_PATH] = {"c:\\TxtFiles\\file.txt"}; //SetCurrentDirectory(szPath2); m_ofn.lpstrFile=szPath2; } Invalidate(); UpdateData(false); } But don't work. N.B.: c:\DocFiles\file.doc and c:\TxtFiles\file.txt are existing files on the disk.

                                I Try, but I'm not sure :))

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups