I create a button and openFileDialog to open file, how to i write my code to open the file dialog?
Printable View
I create a button and openFileDialog to open file, how to i write my code to open the file dialog?
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;
}
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???
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.Quote:
Originally Posted by Vidya.p.nair
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
}
Your code is off-topic for this forum.Quote:
Originally Posted by CooLWong
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
Quote:
Originally Posted by Paul McKenzie
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.Quote:
Originally Posted by CooLWong
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.
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.Code:System::Windows::Forms::DialogResult::OK
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
Post your managed C++ question in the Managed C++ forum.Quote:
Originally Posted by CooLWong
ok, thank you.Quote:
Originally Posted by Arjay