• MfcStrFile


    /* ******* MfcStrFile.h **********
    ********* 字符串、文件、目录操作函数声明 ********** */
    
    /* author: autumoon */
    
    #pragma once
    
    #ifdef _X86_
    #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'"")
    #endif
    
    #include "StdStrFile.h"
    #include <afxdlgs.h>        //打开文件
    #include <ShlObj.h>         //浏览文件夹
    
    class CMfcStrFile : public CStdStrFile
    {
    public:
        char* CString2Char(CString strCstring);                             //内部申请了内存空间,注意释放
    
        CString BrowseDir(CString strTips = CString("请选择文件夹"));                                 //浏览一个文件夹
        CString BrowseDirNew(CString strTips = CString("请选择文件夹"));                              //浏览一个文件夹,带新建按钮
        CString OpenFile();
        CString OpenSuffixFile(std::string strSuffix);
        CString OpenSuffixFile(const int nSuffix, ...);
    };
    #include "MfcStrFile.h"
    
    char* CMfcStrFile::CString2Char(CString strCstring)
    {
    #ifdef _UNICODE
        wchar_t *pWChar = strCstring.GetBuffer(); //获取str的宽字符用数组保存  
        strCstring.ReleaseBuffer();
        int nLen = strCstring.GetLength(); //获取str的字符数  
        char *pChar = new char[nLen * 2 + 1];
        memset(pChar, 0, nLen * 2 + 1);
        size_t i;
        int rtnVal = (int)wcstombs_s(&i, pChar, nLen * 2 + 1, pWChar, _TRUNCATE); //宽字符转换为多字节字符
    
        return pChar; //注意此内存并未释放
    #else
        char *pDst = new char[strCstring.GetLength() + 1];
        strcpy(pDst, strCstring);
    
        return pDst;
    #endif // _UNICODE
    }
    
    CString CMfcStrFile::BrowseDir(CString strTips/* = CString("请选择文件夹")*/)
    {
        CString szFileFolderPath;
        TCHAR pszPath[MAX_PATH];
        BROWSEINFO biFolder;
        biFolder.hwndOwner = NULL;
        biFolder.pidlRoot = NULL;
        biFolder.pszDisplayName = NULL;
        biFolder.lpszTitle = strTips;
        biFolder.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
        biFolder.lpfn = NULL;
        biFolder.lParam = 0;
    
        LPITEMIDLIST pidl = SHBrowseForFolder(&biFolder);
        if (!pidl)
        {
            return "";
        }
        else
        {
            SHGetPathFromIDList(pidl, pszPath);
            return pszPath;
        }
    }
    
    CString CMfcStrFile::BrowseDirNew(CString strTips/* = CString("请选择文件夹")*/)
    {
        CString szFileFolderPath;
        TCHAR pszPath[MAX_PATH];
        BROWSEINFO biFolder;
        biFolder.hwndOwner = NULL;
        biFolder.pidlRoot = NULL;
        biFolder.pszDisplayName = NULL;
        biFolder.lpszTitle = strTips;
        biFolder.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT | BIF_NEWDIALOGSTYLE;
        biFolder.lpfn = NULL;
        biFolder.lParam = 0;
    
        LPITEMIDLIST pidl = SHBrowseForFolder(&biFolder);
        if (!pidl)
        {
            return "";
        }
        else
        {
            SHGetPathFromIDList(pidl, pszPath);
            return pszPath;
        }
    }
    
    CString CMfcStrFile::OpenFile()
    {
        CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("所有文件(*.*)|*.*||"));
    
        CString szFileName("");
    
        if (dlg.DoModal() == IDOK)
        {
            szFileName = dlg.GetPathName();
        }
    
        return szFileName;
    }
    
    CString CMfcStrFile::OpenSuffixFile(std::string strSuffix)
    {
        if (strSuffix[0] == '.')
        {
            //delete the '.' before suffix
            strSuffix.erase(0, 1);
        }
    
        CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, CString((strSuffix + "文件(*." + strSuffix + ")|*." + strSuffix + "|所有文件(*.*)|*.*||").c_str()));
    
        CString szFileName("");
    
        if (dlg.DoModal() == IDOK)
        {
            szFileName = dlg.GetPathName();
        }
    
        return szFileName;
    }
    
    CString CMfcStrFile::OpenSuffixFile(const int nSuffix, ...)
    {
        va_list argp;
        va_start(argp, nSuffix);
    
        CStringArray arrSuffixs;
        CString strSuffix;
        for (int i = 0; i < nSuffix; i++)
        {
            strSuffix = va_arg(argp, char*);
            arrSuffixs.Add(strSuffix);
        }
        va_end(argp);
    
        //打开多种类型
        for (int i = 0; i < nSuffix; i++)
        {
            if (arrSuffixs[i].Left(1) == '.')
            {
                //delete the '.' before suffix
                arrSuffixs[i] = arrSuffixs[i].Mid(1, arrSuffixs[i].GetLength() - 1);
            }
        }
    
        CString strTemp("");
        for (int i = 0; i < nSuffix; i++)
        {
            strTemp += arrSuffixs[i] + "文件(*." + arrSuffixs[i] + ")|*." + arrSuffixs[i] + "|";
        }
    
        CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, strTemp + "所有文件(*.*)|*.*||");
    
        CString szFileName("");
    
        if (dlg.DoModal() == IDOK)
        {
            szFileName = dlg.GetPathName();
        }
    
        return szFileName;
    }
  • 相关阅读:
    Salesforce学习笔记(一)
    踏上Salesforce的学习之路(二)
    踏上Salesforce的学习之路(一)
    Salesforce注册开发者账号
    ubuntu下安装rtl8811cu/rtl8821cu网卡 Tplink WDN5200H网卡
    基于JRebel开发的MySQL Explain插件
    Logback配置解析
    基于springboot实现http响应异常信息国际化
    高并发场景下请求合并的实践
    后台开发常用mysql语句_v1.0
  • 原文地址:https://www.cnblogs.com/autumoonchina/p/7083341.html
Copyright © 2020-2023  润新知