MfcStrFile

Posted 秋月的私语

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

 

以上是关于MfcStrFile的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数