How could you do it?
I have done many projects in MFC and i have some experiment about this topic.
Today i am here to say to you the way you can create a "Browse" button in MFC application.
I suppose that your application's name is DemoBrowse so there are two choice for you.
If you want to browse only one file, you can drag an button object, after that you can write it's source as:
void CDemoBrowseDlg::OnBnClickedButtonBrowse()
{
CFileDialog FileDialog(TRUE,
"*.*",
NULL,
OFN_HIDEREADONLY,
"All files:(*.*)|*.*| // If you want to chose all kinds of file
Files .txt (*.txt)|*.txt| // If you only want to chose .txt file
Files word (*.doc;*.docx)|*.doc;*.docx||"); // If you want to chose .doc and .docx file
if (FileDialog.DoModal() == IDOK)
{
CString pathName = FileDialog.GetPathName();
// And now you can do anything with this path
}
If you want to get lots of files at the same time, you can use this source:
void CDemoBrowseDlg::OnBnClickedButtonBrowse()
{
int i = 0;
{
int i = 0;
CString arrayListFile[100]; // CString array which contains paths
UpdateData(TRUE);
CFileDialog FileDialog(TRUE,
UpdateData(TRUE);
CFileDialog FileDialog(TRUE,
"*.*",NULL,OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT,
"Image Files (*.jpg;*.bmp;*.png;*.gif;*.ico;*.pbm;*.tif;*.pgm;*.xpm)|
"Image Files (*.jpg;*.bmp;*.png;*.gif;*.ico;*.pbm;*.tif;*.pgm;*.xpm)|
*.jpg;*.bmp;*.png;*.gif;*.ico;*.pbm;*.tif;*.pgm;*.xpm|
Files(*.txt;*.docx)|*.txt;*.docx||");
// Create buffer for ten filenames
TCHAR strFiles[MAX_PATH * 100] = "";
FileDialog.m_ofn.lpstrFile = strFiles;
FileDialog.m_ofn.nMaxFile = sizeof(strFiles);
// Create buffer for ten filenames
TCHAR strFiles[MAX_PATH * 100] = "";
FileDialog.m_ofn.lpstrFile = strFiles;
FileDialog.m_ofn.nMaxFile = sizeof(strFiles);
// If it has err here, you can use a number ( > 0) to replace sizeof(strFiles). I haven't also known why it has err
if(FileDialog.DoModal() == IDOK)
{
POSITION pos = FileDialog.GetStartPosition();
if(pos)
{
CString PathName;
do
{
PathName = FileDialog.GetNextPathName(pos);
arrayListFile[i] = PathName;
i++;if(FileDialog.DoModal() == IDOK)
{
POSITION pos = FileDialog.GetStartPosition();
if(pos)
{
CString PathName;
do
{
PathName = FileDialog.GetNextPathName(pos);
arrayListFile[i] = PathName;
} while(pos);
}
// And now you can do anything with your paths
}
If you use Unicode format in your application, you have to insert "L" character before every quote.
Example: "*.*" --> L"*.*"
So you can create easily a "Browse" button by yourself.
Best wishes for you. Thanks for your attention!
Do your best, the rest will come!
Không có nhận xét nào:
Đăng nhận xét