MFC常用的字符串、文件、目录操作(双11版本)

修改日志:

1.增加了list容器存储;

2.修改了IsFile 和 IsDir的实现,使区别于IfExistFile和IfExistDir;

3.修改了部分函数的名称;

4.其他细节修改;

 

最新版下载:https://github.com/autumoon/MyClasses

 

首先是头文件:

/* ******* StrDirFile.h **********
********* 字符串、文件、目录操作函数声明 ********** */

/* author: autumoon */

#ifndef _STR_DIR_FILE_
#define _STR_DIR_FILE_

#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")

#include <afxdlgs.h> //打开文件
#include <ShlObj.h> //浏览文件夹
#include <list>

class CStrDirFile
{
public:
    CString m_strInput;
    CStrDirFile();
    CStrDirFile(const char szPath[]);
    CStrDirFile(CString strPath);

public:
    bool IsDir(); //
    bool IsDir(CString strPath);
    bool IsFile();
    bool IsFile(CString strPath);

    //字符串操作
    bool IfCstringIsNum(const CString& strString); //判断是否为纯数字,不包含小数点和正负号

    char* Cstring2Char(CString strCstring); //注意防止通过修改char*指向的内容损坏CString结构

    CString AppendSlash(CString& strDirPath); //必要的时候在路径右侧添加'\'
    CString GetDirOfDir();//获取目录的上一级目录 例如D:\\dir\\ -> D:
    CString GetDirOfDir(CString strDirPath);//获取目录的上一级目录 例如D:\\dir\\ -> D:
    CString GetDirOfFile(); //获取文件的所在的目录 例如D:\\dir\\a.txt -> D:\\dir\\a
    CString GetDirOfFile(CString strFilePath); //获取文件的所在的目录 例如D:\\dir\\a.txt -> D:\\dir\\a
    CString GetNameOfDir(CString strDirPath); //获取某个路径的最里层的目录名  例如D:\\dir\\ -> dir
    CString GetNameOfFile(CString strFilePath, bool bWithSuffix = true); //获取文件全路径中的文件名 例如D:\\dir\\a.txt -> a.txt
    CString GetNameOfSuffix(CString strFilePath, bool bWithDot = true);
    CString Float2Cstring(const float fNum, const int nDigit = 6);
    CString Int2Cstring(const int nNum);
    CString ParseLineInCsv(CString strLine, const int nColumn); //返回csv行的某一列的值
    CString ShowTheInput(); //显示当前输入参数

    int CstringToInt(const CString& strNum);

    //文件操作
    bool IfExistFile();
    bool IfExistFile(const char szPath[]);
    bool IfExistFile(CString strFilePath);

    //后缀相关的字符串注意都使用小写
    CString OpenSuffixFile(const char szPath[]); //打开特定类型的文件,返回路径
    CString OpenSuffixFile(CString strSuffix = CString("txt")); //打开特定类型的文件,返回路径
    CString OpenSuffixFile(const int nSuffix, ...); //打开多种类型的文件,返回路径
    CString OpenFile(); //打开任意类型的文件,返回路径
    CString OpenTXT(); //打开txt文件,返回路径

    int ParseTXTFile(CStringArray* pArrContentInFile);
    int ParseTXTFile(std::list<CString>& lContentInFile);    
    int ParseTXTFile(const char szPath[], CStringArray* pArrContentInFile);
    int ParseTXTFile(const char szPath[], std::list<CString>& lContentInFile);
    int ParseTXTFile(CString strFilePath, CStringArray* pArrContentInFile);
    int ParseTXTFile(CString strFilePath, std::list<CString>& lContentInFile);
    int SaveStrToFile(const char szPath[], CString strToSave = CString("hello!"));
    int SaveStrToFile(CString strTxtPath, CString strToSave = CString("hello!"));
    int SaveTXTFile(const char szPath[], CStringArray& arrContent, bool bAppend = false);
    int SaveTXTFile(CString strTxtPath, CStringArray& arrContent, bool bAppend = false);

    //文件夹操作
    bool IfExistDir();
    bool IfExistDir(const char szPath[]);
    bool IfExistDir(CString strDirPath);
    bool RemoveAllFiles(const CString& strDirPath);

