MFC打开文件对话框及根据路径获取文件名称

//22定义全局变量
CString g_NewFileName;

//22打开文件对话框
bool CGetFacesDlg::OpenFileFrame()  //CGetFacesDlg为类名,自己可对应修改
{
    CString filePath;
    //采用了MFC提供的类CFileDialog
    CFileDialog fileOpenDlg(TRUE, _T("*.png"), "",OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY,
    "image files (*.jpg;*.bmp) |*.jpg;*.bmp|image file(*.bmp)|*.bmp;|image file(*.png)|*.png;|All Files (*.*)|*.*||",NULL);
    char title[]= {"Open Image"};  //仅仅设置对话框的标题
    fileOpenDlg.m_ofn.lpstrTitle= title;
    if (fileOpenDlg.DoModal()==IDOK)
    {    
        filePath=fileOpenDlg.GetPathName();
        
        if(GetFileNameFromFilePath(filePath)==false)//22从文件路径获取要新建的文件夹的名称
        {
        MessageBox("获取文件夹名称失败","错误提示",MB_OK);
        return ;
        }

        g_SourceImg= cvvLoadImage(filePath); //根据打开图像的路径读图
        if(g_SourceImg!=NULL)
        {
        m_bflagBtn=true;
        DrawPicToHdc(g_SourceImg,IDC_STATIC); //根据控件的ID将图像显示在对话框中
        }

    
    }
}

//22从文件路径获取要新建的文件夹的名称
bool CGetFacesDlg::GetFileNameFromFilePath(CString FilePath)
{
        int nalllength=FilePath.GetLength();
        int nindex=FilePath.ReverseFind('\\');
        int nsubLength=nalllength-nindex-1;

        CString Picname=FilePath.Right(nsubLength);        //22获取路径下文件的名称(包含格式)
        nindex=Picname.Find('.',0);
        g_NewFileName=Picname.Left(nindex);                   //22获取路径下文件的名称(不包含格式)
                                if(g_NewFileName==" ")return false;
        return true;
}

猜你喜欢

转载自blog.csdn.net/ZDT_zdh/article/details/84257079