基于MFC输入输出位置设置方法

来源:互联网 发布:2017好看的网络电影 编辑:程序博客网 时间:2024/06/13 13:36

       今天上午都在整理前段用到的东东,干脆就都理一下吧,虽然是个笨鸟,但是一步一步来,向前走。这篇要总结的是MFC的Dialog上实现输入数据文件路径和存储数据文件路径的方法:

 

1.       选择输入输出单个文件的路径设置方法:

 

选择输入路径的响应函数:

voidCORDCHANGE::OnBnClickedButton1()

{

      // TODO: 在此添加控件通知处理程序代码

      isOpen = TRUE; //表示要打开文件夹

      defaultDir = L"E:\\";   //默认打开的文件路径   

      szFilter = L"Files (*.txt)|*.txt||"; 

      CFileDialog dlg( isOpen,defaultDir,NULL,OFN_FILEMUSTEXIST | OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT,szFilter ); 

      dlg.m_ofn.lpstrTitle = _T("请选择WGS84经纬度坐标文件!");//设置标题 

     

      if(dlg.DoModal() == IDOK) 

      { 

                   

             inPath = dlg.GetPathName();

 

      }

      CWnd::SetDlgItemTextW(IDC_EDIT1, inPath);

       

}

 

选择输出路径的响应函数:

voidCORDCHANGE::OnBnClickedButton2()

{

      // TODO: 在此添加控件通知处理程序代码

      HWND hwnd= GetSafeHwnd();   //得到窗口句柄

      filePath= L""; //得到文件路径

      LPMALLOC pMalloc;

      if (::SHGetMalloc(&pMalloc) ==NOERROR) //取得IMalloc分配器接口

      {  

             BROWSEINFO bi;

             TCHAR pszBuffer[MAX_PATH];

             LPITEMIDLIST pidl;  

             bi.hwndOwner = hwnd;

             bi.pidlRoot = NULL;

             bi.pszDisplayName = pszBuffer;

             bi.lpszTitle = _T("选择文件夹"); //选择目录对话框的上部分的标题

 

             //添加新建文件夹按钮 BIF_NEWDIALOGSTYLE

             bi.ulFlags =  BIF_NEWDIALOGSTYLE | BIF_RETURNONLYFSDIRS |BIF_RETURNFSANCESTORS;

             bi.lpfn = NULL;

             bi.lParam = 0;

             bi.iImage = 0;

 

             if ((pidl =::SHBrowseForFolder(&bi)) != NULL) //取得IMalloc分配器接口

             {  

                    if(::SHGetPathFromIDList(pidl, pszBuffer)) //获得一个文件系统路径

                           {

                           filePath = pszBuffer;

                           }

 

                    pMalloc->Free(pidl); //释放内存

                    //MessageBox(filePath);

                    CWnd::SetDlgItemTextW(IDC_EDIT2,filePath);

 

             }

             pMalloc->Release(); //释放接口

             }

}

 

2.       输入输出多个文件的路径设置方法:

 

选择输入路径的响应函数:

 

voidResample::OnBnClickedButton1()

{

      isOpen = TRUE; //表示要打开文件夹

      defaultDir = L"E:\\";   //默认打开的文件路径   

      szFilter = L"Files (*.tif; *.jpg;*.JPG)|*.tif; *.jpg; *.JPG||"; 

      CFileDialog dlg( isOpen,defaultDir,NULL,OFN_FILEMUSTEXIST | OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT,szFilter ); 

      dlg.m_ofn.lpstrTitle = _T("请选择需要处理的图像!");//设置标题 

     

      if(dlg.DoModal() == IDOK) 

      { 

             posFile=dlg.GetStartPosition(); 

             while(posFile!=NULL) 

             {

                    CString theFile =dlg.GetNextPathName(posFile);//获取完整路径

                    aryFilename.Add(theFile);       //theFile是存储一个一个文件名

                    inPath += theFile;              //inPath用来显示在EDIT条中

                   

                    if(posFile!=NULL)

                    {

                           inPath +=";";

                    }                   

             }

      }

      CWnd::SetDlgItemTextW(IDC_EDIT1, inPath);

      SelFileNum = aryFilename.GetSize();//获取选择的文件数 

     

}

 

(这个操作是将打开的文件夹中所有数据遍历,并将其完整路径存在了数组中,以供后续遍历数组中的一个一个的路径,进行处理。)

 

选择输出路径的响应函数:

 

voidResample::OnBnClickedButton2()

{

 

      HWND hwnd= GetSafeHwnd();   //得到窗口句柄

      filePath= L""; //得到文件路径

      LPMALLOC pMalloc;

      if (::SHGetMalloc(&pMalloc) ==NOERROR) //取得IMalloc分配器接口

      {  

 

             BROWSEINFO bi;

             TCHAR pszBuffer[MAX_PATH];

             LPITEMIDLIST pidl;   

             bi.hwndOwner = hwnd;

             bi.pidlRoot = NULL;

             bi.pszDisplayName = pszBuffer;

             bi.lpszTitle = _T("选择文件夹"); //选择目录对话框的上部分的标题

 

             //添加新建文件夹按钮 BIF_NEWDIALOGSTYLE

             bi.ulFlags =  BIF_NEWDIALOGSTYLE | BIF_RETURNONLYFSDIRS |BIF_RETURNFSANCESTORS;

             bi.lpfn = NULL;

             bi.lParam = 0;

             bi.iImage = 0;

 

             if ((pidl =::SHBrowseForFolder(&bi)) != NULL) //取得IMalloc分配器接口

             {  

                    if(::SHGetPathFromIDList(pidl, pszBuffer)) //获得一个文件系统路径

                           {

                           filePath = pszBuffer;

                           }

 

                    pMalloc->Free(pidl); //释放内存

 

                    //MessageBox(filePath);

                    CWnd::SetDlgItemTextW(IDC_EDIT2,filePath);

 

             }

             pMalloc->Release(); //释放接口

             }

}

 

(这个与单文档路径选择一样,给程序读一个路径,供程序操作。)

 

3.       输入输出带有子文件的路径设置方法:

 

选择输入路径的响应函数(获取文件夹下子文件夹):


voidPathFunc::GetChildFolder(CString strPath,CStringArray& vChildFolders)

{

    //获取文件夹下所有子文件夹名

    CString strFilePath;

    int   dwDirSize = 0;

    strFilePath += strPath;

    strFilePath += _T("//*.*");

    CFileFind finder;

    BOOL bFind = finder.FindFile(strFilePath);

    while (bFind)

    {

        bFind = finder.FindNextFile();

        if (!finder.IsDots())

        {

            CString strTempPath =finder.GetFilePath();

            if (finder.IsDirectory())

            {

                vChildFolders.Add(strTempPath);

            }

            else

            {

                continue;

            }

        }

    }

    finder.Close();

}

原创粉丝点击