    CString BrowseDir(char szTips[]);
    CString BrowseDir(CString strTips = CString("请选择文件夹")); //浏览一个文件夹
    CString BrowseDirPlus(CString strTips = CString("请选择文件夹")); //浏览一个文件夹,带新建按钮

    int ReadCurrentDirs(CStringArray* pArrDirsInFolder); //读取当前目录下的目录,不包含子目录
    int ReadCurrentDirFiles(CStringArray* pArrFilesInFolder, char szSuffix[] = ".txt"); //读取当前目录下的文件,不包含子目录
    int ReadDirs(CString strPath, CStringArray* pArrDirsInFolder, bool bIncludeSub = true); //读取当前目录下的目录
    int ReadDirFiles(CString strPath, CStringArray* pArrFilesInFolder, char szSuffix[] = ".txt", bool bIncludeSub = true); //读取当前目录下的文件

    void ForeachSubDir(CString strPath,BOOL bSuccess); //递归法遍历并删除当前目录下的所有文件及文件夹;

    //MFC其他常用操作
    int CStringArr2list(CStringArray& arrCstring, std::list<CString>& lCstring);
    int list2CstringArr(std::list<CString>& lCstring, CStringArray& arrCstring);
    int SetButtonFont(CButton* pButton, const int& nWidth = 20, const int& nHeight = 0);
    int SetEditFont(CEdit* pCEdit, const int& nWidth = 20, const int& nHeight = 0);
    int StopWatch(); //代码耗时测量代码

};


#endif //_STR_DIR_FILE_

 

然后是cpp文件:

/* ******* StrFileDir.cpp **********
********* 字符串、文件、目录操作函数实现 ********** */

/* author: autumoon */
#include "StrDirFile.h"

/* 构造函数 */
CStrDirFile::CStrDirFile():m_strInput("D:\\autumoon")
{

}

CStrDirFile::CStrDirFile(const char szPath[])
{
    CString strPath(szPath);
    m_strInput = strPath;
}

CStrDirFile::CStrDirFile(CString strPath):m_strInput(strPath)
{

}

bool CStrDirFile::IsDir()
{
    return IsDir(m_strInput);
}

bool CStrDirFile::IsDir(CString strPath)
{
    int nIndex = strPath.ReverseFind('\\');

    if (nIndex != -1)
    {
        CString strDirName = strPath.Mid(nIndex + 1, strPath.GetLength() - nIndex - 1);
        int nIndexP = strDirName.ReverseFind('.');
        return nIndexP == -1;
    }

    return false;
}

bool CStrDirFile::IsFile()
{
    return IsFile(m_strInput);
}

bool CStrDirFile::IsFile(CString strPath)
{
    int nIndex = strPath.ReverseFind('\\');

    if (nIndex != -1)
    {
        CString strDirName = strPath.Mid(nIndex + 1, strPath.GetLength() - nIndex - 1);
        int nIndexP = strDirName.ReverseFind('.');
        return nIndexP != -1;
    }

    return false;
}

CString CStrDirFile::GetDirOfDir(CString strDirPath)
{
    if (strDirPath.Right(1) == '\\')
    {
        strDirPath = strDirPath.Mid(0, strDirPath.GetLength() - 1);
    }

    int index = strDirPath.ReverseFind('\\');

    if (index != -1)
    {
        return strDirPath.Mid(0, index);
    }
    else
    {
        return strDirPath;
    }
}

CString CStrDirFile::GetDirOfFile()
{
    return GetDirOfFile(m_strInput);
}

CString CStrDirFile::GetDirOfFile(CString strFilePath)
{
    if (IsDir(strFilePath))
    {
        return strFilePath;
    }

    int index = strFilePath.ReverseFind('\\');
    return strFilePath.Mid(0, index);
}

CString CStrDirFile::OpenFile()
{
    CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("所有文件(*.*)|*.*||"));

    CString szFileName("");

    if (IsDir())
    {
        dlg.m_ofn.lpstrInitialDir = m_strInput;
    }
    else
    {
        dlg.m_ofn.lpstrInitialDir = GetDirOfFile();
    }

    if (dlg.DoModal() == IDOK)
    {
        szFileName = dlg.GetPathName();
    }

    return szFileName;
}

