1.打开目录对话框
void CMFCApplication7Dlg::OnBnClickedButton1()
{
TCHAR szPath[MAX_PATH] = { 0 };
BROWSEINFO mBroInfo = { 0 };
mBroInfo.hwndOwner = m_hWnd;
ITEMIDLIST *pidl = SHBrowseForFolder(&mBroInfo);//打开文件目录对话框
if (SHGetPathFromIDList(pidl, szPath));
{
SetDlgItemText(IDC_EDIT_PATH, szPath);
}
CoTaskMemFree(pidl);
// TODO: 在此添加控件通知处理程序代码
}
2.目录选择对话框的根目录设定
mBroInfo.pidlRoot = GetItemIdListFromPath(_T("D:\\"));
LPITEMIDLIST GetItemIdListFromPath(CString strPath)
{
if (!strPath)
{
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;
}
3.拖拽功能
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);