⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 property.cpp

📁 上传的是有关mfc的详细介绍
💻 CPP
字号:
// Property.cpp: implementation of the CProperty class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Property.h"

#include "ItemKey.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


void CProperty::SetDefaultFont(LOGFONT &font)
{
	font.lfHeight		= 0;		// 以逻辑单位指定字体字符元(character cell)或字符的高度
	font.lfWidth		= 0;		// 以逻辑单位指定字体字符的平均宽度
	font.lfEscapement	= 0;		// 以十分之一度为单位指定每一行文本输出时相对于页面底端的角度
	font.lfOrientation	= 0;		// 以十分之一度为单位指定字符基线相对于页面底端的角度
	font.lfWeight		= 700;		// 指定字体重量。
	font.lfItalic		= 0;		// 当lfItalic为TRUE时使用斜体
	font.lfUnderline	= 0;		// 当lfUnderline为TRUE时给字体添加下划线
	font.lfStrikeOut	= 0;		// 当lfStrikeOut为TRUE时给字体添加删除线
	font.lfCharSet		= 0;		// 指定字符集
	font.lfOutPrecision	= 0;		// 指定输出精度
	font.lfQuality		= 0;		// 定义输出质量 DEFAULT_QUALITY 
									// (默认质量)DRAFT_QUALITY (草稿质量)PROOF_QUALITY (正稿质量)

	font.lfClipPrecision	= 0;	// 指定剪辑精度
	font.lfPitchAndFamily	= 0;		// 指定字体的字符间距和族
	strcpy(font.lfFaceName, TEXT(""));

}

void CProperty::SetDefaultContrlStruct(CreateWndStruct &controlStruct)
{
	controlStruct.dwExStyle			= 0;
	controlStruct.lpszName			= TEXT("");
	controlStruct.style				= WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_OWNERDRAW;
	controlStruct.hInstance			= GetModuleHandle(NULL);//CBaseWnd::g_hInst;
	controlStruct.lpCreateParams	= NULL;
}

CProperty::CProperty()
{
	// 设置属性的默认值
	//this->m_chValue		= NULL;					// 存储配置信息的字符串
	this->m_FocColor	= RGB(255,255,255);		// 获得焦点后的背景颜色
	this->m_EnbColor	= RGB(170,160,180);		// 控件不可用时的颜色
	this->m_ForColor	= RGB(39, 102, 203);	// 前台颜色
	this->m_BgColor		= RGB(170,160,180);		// 背景颜色
	this->m_strImgSrc	= "";		// 背景图路径
	this->m_nImgIndex	= 1;		// 背景图索引
	this->m_nImgCount	= 1;		// 背景图图数
	this->m_bIsShow		= true;		// 控件是否显示
	this->m_strText		= "";		// 控件文本
	this->m_bIsBold		= true;		// 字体是否粗体
	this->m_bIsEnable	= false;	// 控件是否不可用
	this->m_TextFormat	= DT_SINGLELINE|DT_VCENTER|DT_CENTER;	// 文本的现实位置

	// 创建控件时的默认样式
	CProperty::SetDefaultContrlStruct(this->m_WndStruct);

	// 控件文本的默认字体
	CProperty::SetDefaultFont(this->m_logfont);
}

CProperty::~CProperty()
{
}