CString CStrDirFile::OpenTXT()
{
    CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("txt文件(*.txt)|*.txt|所有文件(*.*)|*.*||"));

    CString szFileName("");

    if (IsDir())
    {
        dlg.m_ofn.lpstrInitialDir = m_strInput;
    }
    else
    {
        dlg.m_ofn.lpstrInitialDir = GetDirOfFile();
    }

    if (dlg.DoModal() == IDOK)
    {
        szFileName = dlg.GetPathName();
    }

    return szFileName;
}

CString CStrDirFile::OpenSuffixFile(CString strSuffix)
{
    if (strSuffix.Left(1) == '.')
    {
        //delete the '.' before suffix
        strSuffix = strSuffix.Mid(1, strSuffix.GetLength() - 1);
    }

    CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, strSuffix + "文件(*." + strSuffix + ")|*." + strSuffix + "|所有文件(*.*)|*.*||");

    CString szFileName("");

    if (IsDir())
    {
        dlg.m_ofn.lpstrInitialDir = m_strInput;
    }
    else
    {
        dlg.m_ofn.lpstrInitialDir = GetDirOfFile();
    }

    if (dlg.DoModal() == IDOK)
    {
        szFileName = dlg.GetPathName();
    }

    return szFileName;
}

CString CStrDirFile::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 (IsDir())
    {
        dlg.m_ofn.lpstrInitialDir = m_strInput;
    }
    else
    {
        dlg.m_ofn.lpstrInitialDir = GetDirOfFile();
    }

    if (dlg.DoModal() == IDOK)
    {
        szFileName = dlg.GetPathName();
    }

    return szFileName;
}

bool CStrDirFile::IfExistFile()
{
    return IfExistFile(m_strInput);
}

bool CStrDirFile::IfExistFile(const char szPath[])
{
    CString strFilePath = CString(szPath);
    return IfExistFile(strFilePath);
}

bool CStrDirFile::IfExistFile(CString strFilePath)
{
    CFile file;
    if (file.Open(strFilePath,CFile::modeRead))
    {
        file.Close();
        return true;
    }
    return false;
}

CString CStrDirFile::OpenSuffixFile(const char szPath[])
{
    CString strPath(szPath);
    return OpenSuffixFile(strPath);
}

int CStrDirFile::ParseTXTFile(CStringArray* pArrContentInFile)
{
    return ParseTXTFile(m_strInput, pArrContentInFile);
}

int CStrDirFile::ParseTXTFile(const char szPath[], CStringArray* pArrContentInFile)
{
    CString strFilePath(szPath);
    return ParseTXTFile(strFilePath, pArrContentInFile);
}

int CStrDirFile::ParseTXTFile(CString strFilePath, CStringArray* pArrContentInFile)
{
    CStdioFile file;
    file.Open(strFilePath, CFile::modeRead);

    if (!file.m_pStream)
    {
        return -1;
    }
    CString szLine;
    while(file.ReadString(szLine))
    {
        pArrContentInFile->Add(szLine);
    }

    return 0;
}

int CStrDirFile::ParseTXTFile( CString strFilePath, std::list<CString>& lContentInFile )
{
    CStdioFile file;
    file.Open(strFilePath, CFile::modeRead);

    if (!file.m_pStream)
    {
        return -1;
    }
    CString szLine;
    while(file.ReadString(szLine))
    {
        lContentInFile.push_back(szLine);
    }

    return 0;
}

int CStrDirFile::ParseTXTFile( const char szPath[], std::list<CString>& lContentInFile )
{
    CString strPath(szPath);
    return ParseTXTFile(strPath, lContentInFile);
}

int CStrDirFile::ParseTXTFile( std::list<CString>& lContentInFile )
{
    return ParseTXTFile(m_strInput, lContentInFile);
}

int CStrDirFile::SaveStrToFile(const char szPath[], CString strToSave/* = CString("hello!")*/)
{
    CString strPath(szPath);
    return SaveStrToFile(strPath, strToSave);
}

int CStrDirFile::SaveStrToFile(CString strFilePath, CString strToSave/* = CString("hello!")*/)
{
    CStdioFile file;
    file.Open(strFilePath, CFile::modeCreate|CFile::modeReadWrite|CFile::modeNoTruncate);
    file.SeekToEnd();
    file.WriteString(strToSave);
    file.Close();

    return 0;
}

