VC++ Ini配置文件操作

头文件声明(CIniFile.h):

#ifndef INIFILE_HEAD_FILE
#define INIFILE_HEAD_FILE

#pragma once

//配置数据
class CIniFile
{
	//变量定义
protected:
	TCHAR							m_szIniFile[MAX_PATH];				//文件路径

	//函数定义
public:
	//构造函数
	CIniFile();
	//析构函数
	virtual ~CIniFile();

	//路径函数
public:
	//设置路径
	VOID SetIniFilePath(LPCTSTR pszIniFile);
	//获取路径
	LPCTSTR GetIniFilePath() { return m_szIniFile; }

	//数据读取
public:
	//读取矩形
	bool ReadRect(RECT & ValueRect, LPCTSTR pszItem, LPCTSTR pszSubItem);
	//读取尺寸
	bool ReadSize(SIZE & ValueSize, LPCTSTR pszItem, LPCTSTR pszSubItem);
	//读取坐标
	bool ReadPoint(POINT & ValuePoint, LPCTSTR pszItem, LPCTSTR pszSubItem);
	//读取颜色
	bool ReadColor(COLORREF & ValueColor, LPCTSTR pszItem, LPCTSTR pszSubItem);

	//常规读取
public:
	//读取数值
	UINT ReadInt(LPCTSTR pszItem, LPCTSTR pszSubItem, INT nDefault);
	//读取字符
	LPCTSTR ReadString(LPCTSTR pszItem, LPCTSTR pszSubItem, LPCTSTR pszDefault, LPTSTR pszString, WORD wMaxCount);

	//内部函数
protected:
	//转换数值
	LONG SwitchStringToValue(LPCTSTR & pszSring);
};

#endif

源码实现(CIniFile.cpp):

#include "StdAfx.h"
#include "CIniFile.h"

//构造函数
CIniFile::CIniFile()
{
	//设置变量
	ZeroMemory(m_szIniFile,sizeof(m_szIniFile));

	return;
}

//析构函数
CIniFile::~CIniFile()
{
}

//设置路径
VOID CIniFile::SetIniFilePath(LPCTSTR pszIniFile)
{
	//设置变量
	lstrcpyn(m_szIniFile,pszIniFile,sizeof(m_szIniFile));

	return;
}

//读取数值
UINT CIniFile::ReadInt(LPCTSTR pszItem, LPCTSTR pszSubItem, INT nDefault)
{
	//效验状态
	ASSERT(m_szIniFile[0]!=0);

	//读取数值
	UINT uReadData=GetPrivateProfileInt(pszItem,pszSubItem,nDefault,m_szIniFile);

	return uReadData;
}

//读取字符
LPCTSTR CIniFile::ReadString(LPCTSTR pszItem, LPCTSTR pszSubItem, LPCTSTR pszDefault, LPTSTR pszString, WORD wMaxCount)
{
	//效验状态
	ASSERT(m_szIniFile[0]!=0);

	//读取字符
	GetPrivateProfileString(pszItem,pszSubItem,pszDefault,pszString,wMaxCount,m_szIniFile);

	return pszString;
}

//读取矩形
bool CIniFile::ReadRect(RECT & ValueRect, LPCTSTR pszItem, LPCTSTR pszSubItem)
{
	//效验状态
	ASSERT(m_szIniFile[0]!=0);

	//设置变量
	TCHAR szReadData[64]=TEXT("");
	ZeroMemory(&ValueRect,sizeof(ValueRect));

	//读取字符
	GetPrivateProfileString(pszItem,pszSubItem,TEXT(""),szReadData,sizeof(szReadData),m_szIniFile);

	//数据处理
	if (szReadData[0]!=0)
	{
		//读取变量
		LPCTSTR pszString=szReadData;
		ValueRect.left=SwitchStringToValue(pszString);
		ValueRect.top=SwitchStringToValue(pszString);
		ValueRect.right=SwitchStringToValue(pszString);
		ValueRect.bottom=SwitchStringToValue(pszString);

		return true;
	}

	return false;
}

//读取尺寸
bool CIniFile::ReadSize(SIZE & ValueSize, LPCTSTR pszItem, LPCTSTR pszSubItem)
{
	//效验状态
	ASSERT(m_szIniFile[0]!=0);

	//设置变量
	TCHAR szReadData[64]=TEXT("");
	ZeroMemory(&ValueSize,sizeof(ValueSize));

	//读取字符
	GetPrivateProfileString(pszItem,pszSubItem,TEXT(""),szReadData,sizeof(szReadData),m_szIniFile);

	//数据处理
	if (szReadData[0]!=0)
	{
		//读取变量
		LPCTSTR pszString=szReadData;
		ValueSize.cx=SwitchStringToValue(pszString);
		ValueSize.cy=SwitchStringToValue(pszString);

		return true;
	}

	return false;
}

//读取坐标
bool CIniFile::ReadPoint(POINT & ValuePoint, LPCTSTR pszItem, LPCTSTR pszSubItem)
{
	//效验状态
	ASSERT(m_szIniFile[0]!=0);

	//设置变量
	TCHAR szReadData[64]=TEXT("");
	ZeroMemory(&ValuePoint,sizeof(ValuePoint));

	//读取字符
	GetPrivateProfileString(pszItem,pszSubItem,TEXT(""),szReadData,sizeof(szReadData),m_szIniFile);

	//数据处理
	if (szReadData[0]!=0)
	{
		//读取变量
		LPCTSTR pszString=szReadData;
		ValuePoint.x=SwitchStringToValue(pszString);
		ValuePoint.y=SwitchStringToValue(pszString);

		return true;
	}

	return false;
}

//读取颜色
bool CIniFile::ReadColor(COLORREF & ValueColor, LPCTSTR pszItem, LPCTSTR pszSubItem)
{
	//效验状态
	ASSERT(m_szIniFile[0]!=0);

	//设置变量
	TCHAR szReadData[64]=TEXT("");
	ZeroMemory(&ValueColor,sizeof(ValueColor));

	//读取字符
	GetPrivateProfileString(pszItem,pszSubItem,TEXT(""),szReadData,sizeof(szReadData),m_szIniFile);

	//数据处理
	if (szReadData[0]!=0)
	{
		//读取变量
		LPCTSTR pszString=szReadData;
		ValueColor=RGB(SwitchStringToValue(pszString),SwitchStringToValue(pszString),SwitchStringToValue(pszString));

		return true;
	}

	return false;
}

//转换数值
LONG CIniFile::SwitchStringToValue(LPCTSTR & pszSring)
{
	//效验参数
	ASSERT((pszSring!=NULL)&&(pszSring[0]!=0));
	if ((pszSring==NULL)||(pszSring[0]==0)) return 0L;

	//寻找开始
	while (((pszSring[0]>0)&&(pszSring[0]<TEXT('0')))||(pszSring[0]>TEXT('9'))) pszSring++;

	//读取数值
	LONG lValue=0L;
	while ((pszSring[0]>=TEXT('0'))&&(pszSring[0]<=TEXT('9')))
	{
		lValue=lValue*10L+pszSring[0]-TEXT('0');
		++pszSring;
	}

	return lValue;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值