-
July 15th, 2007, 07:01 PM
#1
How to use the openFileDialog?
I create a button and openFileDialog to open file, how to i write my code to open the file dialog?
-
July 15th, 2007, 07:05 PM
#2
Re: How to use the openFileDialog?
Example code:
Code:
int BrowseForFile(HWND hParent, LPTSTR szDir, LPTSTR szFile, UINT nIDTitle, UINT nIDFilters)
{
//TestLongFileBrowse();
OPENFILENAME ofn;
LPTSTR szPlainName = NULL;
TCHAR szInitialDir[MAX_PATH * 10] = _T("");
TCHAR szFileName[MAX_PATH * 10] = _T("");
TCHAR szFilters[MAX_PATH * 10] = _T("");
TCHAR szTitle[MAX_PATH * 10] = _T("");
BOOL bResult = FALSE;
// Load the title
LoadString(g_hInst, nIDTitle, szTitle, _tsize(szTitle));
// Resolve the initial dir
if(szDir != NULL)
{
if((DWORD_PTR)szDir <= 0x10000)
GetDlgItemText(hParent, (UINT)(UINT_PTR)szDir, szInitialDir, _tsize(szInitialDir));
else
_tcscpy(szInitialDir, szDir);
// If an NT native name, clear it
if(!_tcsncmp(szInitialDir, _T("\\\\?\\"), 4))
memmove(szInitialDir, &szInitialDir[4], (sizeof(szInitialDir) - 4 * sizeof(TCHAR)));
}
// Resolve the file name
if(szFile != NULL)
{
if((DWORD_PTR)szFile <= 0x10000)
GetDlgItemText(hParent, (UINT)(UINT_PTR)szFile, szFileName, _tsize(szFileName));
else
_tcscpy(szFileName, szFile);
// If an NT native name, clear it
if(!_tcsncmp(szFileName, _T("\\\\?\\"), 4))
memmove(szFileName, &szFileName[4], (sizeof(szFileName) - 4 * sizeof(TCHAR)));
}
// Load the filters. They must be separated by "|"
LoadString(g_hInst, nIDFilters, szFilters, _tsize(szFilters));
for(int i = 0; szFilters[i] != 0; i++)
{
if(szFilters[i] == _T('|'))
szFilters[i] = 0;
}
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hParent;
ofn.hInstance = g_hInst;
ofn.lpstrInitialDir = szInitialDir;
ofn.lpstrFilter = szFilters;
ofn.lpstrFile = szFileName;
ofn.nMaxFile = _tsize(szFileName);
ofn.lpstrTitle = szTitle;
ofn.nMaxFileTitle = _tsize(szTitle);
ofn.Flags = OFN_ENABLESIZING | OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_NOCHANGEDIR | OFN_NONETWORKBUTTON | OFN_PATHMUSTEXIST;
bResult = GetOpenFileName(&ofn);
if(bResult)
{
szPlainName = szFileName;
// Store the initial dir
if(szDir != NULL)
{
_tcscpy(szInitialDir, szFileName);
szPlainName = GetPlainName(szInitialDir);
*(szPlainName - 1) = 0;
if((DWORD_PTR)szDir <= 0x10000)
SetDlgItemText(hParent, (UINT)(UINT_PTR)szDir, szInitialDir);
else
_tcscpy(szDir, szInitialDir);
}
// Store the file name
if(szFile != NULL)
{
if((DWORD_PTR)szFile <= 0x10000)
SetDlgItemText(hParent, (UINT)(UINT_PTR)szFile, szPlainName);
else
_tcscpy(szFile, szPlainName);
}
}
return bResult;
}
-
July 15th, 2007, 07:41 PM
#3
Re: How to use the openFileDialog?
Thank your sample code. I using the Visual C++ 2005 Express Edition. My button called button1 and my openFileDialog called openFileDialog1. So the sample code can use???
-
July 16th, 2007, 05:58 AM
#4
Re: How to use the openFileDialog?
I am using Visual Studio C++ 6.0
here you can use the open file dialog in this manner
i think it will be similar
Code:
void CProperties::OnButtonTargetbrowse()
{
CFileDialog opendialog(TRUE,"*.JPEG;*.BMP","Untitled.bmp",OFN_HIDEREADONLY,"*.jpeg;*.bmp\0*.jpeg;*.bmp\0",NULL);
opendialog.DoModal();
}
-
July 16th, 2007, 02:54 PM
#5
Re: How to use the openFileDialog?
Originally Posted by Vidya.p.nair
I am using Visual Studio C++ 6.0
here you can use the open file dialog in this manner
i think it will be similar
Code:
void CProperties::OnButtonTargetbrowse()
{
CFileDialog opendialog(TRUE,"*.JPEG;*.BMP","Untitled.bmp",OFN_HIDEREADONLY,"*.jpeg;*.bmp\0*.jpeg;*.bmp\0",NULL);
opendialog.DoModal();
}
Nope, that is incorrect. CFileDialog is an MFC class. You cannot write MFC applications with VisualC++ 2005 Express Edition.
-
July 16th, 2007, 07:17 PM
#6
Re: How to use the openFileDialog?
I can open file dialog in my project. There is my sample code:
Code:
OpenFileDialog CommonDialog;
CommonDialog.Title = "Select File";
CommonDialog.Multiselect = true;
CommonDialog.CheckFileExists = true;
CommonDialog.Filter = "All File(*.*) | *.*";
CommonDialog.FilterIndex = 2;
//if user has clicked on the OK button
if (CommonDialog.ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
//do something here
}
-
July 16th, 2007, 08:06 PM
#7
Re: How to use the openFileDialog?
Originally Posted by CooLWong
I can open file dialog in my project. There is my sample code:
Your code is off-topic for this forum.
You've made several posts, where each one deals with Managed C++. Your code is not traditional C++. It is CLR or Managed C++, and is off-topic for this forum. There is a managed C++ forum elsewhere here.
Regards,
Paul McKenzie
-
July 16th, 2007, 08:43 PM
#8
Re: How to use the openFileDialog?
Originally Posted by Paul McKenzie
Your code is off-topic for this forum.
You've made several posts, where each one deals with Managed C++. Your code is not traditional C++. It is CLR or Managed C++, and is off-topic for this forum. There is a managed C++ forum elsewhere here.
Regards,
Paul McKenzie
Sorry, which forum i can discuss??? I think visual c++ question that can ask here.
-
July 16th, 2007, 09:34 PM
#9
Re: How to use the openFileDialog?
Originally Posted by CooLWong
Sorry, which forum i can discuss??? I think visual c++ question that can ask here.
Your code is not "traditional" Visual C++. It uses classes that only exist in the Managed environment.
Create a Win32 console or GUI project, and you will see that your code will not compile. And no, it isn't a matter of including some header files -- the syntax used in these programs are not "traditional" C++ syntax.
Code:
System::Windows::Forms::DialogResult::OK
Classes such as this, code that uses "^" in weird places (which I've seen in this forum lately), etc. are *not* C++ or MFC or ATL, but part of the managed extensions.
If you don't know the difference between the two, then it is highly important that you do get more familiar with the compiler tools and languages that you're using to create programs.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; July 16th, 2007 at 09:38 PM.
-
July 16th, 2007, 10:25 PM
#10
Re: How to use the openFileDialog?
Originally Posted by CooLWong
Sorry, which forum i can discuss??? I think visual c++ question that can ask here.
Post your managed C++ question in the Managed C++ forum.
-
July 16th, 2007, 10:51 PM
#11
Re: How to use the openFileDialog?
Originally Posted by Arjay
Post your managed C++ question in the Managed C++ forum.
ok, thank you.
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
|