int CStrDirFile::SaveTXTFile(const char szPath[], CStringArray& arrContent, bool bAppend)
{
    CString strTxtPath(szPath);
    SaveTXTFile(strTxtPath, arrContent, bAppend);

    return 0;
}

int CStrDirFile::SaveTXTFile(CString strTxtPath, CStringArray& arrContent, bool bAppend)
{
    CStdioFile file;
    if (bAppend)
    {
        file.Open(strTxtPath, CFile::modeCreate|CFile::modeReadWrite|CFile::modeNoTruncate);
        file.SeekToEnd();
    }
    else
    {
        file.Open(strTxtPath, CFile::modeCreate|CFile::modeReadWrite);
    }

    for (int i = 0; i < arrContent.GetCount(); i++)
    {
        file.WriteString(arrContent[i]);
    }
    file.Close();

    return 0;
}


bool CStrDirFile::IfExistDir()
{
    return IfExistDir(m_strInput);
}

bool CStrDirFile::IfExistDir(const char szPath[])
{
    CString strDirPath(szPath);

    return IfExistDir(strDirPath);
}

bool CStrDirFile::IfExistDir(CString strDirPath)
{
    //本方法不能判断根目录
    WIN32_FIND_DATA fd;
    bool ret = FALSE;
    HANDLE hFind = FindFirstFile(strDirPath, &fd);
    if ((hFind != INVALID_HANDLE_VALUE) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
    {
        //目录存在
        ret = TRUE;

    }
    FindClose(hFind);

    return ret;
}


bool CStrDirFile::RemoveAllFiles( const CString& strDirPath )
{
        //=========功能概要:删除给定路径下的所有文件========//
    /*
    * 功能:传入绝对路径,清空此文件夹内的所有内容(做为接口使用,故建议不在此函数内递归);
    */
    //==================================================//

    bool bSuccess = true;//作为in/out参数传入;
    ForeachSubDir(strDirPath, bSuccess);

    return bSuccess;
}


CString CStrDirFile::BrowseDir(char szTips[]/* = "请选择文件夹"*/)
{
    CString strTips = CString(szTips);
    return BrowseDir(strTips);
}

CString CStrDirFile::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);
        m_strInput = pszPath;
        return pszPath;
    }
}

CString CStrDirFile::BrowseDirPlus(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);
        m_strInput = pszPath;
        return pszPath;
    }
}

CString CStrDirFile::GetNameOfDir(CString strDirPath)
{
    if (IsFile(strDirPath))
    {
        strDirPath = GetDirOfFile(strDirPath);
    }
    int index = strDirPath.Trim('\\').ReverseFind('\\');
    return strDirPath.Mid(index + 1, strDirPath.GetLength() - index -1);
}

CString CStrDirFile::GetNameOfFile(CString strFilePath, bool bWithSuffix)
{
    int index = strFilePath.ReverseFind('\\');
    CString strFileName = strFilePath.Mid(index + 1, strFilePath.GetLength() - index - 1);

    if (bWithSuffix)
    {
        return strFileName;
    }
    else
    {
        int nIndexOfDot = strFileName.ReverseFind('.');
        if (nIndexOfDot == -1)
        {
            return strFileName;
        }
        else
        {
            return strFileName.Mid(0, nIndexOfDot);
        }
    }

}

CString CStrDirFile::GetNameOfSuffix( CString strFilePath, bool bWithDot /*= true*/ )
{
    if (IsDir(strFilePath))
    {
        return _T("");
    }

    CString strFileName = GetNameOfFile(strFilePath);
    int index = strFileName.ReverseFind('.');
    if (index != -1)
    {
        if (bWithDot)
        {
            return strFileName.Mid(index, strFileName.GetLength() - index);
        }
        else
        {
            return strFileName.Mid(index + 1, strFileName.GetLength() - index - 1);
        }
    }
    else
    {
        return _T("");
    }
}

CString CStrDirFile::Float2Cstring(const float fNum, const int nDigit)
{
    CString strTemp;
    strTemp.Format(_T("%.") + Int2Cstring(nDigit) + _T("lf"), fNum);
    return strTemp;
}

CString CStrDirFile::Int2Cstring(const int nNum)
{
    CString strTemp;
    strTemp.Format(_T("%d"), nNum);
    return strTemp;
}

