首页 > 代码库 > MFC 打开文件/打开路径

MFC 打开文件/打开路径

void CMFCOpenFileOrDirectoryDlg::OnBnClickedButton1()
{
    //打开文件
    CFileDialog dlgOpen(TRUE/*TRUE打开,FALSE保存*/,
        0,
        0,
        OFN_HIDEREADONLY|OFN_FILEMUSTEXIST,
        "All Files(*.*)|*.*||",//文件过滤器
        NULL);
    char szCurrentPath[MAX_PATH] = {0};
    GetCurrentDirectory(MAX_PATH, szCurrentPath);
    dlgOpen.m_ofn.lpstrInitialDir = szCurrentPath;
    if(IDOK == dlgOpen.DoModal())
    {
        SetDlgItemText(IDC_EDIT1, dlgOpen.GetPathName());        
    }
}

void CMFCOpenFileOrDirectoryDlg::OnBnClickedButton2()
{
    //打开路径
    BROWSEINFO bi;
    char Buffer[MAX_PATH] = {0};
    //初始化入口参数bi开始
    bi.hwndOwner = NULL;
    bi.pidlRoot =NULL;
    bi.pszDisplayName = Buffer;//此参数如为NULL则不能显示对话框
    bi.lpszTitle = "打开路径";
    bi.ulFlags = BIF_EDITBOX;//包括文件
    bi.lpfn = NULL;
    bi.iImage=IDR_MAINFRAME;

    LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);//调用显示选择对话框
    if(pIDList)
    {
        SHGetPathFromIDList(pIDList, Buffer);//取得文件夹路径到Buffer里
        SetDlgItemText(IDC_EDIT1, Buffer);
    }
}

 

MFC 打开文件/打开路径