MFC常用小工具函数

1、判断文件是否存在

bool IsFileExist(CString sFileName)
{
	CFile File;
	if (TRUE != File.Open(sFileName, CFile::modeRead))
	{
		return false;
	}

	return true;
}

2、判断目录是否存在

bool CLib_Global::IsPathExist(const CString sPathName)
{
	WIN32_FIND_DATA  wfd;
	bool rValue = false;
	HANDLE hFind = FindFirstFile(sPathName, &wfd);
	if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
	{
		rValue = true;
	}
	FindClose(hFind);
	return rValue;
}

3、从全文件路径中找到带后缀的文件名

CString CLib_Global::GetTitleFileName(const CString sFileName)
{
	CString sRt = sFileName;
	const TCHAR cSeparator = _T('\\');
	CString sTempName = sFileName;
	sTempName.Replace(_T('/'), cSeparator); //有些路径是反斜杠,必须校正简化
	int nFind = sTempName.ReverseFind(cSeparator);
	if (nFind >= 0 && nFind<sTempName.GetLength() - 1)
	{
		sRt = sTempName.Mid(nFind + 1);
	}

	return sRt;
}

4、从全文件路径中找到路径名(不含)

CString CLib_Global::GetFilePath(const CString sFileName)
{
	CString sRt;
	const TCHAR cSeparator = _T('\\');
	CString sTempName = sFileName;
	sTempName.Replace(_T('/'), cSeparator); //有些路径是反斜杠,必须校正简化
	int nFind = sTempName.ReverseFind(cSeparator);
	if (nFind>=0 && nFind<sTempName.GetLength()-1)
	{
		sRt = sTempName.Left(nFind);
	}

	return sRt;
}

5、取文件后缀

CString CLib_Global::GetFileExtName(CString sFileName)
{
	CString sExt;
	const int nFindIndex = sFileName.ReverseFind(_T('.'));
	if (nFindIndex > 0 &&
		sFileName.GetLength() - 1 != nFindIndex)
	{
		sExt = sFileName.Mid(nFindIndex + 1);
	}

	return sExt;
}

6、MFC判断文件夹路径是否存在以及创建文件夹的方法

PathIsDirectory()  检测路径是否存在
CreateDirectioy() 创建文件夹
两个函数需要使用shlwapi.h头文件以及添加shlwapi.lib或shlwapi.dll 库函数

7、平面坐标下计算外包类

// Summary: 平面坐标系下的外包立方体,横平竖直
class CHCExtent
{
public:
	CHCExtent()
	{
		m_bIsEmpty = true;
	}

	void AddPoint(const AcGePoint3d& pt)
	{
		if(m_bIsEmpty)
		{
			m_min.x = m_max.x = pt.x;
			m_min.y = m_max.y = pt.y;
			m_min.z = m_max.z = pt.z;
			m_bIsEmpty = false;
		}
		else
		{
			m_min.x = min(m_min.x, pt.x);
			m_min.y = min(m_min.y, pt.y);
			m_min.z = min(m_min.z, pt.z);
			m_max.x = max(m_max.x, pt.x);
			m_max.y = max(m_max.y, pt.y);
			m_max.z = max(m_max.z, pt.z);
		}
	}

	void AddExtent(const CHCExtent& other)
	{
		if(other.m_bIsEmpty)
			return;

		if(m_bIsEmpty)
			*this = other;
		else
		{
			m_min.x = min(m_min.x, other.m_min.x);
			m_min.y = min(m_min.y, other.m_min.y);
			m_min.z = min(m_min.z, other.m_min.z);
			m_max.x = max(m_max.x, other.m_max.x);
			m_max.y = max(m_max.y, other.m_max.y);
			m_max.z = max(m_max.z, other.m_max.z);
		}
	}

public:
	bool m_bIsEmpty;
	AcGePoint3d m_min;
	AcGePoint3d m_max;
};

8、判断是否是数字

bool IsNumber(CString sNumber)
{
	if (sNumber.IsEmpty())
		return false;

	if (sNumber[0] == '-')
	{
		sNumber = sNumber.Mid(1);
		if (sNumber.IsEmpty())
			return false;
	}

	int nDotCount = 0;
	for (int i = 0; i < sNumber.GetLength(); i++)
	{
		if (sNumber[i] == '.')
			nDotCount++;
		else if ((sNumber[i] < '0') || (sNumber[i] > '9'))
			return false;
	}

	if (nDotCount > 1)
		return false;

	return true;
}

9、删除文件夹及其里面的所有内容

void myDeleteDirectory(CString directory_path)   //删除一个文件夹下的所有内容
{   
	CFileFind finder;
	CString path;
	path.Format("%s/*.*",directory_path);
	BOOL bWorking = finder.FindFile(path);
	while(bWorking)
	{
		bWorking = finder.FindNextFile();
		if(finder.IsDirectory() && !finder.IsDots())
		{//处理文件夹
			myDeleteDirectory(finder.GetFilePath()); //递归删除文件夹
			RemoveDirectory(finder.GetFilePath());
		}
		else
		{//处理文件
			DeleteFile(finder.GetFilePath());
		}
	}
}

10、CList设定水平拖动

void SetHorizontalScroll()
{
	CDC* dc = GetDC();
	SIZE s;
	int index;
	CString str;
	long temp;
	for (index = 0; index< m_listLog.GetCount(); index++)
	{
		m_listLog.GetText(index, str);
		s = dc->GetTextExtent(str, str.GetLength() + 1);   // 获取字符串的像素大小
		// 如果新的字符串宽度大于先前的水平滚动条宽度,则重新设置滚动条宽度
		// IDC_LISTBOX为m_List的资源ID
		temp = (long)SendDlgItemMessage(IDC_LIST_LOG, LB_GETHORIZONTALEXTENT, 0, 0); //temp得到滚动条的宽度
		if (s.cx > temp)
		{
			SendDlgItemMessage(IDC_LIST_LOG, LB_SETHORIZONTALEXTENT, (WPARAM)s.cx, 0);
		}
	}
	ReleaseDC(dc);
}

11 、利用分隔符分隔字符串
//字符串处理部分
void GetStrArrayFromString(TCHAR cSeparator, CStringArray &saTag, CString sSource)
{
	saTag.RemoveAll();

	int nPreIndex=-1, nNextIndex;
	while((nNextIndex=sSource.Find(cSeparator, nPreIndex+1))>-1)
	{
		CString sName = sSource.Mid(nPreIndex+1, nNextIndex-nPreIndex-1);
		saTag.Add(sName );
		nPreIndex = nNextIndex;
	}

	//允许最后以逗号或不以逗号结尾
	int nLen = sSource.GetLength();
	if(nPreIndex+1<sSource.GetLength())
	{
		int nL = nLen - nPreIndex - 1;
		CString sName = sSource.Mid(nPreIndex+1, sSource.GetLength() - nPreIndex - 1);
		saTag.Add(sName );
	}
}
发布了58 篇原创文章 · 获赞 42 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/m0_37251750/article/details/102700304