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. A Browse Button

A Browse Button

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
6 Posts 3 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.
  • H Offline
    H Offline
    Hosam Aly Mahmoud
    wrote on last edited by
    #1

    I have made a program using MFC wizard to make a dialog box. I need to add a "Browse" button through which the user can select one file or folder, and get the filename and path (relative to the program's folder, assuming that the file or folder is in it, or the full path if outside the program's folder). I then want to put that name and path into an edit box in 8.3 filename format. Is there a simple way to do such a thing? Thank you very much for your time and effort.

    Hosam Aly Mahmoud

    V M 2 Replies Last reply
    0
    • H Hosam Aly Mahmoud

      I have made a program using MFC wizard to make a dialog box. I need to add a "Browse" button through which the user can select one file or folder, and get the filename and path (relative to the program's folder, assuming that the file or folder is in it, or the full path if outside the program's folder). I then want to put that name and path into an edit box in 8.3 filename format. Is there a simple way to do such a thing? Thank you very much for your time and effort.

      Hosam Aly Mahmoud

      V Offline
      V Offline
      valikac
      wrote on last edited by
      #2

      Yes. One solution is CFileDialog. Kuphryn

      1 Reply Last reply
      0
      • H Hosam Aly Mahmoud

        I have made a program using MFC wizard to make a dialog box. I need to add a "Browse" button through which the user can select one file or folder, and get the filename and path (relative to the program's folder, assuming that the file or folder is in it, or the full path if outside the program's folder). I then want to put that name and path into an edit box in 8.3 filename format. Is there a simple way to do such a thing? Thank you very much for your time and effort.

        Hosam Aly Mahmoud

        M Offline
        M Offline
        MAAK
        wrote on last edited by
        #3

        I think you should take a look at the CFileDialog class in the MSDN for using the "open file" common dialog. CFileDialog The paths retreived from the CFileDialogs are all absolute path, so you need to change it to relative paths yourself. I did not try to get folder paths using this common dialog but you may use the "Browse For Folder" dialog that can be used through the SHBrowseForFolder() shell function. Again you will find it in the MSDN. SHBrowseForFolder() This function could be a little complicated to use but I think there are some wrapper classes on code project for it. For the last part cocerning the 8.3 paths there is the API function GetShortPathName() for doing this and here is its parameters. DWORD GetShortPathName( LPCTSTR lpszLongPath, // null-terminated path string LPTSTR lpszShortPath, // short form buffer DWORD cchBuffer // size of short form buffer );

        H 1 Reply Last reply
        0
        • M MAAK

          I think you should take a look at the CFileDialog class in the MSDN for using the "open file" common dialog. CFileDialog The paths retreived from the CFileDialogs are all absolute path, so you need to change it to relative paths yourself. I did not try to get folder paths using this common dialog but you may use the "Browse For Folder" dialog that can be used through the SHBrowseForFolder() shell function. Again you will find it in the MSDN. SHBrowseForFolder() This function could be a little complicated to use but I think there are some wrapper classes on code project for it. For the last part cocerning the 8.3 paths there is the API function GetShortPathName() for doing this and here is its parameters. DWORD GetShortPathName( LPCTSTR lpszLongPath, // null-terminated path string LPTSTR lpszShortPath, // short form buffer DWORD cchBuffer // size of short form buffer );

          H Offline
          H Offline
          Hosam Aly Mahmoud
          wrote on last edited by
          #4

          Thank you very much for your help. 1. I couldn't understand how the shell function SHBrowseForFolder() works. Could you please tell me either how to use it or how to avoid it? 2. Is there a way to find the path of the program? This way I could remove it from the path to get a relative one. 3. How can I use an API function inside my MFC application? Thank you again.

          Hosam Aly Mahmoud

          M 1 Reply Last reply
          0
          • H Hosam Aly Mahmoud

            Thank you very much for your help. 1. I couldn't understand how the shell function SHBrowseForFolder() works. Could you please tell me either how to use it or how to avoid it? 2. Is there a way to find the path of the program? This way I could remove it from the path to get a relative one. 3. How can I use an API function inside my MFC application? Thank you again.

            Hosam Aly Mahmoud

            M Offline
            M Offline
            MAAK
            wrote on last edited by
            #5

            1. As i told you in my post you could find a wrapper class for the SHBrowseForFolder() on code project this is its link http://www.codeproject.com/shell/cxsbrowsefolder.asp 2. The best way to get the program path exactly is to use the function GetModuleFileName() this is its declaration: DWORD GetModuleFileName( HMODULE hModule, // handle to module LPTSTR lpFilename, // path buffer DWORD nSize // size of buffer ); This function gives you the whole path to your program including the file name. Just remove the file name and you get theabsolute path of the program. This is an example of how to use it to get the program path:

            TCHAR procPath[MAX_PATH];
            GetModuleFileName(hInst, procPath, MAX_PATH * sizeof(TCHAR));
            CString strPath = procPath;
            strPath = strPath.Left(str.ReverseFind('\\'));
            

            But in some cases this is not the working path of the program (i.e. the working path is the path assumed if you just gave the program a file name only). One of this case is if you tried to double click a document to be opened with your program , in this case the working path will be that of the document. Any way to get the working path you may use the funcion GetCurrentDirectory() DWORD GetCurrentDirectory( DWORD nBufferLength, // size of directory buffer LPTSTR lpBuffer // directory buffer ); it returns the current working directory of the program. See which one suits your situation and use it. 3. MFC are wrappers for the API functoins, you can call API functions anywhere in your MFC project as any other function, but in some cases an MFC method could have the same name as an API in this case you should preceed the function call with a double scope. e.g. the method SetWindowText() if you want to call the API SetWindowText() instead of it call it like that

            ::SetWindowText()
            
            H 1 Reply Last reply
            0
            • M MAAK

              1. As i told you in my post you could find a wrapper class for the SHBrowseForFolder() on code project this is its link http://www.codeproject.com/shell/cxsbrowsefolder.asp 2. The best way to get the program path exactly is to use the function GetModuleFileName() this is its declaration: DWORD GetModuleFileName( HMODULE hModule, // handle to module LPTSTR lpFilename, // path buffer DWORD nSize // size of buffer ); This function gives you the whole path to your program including the file name. Just remove the file name and you get theabsolute path of the program. This is an example of how to use it to get the program path:

              TCHAR procPath[MAX_PATH];
              GetModuleFileName(hInst, procPath, MAX_PATH * sizeof(TCHAR));
              CString strPath = procPath;
              strPath = strPath.Left(str.ReverseFind('\\'));
              

              But in some cases this is not the working path of the program (i.e. the working path is the path assumed if you just gave the program a file name only). One of this case is if you tried to double click a document to be opened with your program , in this case the working path will be that of the document. Any way to get the working path you may use the funcion GetCurrentDirectory() DWORD GetCurrentDirectory( DWORD nBufferLength, // size of directory buffer LPTSTR lpBuffer // directory buffer ); it returns the current working directory of the program. See which one suits your situation and use it. 3. MFC are wrappers for the API functoins, you can call API functions anywhere in your MFC project as any other function, but in some cases an MFC method could have the same name as an API in this case you should preceed the function call with a double scope. e.g. the method SetWindowText() if you want to call the API SetWindowText() instead of it call it like that

              ::SetWindowText()
              
              H Offline
              H Offline
              Hosam Aly Mahmoud
              wrote on last edited by
              #6

              Thank you very much for your appreciated help. Now I want to remove the path from the filename I got from the Browse button. I shall tell you of a way I tried to use (before I read your second reply). I used system ( "cd >> dir" ) to get the program's path, read it in a buffer and then compared it to the first characters in the filename. To take out the path, here is the code I used:

              		for (short i = 0; i < length; i ++)
              			if ( FileName.GetAt(i) != path.GetAt(i) ) { i=-1; break; }
              		if (i == -1) break;
              		
              		for (short j = 0; i <= FileName.GetLength(); i ++, j++)
              			FileName.SetAt (j, FileName.GetAt(i) );
              

              This part of code was inside a loop, and length is the length of the path of the program. FileName is the buffer in which I stored the filename from the browse button. The problem with the above code is that it doesn't work when the program is at the root folder of a partition. What do you think is the reason? I am thinking of using a similar code (with GetModuleFileName instead of system ( "cd >> dir" ). But shouldn't this give me the same problem? If you find that my method was faulty somehow, please tell me. Again thank you for your help.

              Hosam Aly Mahmoud

              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