// 根据配置信息设置控件属性值
void CProperty::SetValue(char *chValue, char* key)
{
	if (!chValue)
	{
		return;
	}

	// 将配置文本存储起来,用到时就不用再从配置文件中读取了。当前该属性没有用到
	//this->m_chValue = new char[strlen(chValue) + 1];
	//::strcpy(this->m_chValue, chValue);

	// 根据配置文件中的信息配置控件的属性
	m_strKey = CString(key);											// 控件主键	
	this->GetColor(chValue, m_BgColor, ItemKey::CONTROL_BGCOLOR);		// 背景颜色	
	this->GetColor(chValue, m_ForColor, ItemKey::CONTROL_FORCOLOR);		// 前台颜色	
	this->GetColor(chValue, m_FocColor, ItemKey::CONTROL_FOCCOLOR);		// 获得焦点后的背景颜色
	this->GetCString(chValue, m_strImgSrc, ItemKey::CONTROL_IMGSRC);	// 获得背景图片的路径
	this->GetImgInfo(chValue, m_nImgCount, m_nImgIndex);				// 背景图片的路径,个数及索引号	
	this->GetCString(chValue, m_strType, ItemKey::CONTROL_TYPE);		// 控件类型
	this->GetCString(chValue, m_strText, ItemKey::CONTROL_TEXT);		// 控件文本
	this->GetRect(chValue, m_Rect, ItemKey::CONTROL_RECT);			    // 控件位置信息
	
	int nIsShow;														// 是否显示
	this->GetInt(chValue, nIsShow, ItemKey::CONTROL_ISSHOW);
	if (nIsShow == 0)
	{
		m_bIsShow = false;
	}

	int nIsBold;													// 字体是否粗体
	this->GetInt(chValue, nIsBold, ItemKey::CONTROL_BOLD);
	if (nIsBold == 0)
	{
		m_bIsBold = false;
	}

	this->m_WndStruct.lpszClass = TEXT(this->m_strType);

	// 按钮
	if (this->m_strType == "button")
	{
		this->m_WndStruct.nWndType	= 1;
		this->m_WndStruct.dwstyle	=  WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_OWNERDRAW;
	}
	// 标签
	else if (this->m_strType == "static")
	{
		this->m_WndStruct.nWndType	=  3;
		this->m_WndStruct.dwstyle	=  WS_CHILD|WS_VISIBLE|WS_EX_CLIENTEDGE|WS_BORDER;
	}/*
	// 文本框
	else if (this->m_strType == "edit")
	{
		this->m_WndStruct.nWndType	=	2;
		this->m_WndStruct.dwstyle	=  WS_CHILD|WS_VISIBLE|ES_LEFT;
	}*/
	// 其他
	else
	{
		this->m_WndStruct.nWndType	= 1;
		this->m_WndStruct.dwstyle	=  WS_CHILD|WS_VISIBLE|WS_EX_CLIENTEDGE|WS_BORDER;
	}
}

// 取得字符串中某特定字符串第一次出现时的指针
void CProperty::GetSpeCharPtr(char *chValue, char* &chSpe, char* key)
{
	int len = strlen(key);
	chSpe = ::strstr(chValue, key);
	if (chSpe == NULL)
	{
		return;
	}
	chSpe = chSpe + len;
}

// 从配置文件中读取控件的颜色
void CProperty::GetColor(char *chValue, COLORREF &color, char* key)
{
	char* chSpeValue = NULL;
	this->GetSpeCharPtr(chValue, chSpeValue, key);
	if (chSpeValue != NULL)
	{		
		int r = 0;
		int g = 0;
		int b = 0;
		sscanf(chSpeValue, ("=%d,%d,%d;%*"), &r, &g, &b);
		if (r >= 0 && g >= 0 && b >= 0)
		{
			color = RGB(r, g, b);
		}
	}
}

// 从配置文件中读取一个字符串
void CProperty::GetCString(char *chValue, CString &strSrc, char* key)
{
	char* chSpeValue = NULL;
	this->GetSpeCharPtr(chValue, chSpeValue, key); 
	if (chSpeValue == NULL)
	{
		return;
	}

	char chBmpPath[512];
	::memset(chBmpPath, 0, 512);
	::sscanf(chSpeValue, ("=%[^;]s;%*"), chBmpPath);
	ToolKit::TrimForCharArray(chBmpPath);
	strSrc.Format("%s", chBmpPath);
}

// 从配置文件中读取背景图片的信息
void CProperty::GetImgInfo(char *chValue, int &nCount, int &nIndex)
{
	nCount = 1;
	nIndex = 1;

	char* chSpeValue = NULL;
	this->GetSpeCharPtr(chValue, chSpeValue, ItemKey::CONTROL_IMG_NUM);

	if (chSpeValue == NULL)
	{
		return;
	}
	::sscanf(chSpeValue, ("=%d,%d;%*"), &nCount, &nIndex);
}

// 从配置文件中读取一个整数
void CProperty::GetInt(char *chValue, int &nInt, char* key)
{
	char* chSpeValue = NULL;
	this->GetSpeCharPtr(chValue, chSpeValue, key);

	if (chSpeValue == NULL)
	{
		return;
	}
	::sscanf(chSpeValue, ("=%d;%*"), &nInt);
}

// 从配置文件中读取控件的位置信息
void CProperty::GetRect(char *chValue, RECT &rect, char* key)
{
	char* chSpeValue = NULL;
	this->GetSpeCharPtr(chValue, chSpeValue, key);
	if (chSpeValue == NULL)
	{
		return;
	}
	::sscanf(chSpeValue, "=%d,%d,%d,%d;%*", &rect.left, &rect.right, &rect.top, &rect.bottom);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -