A Browse Button
-
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
-
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
-
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
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 theCFileDialog
s 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 functionGetShortPathName(
) 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 );
-
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 theCFileDialog
s 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 functionGetShortPathName(
) 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 );
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
-
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
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 functionGetModuleFileName()
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 methodSetWindowText()
if you want to call the APISetWindowText()
instead of it call it like that::SetWindowText()
-
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 functionGetModuleFileName()
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 methodSetWindowText()
if you want to call the APISetWindowText()
instead of it call it like that::SetWindowText()
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 (withGetModuleFileName
instead ofsystem ( "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