选取EXCEL文件:
TCHAR szFilters[]= _T("Excel Files (*.xls)|*.xls|");
CFileDialog fileDlg(TRUE, _T("xls"), _T("*.xls"), OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters);
if(fileDlg.DoModal() == IDOK)
{
CString strFilename = fileDlg.GetPathName() + fileDlg.GetFileName();
GetDlgItem(IDC_EDIT_EXCEL)->SetWindowTextW( strFilename );
}
CFileDialog fileDlg(TRUE, _T("xls"), _T("*.xls"), OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters);
if(fileDlg.DoModal() == IDOK)
{
CString strFilename = fileDlg.GetPathName() + fileDlg.GetFileName();
GetDlgItem(IDC_EDIT_EXCEL)->SetWindowTextW( strFilename );
}
fileDlg.DestroyWindow();
选取文件夹:
CString sFolderPath;
BROWSEINFO bi;
wchar_t Buffer[MAX_PATH];
bi.hwndOwner = NULL;
bi.pidlRoot =NULL;//初始化制定的root目录很不容易,
bi.pszDisplayName = Buffer;//此参数如为NULL则不能显示对话框
bi.lpszTitle = _T("修改存儲位置");
//bi.ulFlags = BIF_BROWSEINCLUDEFILES;//包括文件
bi.ulFlags = BIF_EDITBOX;//包括文件
bi.lpfn = NULL;
bi.iImage=IDR_MAINFRAME;
//初始化入口参数bi结束
LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);//调用显示选择对话框
if(pIDList)
{
SHGetPathFromIDList(pIDList, Buffer);
//取得文件夹路径到Buffer里
sFolderPath = Buffer;//将路径保存在一个CString对象里
GetDlgItem(IDC_EDIT_OUTDIR)->SetWindowTextW( sFolderPath );
}
LPMALLOC lpMalloc;
if(FAILED(SHGetMalloc(&lpMalloc))) return;
//释放内存
lpMalloc->Free(pIDList);
BROWSEINFO bi;
wchar_t Buffer[MAX_PATH];
bi.hwndOwner = NULL;
bi.pidlRoot =NULL;//初始化制定的root目录很不容易,
bi.pszDisplayName = Buffer;//此参数如为NULL则不能显示对话框
bi.lpszTitle = _T("修改存儲位置");
//bi.ulFlags = BIF_BROWSEINCLUDEFILES;//包括文件
bi.ulFlags = BIF_EDITBOX;//包括文件
bi.lpfn = NULL;
bi.iImage=IDR_MAINFRAME;
//初始化入口参数bi结束
LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);//调用显示选择对话框
if(pIDList)
{
SHGetPathFromIDList(pIDList, Buffer);
//取得文件夹路径到Buffer里
sFolderPath = Buffer;//将路径保存在一个CString对象里
GetDlgItem(IDC_EDIT_OUTDIR)->SetWindowTextW( sFolderPath );
}
LPMALLOC lpMalloc;
if(FAILED(SHGetMalloc(&lpMalloc))) return;
//释放内存
lpMalloc->Free(pIDList);
lpMalloc->Release();