CString CStrDirFile::ParseLineInCsv(CString strLine, const int nColumn)
{
    CString strContent;
    AfxExtractSubString(strContent, strLine, nColumn, ',');
    return strContent;
}

CString CStrDirFile::AppendSlash(CString& strDirPath)
{
    if (strDirPath.Right(1) != _T('\\'))
    {
        strDirPath += _T('\\');
    }

    return strDirPath;
}

CString CStrDirFile::ShowTheInput()
{
    return m_strInput;
}


int CStrDirFile::CstringToInt( const CString& strNum )
{
    return _ttoi(strNum);
    //return _ttoi64(strNum);
}


bool CStrDirFile::IfCstringIsNum(const CString& strContent)
{
    int n = strContent.GetLength();

    for (int i = 0; i < n; i++)
    {
        if ((strContent[i] > '9') || (strContent[i] < '0'))
        { 
            return false; 
        } 
    }

    return true;
}

char* CStrDirFile::Cstring2Char(CString strCstring)
{
#ifdef _UNICODE
    USES_CONVERSION;
    return W2A(strCstring);
#else
    return (LPSTR)(LPCTSTR)strCstring;
#endif
}

int CStrDirFile::ReadCurrentDirs(CStringArray* pArrDirsInFolder)
{
    if (IsFile())
    {
        return -1;
    }

    return ReadDirs(m_strInput, pArrDirsInFolder, false);
}

int CStrDirFile::ReadCurrentDirFiles(CStringArray* pArrFilesInFolder, char szSuffix[]/* = ".txt"*/)
{
    if (IsFile())
    {
        return -1;
    }

    return ReadDirFiles(m_strInput, pArrFilesInFolder, szSuffix, false);
}

int CStrDirFile::ReadDirs(CString strPath, CStringArray* pArrDirsInFolder, bool bIncludeSub/* = true*/)
{
    CFileFind ff; 
    DWORD size = 0; 
    CString szDir = strPath + _T("\\*.*"); //搜索路径,包括所有子目录 
    BOOL ret = ff.FindFile(szDir); 

    while (ret) 
    { 
        ret = ff.FindNextFile(); 

        if(!ff.IsDots()) 
        { 
            if(ff.IsDirectory() && !ff.IsHidden()) 
            { 
                //子目录结点,递归
                pArrDirsInFolder->Add(ff.GetFilePath());
                if (bIncludeSub)
                {
                    ReadDirs(ff.GetFilePath(), pArrDirsInFolder);
                }
            } 
        } 
    }

    return 0;
}

int CStrDirFile::ReadDirFiles(CString strPath, CStringArray* pArrFilesInFolder, char szSuffix[]/* = "txt"*/, bool bIncludeSub/* = true*/)
{
    CFileFind ff; 
    DWORD size = 0; 

    CString szDir = strPath + _T("\\*.*"); //搜索路径,包括所有子目录 
    BOOL ret = ff.FindFile(szDir); 

    if (IfExistFile(strPath))
    {
        return -1;
    }

    while (ret) 
    { 
        ret = ff.FindNextFile(); 

        if(!ff.IsDots()) 
        { 
            if(ff.IsDirectory() && bIncludeSub) 
            { 
                //子目录结点,递归
                ReadDirFiles(ff.GetFilePath(), pArrFilesInFolder, szSuffix, bIncludeSub);
            } 
            else 
            { 
                if (ff.GetFileName().MakeLower().Find(CString(szSuffix)) != -1)
                {
                    pArrFilesInFolder->Add(ff.GetFilePath());
                }
            } 
        } 
    }

    return 0;
}

