我是靠谱客的博主 懦弱苗条,最近开发中收集的这篇文章主要介绍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;
}

 

转载于:https://www.cnblogs.com/autumoonchina/p/7083341.html

最后

以上就是懦弱苗条为你收集整理的MfcStrFile的全部内容,希望文章能够帮你解决MfcStrFile所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部