vc++基础班[15]---通用对话框讲解
------------------------------------------ Begin ----------------------------------------
①、文件选择对话框;
//单选;
CFileDialog fileDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT , _T("Text Files (*.txt)|*.txt|WinRar Files (*.rar)|*.rar|Data Files (*.xlc;*.xls)|*.xlc; *.xls|All Files (*.*)|*.*||"));
if ( fileDlg.DoModal() == IDOK ){
SetDlgItemText(IDC_PATH_EDIT, fileDlg.GetPathName());
}
//多选;
CFileDialog fileDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT, _T("Text Files (*.txt)|*.txt|WinRar Files (*.rar)|*.rar|Data Files (*.xlc;*.xls)|*.xlc; *.xls|All Files (*.*)|*.*||"));
if ( fileDlg.DoModal() == IDOK ){
POSITION pos = fileDlg.GetStartPosition();
while (pos != NULL) {
CString strPath = fileDlg.GetNextPathName(pos);
MessageBox(strPath);
}
}
#####################################################
②、解决多选问题:http://www.cctry.com/thread-21906-1-1.html
CFileDialog fileDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT, _T("Text Files (*.txt)|*.txt|WinRar Files (*.rar)|*.rar|Data Files (*.xlc;*.xls)|*.xlc; *.xls|All Files (*.*)|*.*||"));
const int MAX_FILE_NUM = 100; //最大允许选择100个文件
fileDlg.m_ofn.lpstrFile = new TCHAR[MAX_PATH*MAX_FILE_NUM]; //重新定义缓冲区的大小
ZeroMemory(fileDlg.m_ofn.lpstrFile, MAX_PATH*MAX_FILE_NUM); //初始化定义的缓冲区
fileDlg.m_ofn.nMaxFile = MAX_PATH*MAX_FILE_NUM;
if ( fileDlg.DoModal() == IDOK ){
POSITION pos = fileDlg.GetStartPosition();
while (pos != NULL) {
CString strPath = fileDlg.GetNextPathName(pos);
MessageBox(strPath);
}
}
delete [] fileDlg.m_ofn.lpstrFile; //别忘了释放
#####################################################
③、初始化打开路径:
fileDlg.m_ofn.lpstrInitialDir = _T("C:\\");
我的电脑 --- ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}
我的文档 --- ::{450D8FBA-AD25-11D0-98A8-0800361B1103}
#####################################################
④、扩展文件选择对话框:
应vip会员要求:http://www.cctry.com/thread-18691-1-1.html
1>、添加继承自 CFileDialog 类的子类 CFileDialogEx;
2>、在主对话框界面上按钮的响应中改用 CFileDialogEx 类;
3>、增加 OFN_ENABLETEMPLATE 标志,以进行扩展模版对话框的支持:
CFileDialogEx fileDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_ENABLETEMPLATE, _T("Text Files (*.txt)|*.txt|WinRar Files (*.rar)|*.rar|Data Files (*.xlc;*.xls)|*.xlc; *.xls|All Files (*.*)|*.*||"));
4>、向工程中添加对话框资源模版 IDD_EXT_DIALOG,属性设置如下:
Style:Child
Border:None
Contol:True
3D-Look:True
Clip siblings:True
5>、向文件选择对话框中设置模版:
fileDlg.SetTemplate(0, IDD_EXT_DIALOG);
6>、重载 CFileDialogEx::OnFileNameOK() 函数(文件选择成功后调用),进行自定义的处理:
GetDlgItemText(IDC_PARAM_EDIT, m_strParam);
7>、获得自定义的值:
CFileDialogEx fileDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_ENABLETEMPLATE, _T("Text Files (*.txt)|*.txt|WinRar Files (*.rar)|*.rar|Data Files (*.xlc;*.xls)|*.xlc; *.xls|All Files (*.*)|*.*||"));
fileDlg.SetTemplate(0, IDD_EXT_DIALOG);
if ( fileDlg.DoModal() == IDOK ){
ShellExecute(m_hWnd, _T("open"), fileDlg.GetPathName(), fileDlg.m_strParam, NULL, SW_SHOW);
}
⑤、改变扩展对话框模版的位置:
只需向扩展对话框模版中加入一个ID为 stc32 的 Static 控件即可!
#####################################################
#####################################################
############## 以下两点内容下节课给大家讲解 ###############
⑥、目录选择对话框;
⑦、让对话框程序支持拖拽。
------------------------------------- End -------------------------------------------
0则评论给“vc++基础班[15]---通用对话框讲解”