void CStrDirFile::ForeachSubDir( CString strPath, BOOL bSuccess )
{
    if (strPath.Right(1)!="\\")
    {
        strPath += "\\";
    }
    strPath += "*.*";  //形如c:\windows\system32\*.*
    CFileFind fileFinder;
    BOOL bFile = fileFinder.FindFile(strPath);
    while(bFile)
    {
        bFile = fileFinder.FindNextFile();
        if (fileFinder.IsDirectory() && !fileFinder.IsDots())  //当为文件夹时;
        {

            CString temp = strPath;
            int i = temp.Find("*.*");
            temp.Delete(i,3);
            temp = temp + fileFinder.GetFileName();  //获取文件夹的全路径,诸如:c:\windows\system32\drivers
            if(fileFinder.GetLength()==0)//如果为空目录,则删除文件夹;
            {
                RemoveDirectory(temp.GetBuffer());
            }
            else
            {
                ForeachSubDir(temp,TRUE);
            }   
        }
        else if(!fileFinder.IsDirectory() && !fileFinder.IsDots()) //当为文件时;
        {
            CString strFullName = fileFinder.GetFilePath();

            //因为CFile::Remove可能会抛出CFile的异常,所以应该用try..catch来处理异常;
            try
            {
                CFile::Remove(strFullName.GetBuffer());
            }
            catch (CFileException* pEx)
            {
#ifdef _DEBUG
                afxDump << "File " << strFullName.GetBuffer() << " cannot be removed\n";
#endif
                pEx->Delete();

                bSuccess = FALSE;//遇到异常,返回FALSE;
                return ;
            }
        }
    }//--end--while
    fileFinder.Close();
}


int CStrDirFile::CStringArr2list( CStringArray& arrCstring, std::list<CString>& lCstring )
{
    lCstring.clear();
    int nCount = arrCstring.GetCount();
    for (int i = 0; i <nCount; i++)
    {
        lCstring.push_back(arrCstring[i]);
    }

    return 0;
}


int CStrDirFile::list2CstringArr( std::list<CString>& lCstring, CStringArray& arrCstring )
{
    arrCstring.RemoveAll();

    for (std::list<CString>::iterator it = lCstring.begin(); it != lCstring.end(); it++)
    {
        arrCstring.Add(*it);
    }

    return 0;
}


int CStrDirFile::SetButtonFont(CButton* pButton, const int& nWidth/* = 20*/, const int& nHeight/* = 0*/)
{
    CFont *f = new CFont;
    f->CreateFont(nWidth,  // nHeight //高度
        nHeight,  // nWidth
        0,  // nEscapement
        0,  // nOrientation
        0,  // nWeight
        FALSE, // bItalic
        FALSE, // bUnderline
        0,  // cStrikeOut
        ANSI_CHARSET,   // nCharSet
        OUT_DEFAULT_PRECIS, // nOutPrecision
        CLIP_DEFAULT_PRECIS, // nClipPrecision
        DEFAULT_QUALITY,  // nQuality
        DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
        _T("微软雅黑"));    // lpszFac  字体名字
    pButton->SetFont(f);

    return 0;
}

int CStrDirFile::SetEditFont(CEdit* pCEdit, const int& nWidth, const int& nHeight)
{
    CFont *f = new CFont;
    f->CreateFont(nWidth,  // nHeight //高度
        nHeight,  // nWidth
        0,  // nEscapement
        0,  // nOrientation
        0,  // nWeight
        FALSE, // bItalic
        FALSE, // bUnderline
        0,  // cStrikeOut
        ANSI_CHARSET,   // nCharSet
        OUT_DEFAULT_PRECIS, // nOutPrecision
        CLIP_DEFAULT_PRECIS, // nClipPrecision
        DEFAULT_QUALITY,  // nQuality
        DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
        _T("微软雅黑"));    // lpszFac  字体名字
    pCEdit->SetFont(f);

    return 0;
}

int CStrDirFile::StopWatch()
{
    //测量时注意区分是Debug版本还是Release版本
    LARGE_INTEGER   litmp;   
    LONGLONG   QPart1,QPart2;   
    double   dfMinus,   dfFreq,   dfTim;   
    QueryPerformanceFrequency(&litmp); //获得计数器的时钟频率   
    dfFreq   =   (double)litmp.QuadPart;   
    QueryPerformanceCounter(&litmp);   //获得初始值     
    QPart1   =   litmp.QuadPart;   

    //----------------------------------------------
    // 插入待测试的代码或函数
    //----------------------------------------------
    QueryPerformanceCounter(&litmp);   //获得中止值   
    QPart2   =   litmp.QuadPart;  
    dfMinus = (double)(QPart2 - QPart1);//获得对应的时间值  
    dfTim   =   dfMinus   /   dfFreq;   
    CString strrr;
    strrr.Format("%f", dfTim*1000000);
    strrr += "微秒";
    AfxMessageBox(strrr);

    return 0;
}

 

posted @ 2014-11-11 09:30  秋月的私语  阅读(856)  评论(0编辑  收藏  举报