第十节 通用对话框

第十节 通用对话框

通用对话框就是一般进行打开操作时弹出来的标准对话框,

1->在消息响应控件函数中添加

 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());
 }

2->如果要支持多选则添加

 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;
 fileDlg.m_ofn.lpstrInitialDir = _T("C:\\"); 
 if ( fileDlg.DoModal() == IDOK ){
  POSITION pos = fileDlg.GetStartPosition();
  while (pos != NULL) {
   CString strPath = fileDlg.GetNextPathName(pos);
   MessageBox(strPath);
  }
 }

 delete [] fileDlg.m_ofn.lpstrFile;
 // TODO: 在此添加控件通知处理程序代码

3->多选记得释放delete [] fileDlg.m_ofn.lpstrFile;

--------------------------------------------------------------------------------------------------------------------------------------

二.目录选择对话框

1->添加

TCHAR szPath[MAX_PATH] = {0};
BROWSEINFO mBroInfo = {0};
mBroInfo.hwndOwner = m_hWnd;
ITEMIDLIST *pidl = SHBrowseForFolder(&mBroInfo);
if (SHGetPathFromIDList(pidl, szPath)){
 SetDlgItemText(IDC_PATH_EDIT, szPath);
}

CoTaskMemFree(pidl);

2->初始化,利用回调函数,增加设置

mBroInfo.lpfn = BrowseCallbackProc;

int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
 if(uMsg == BFFM_INITIALIZED){
  ::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)_T("D:\\Program Files\\SoftWare"));
 }
 return 0;
}
3->目录选择对话框的根目录设定

LPITEMIDLIST GetItemIDListFromPath(CString strPath)
{
 if (strPath.IsEmpty())
  return NULL;

 LPSHELLFOLDER pDesktopFolder = NULL;
 HRESULT hr = SHGetDesktopFolder(&pDesktopFolder);
 if (FAILED(hr))
  return NULL;

 PWCHAR pWchar = NULL;
#ifdef _UNICODE
 pWchar = (LPTSTR)(LPCTSTR)strPath;
#else
 USES_CONVERSION;
 pWchar = A2W(strPath);
#endif

 LPITEMIDLIST pItemIDList = NULL;
 hr = pDesktopFolder->ParseDisplayName(NULL, NULL, pWchar, NULL, &pItemIDList, NULL);
 pDesktopFolder->Release();
 if ( FAILED(hr))
  return NULL;

 return pItemIDList;
}

4->让对话框程序支持拖拽

1> 对话框模版的 Accept Files 属性设置成 True;
2> 响应主对话框类的 WM_DROPFILES 消息;
3> 添加如下示例代码:

TCHAR szPath[MAX_PATH] = {0};
UINT nCount = DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0);
for(UINT idx = 0; idx < nCount; ++idx) {
 DragQueryFile(hDropInfo, idx, szPath, MAX_PATH);
 MessageBox(szPath);
}
DragFinish(hDropInfo);

你可能感兴趣的:(c,File,null,delete,Path,callback)