0
点赞
收藏
分享

微信扫一扫

WTL资源管理(皮肤管理)

janedaring 2022-04-30 阅读 50

概述:

概述:
CSkinManager 主要是实现皮肤管理和颜色管理,颜色主要使用如下格式:
default.color

defalt_color=255,53,88,133
tool_color_one=255,53,88,133

default.skin 该文件是default.zip 改了后缀而来的。

代码实现如下:

SkinManager.h 头文件的实现

#ifndef     SKINMANAGER_H
#define     SKINMANAGER_H

#include <set>
#include <map>
#include "ResourceManager.h"
#include "pch.h"

class CSkinManager
{
private:

    /**
    *@method  	CSkinManager
    *@brief   	CSkinManager类的构造函数
    *    
    *@param   	void
    *@return  	
    */
    CSkinManager(void);

    /**
    *@method  	~CSkinManager
    *@brief   	CSkinManager类的析构函数
    *    
    *@param   	void
    *@return  	
    */
    ~CSkinManager(void);

    CSkinManager& operator=( CSkinManager& );

    CSkinManager( const CSkinManager& );

public:

    
	/**
	*@brief    返回皮肤管理类实例
	*@return   CSkinMgr&
	*@retval   皮肤管理类实例
	*/
	static CSkinManager& instance( void )
	{
        static CSkinManager skinMgr;
		return skinMgr;
	}


    /**
    *@method  	Init
    *@brief   	初始化,获取有多少个皮肤文件
    *    
    *@param   	void
    *@return  	void
    */
    void Init( void );

    /**
    *@method  	SetCurrentSkinStyle
    *@brief   	设置当前皮肤
    *    
    *@param   	const std::wstring & wstrSkin,当前皮肤
    *@return  	bool,成功返回true,失败返回false
    */
    bool SetCurrentSkinStyle( const std::wstring& wstrSkin );

    /**
    *@method  	GetCurrentSkinStyle
    *@brief   	获取当前皮肤
    *    
    *@param   	std::wstring & wstrSkin,当前皮肤
    *@return  	void
    */
    void GetCurrentSkinStyle( std::wstring& wstrSkin );

    /**
    *@method  	GetAllSkinStyleCount
    *@brief   	获取皮肤目录下的数量
    *    
    *@param   	void
    *@return  	UINT
    */
    UINT GetAllSkinStyleCount( void );

    /**
    *@method  	GetAllSkinStyle
    *@brief   	获取所有的皮肤
    *    
    *@param   	std::vector<std::wstring>& wstrSkin,皮肤数组
    *@return  	void
    */
    void GetAllSkinStyle( std::vector<std::wstring>& wstrSkin );
    
    /**
    *@method  	Load
    *@brief   	加载图片压缩文件
    *    
    *@param   	wchar_t * szImageZipFile,图片压缩文件名
    *@return  	void
    */
    void LoadImageFile( const std::wstring& wstrImageZipFile );

    /**
    *@method  	LoadColorFile
    *@brief   	加载颜色资源
    *    
    *@param   	const std::wstring & wstrImageZipFile
    *@return  	void
    */
    void LoadColorFile( const std::wstring& wstrImageZipFile );

    /**
    *@method  	String2ARGB
    *@brief   	将字符串转换为ARGB,字符串格式为(“A,R,G,B”),A、R、G、B
    *@          是0~到255的数字
    *    
    *@param   	std::wstring str
    *@return  	DWORD
    */
    DWORD String2ARGB( const std::wstring& str  ) const;

    /**
    *@method  	Unload
    *@brief   	卸载已经加载的资源
    *    
    *@return  	void
    */
    void Unload();

    /**
    *@method  	LoadImage
    *@brief   	打开Image文件
    *    
    *@param   	const std::wstring & wstrUri,Image文件的名称
    *@param   	Image * & pImg,返回的Image对象指针
    *@return  	void
    */
    bool GetImage( const std::wstring& wstrUri, Image *&pImg );

    /**
    *@method  	GetColor
    *@brief   	获取颜色值(Color)
    *    
    *@param   	const std::wstring wstrUri,颜色值的标识
    *@param   	DWORD & dwArgb,颜色值(ARGB格式)
    *@return  	extern "C" void RESMGR_API
    */
    bool GetColor
        (
            const std::wstring& wstrUri,
            DWORD& dwArgb
        );
    

    /**
    *@method  	GetColor
    *@brief   	获取颜色值(Color)
    *    
    *@param   	const std::wstring & wstrUri
    *@param   	BYTE & a
    *@param   	BYTE & r
    *@param   	BYTE & g
    *@param   	BYTE & b
    *@return  	void
    */
    bool GetColor
        (
            const std::wstring& wstrUri,
            BYTE& a,
            BYTE& r,
            BYTE& g,
            BYTE& b
        );

private:
    std::set<std::wstring> m_setSkinStyle;     //  所有皮肤风格
    typedef std::set<std::wstring>::iterator SkinStyleIter;
    std::wstring   m_CurrentSkinStyle;     //  当前皮肤风格

    std::map<std::wstring, Image*>   m_mapImage;         //  图片管理矩阵
    typedef std::map<std::wstring, Image*>::iterator  ImageManagerIter;

    std::map<std::wstring, DWORD>   m_mapColor;         //  颜色值管理矩阵
    typedef std::map<std::wstring, DWORD>::iterator  ColorManagerIter;

    void DeleteTempFile( void );
};

#endif

SkinManager.cpp 头文件的实现。


#define _CRT_SECURE_NO_WARNINGS

#include "ResourceManagerPublic.h"
#include "SkinManager.h"
#include "LanguageManager.h"
#include <Windows.h>
#include <Shlwapi.h>
#include <wchar.h>
#include "unzip.h"
#include "UiPublicDefine.h"

#include <algorithm>
#include <assert.h>
#include <fstream>

#pragma comment(lib, "gdiplus.lib")

/**
*@method  	CSkinManager
*@brief   	CSkinManager类的构造函数
*    
*@param   	void
*@return  	
*/
CSkinManager::CSkinManager(void)
{
}


/**
*@method  	~CSkinManager
*@brief   	CSkinManager类的析构函数
*    
*@param   	void
*@return  	
*/
CSkinManager::~CSkinManager(void)
{
	Unload( );
}


/**
*@method  	Init
*@brief   	初始化,获取有多少个皮肤文件
*    
*@param   	void
*@return  	void
*/
void CSkinManager::Init( void )
{
	// 取得皮肤文件搜索路径
	std::wstring strSkinSearchPath = GetSkinPath() + L"\\*" + IMAGEFILE_EXT;

	HANDLE hFind = NULL;
	WIN32_FIND_DATA FindFileData;
	memset( &FindFileData, 0, sizeof(FindFileData) );

	hFind = FindFirstFile( strSkinSearchPath.c_str(), &FindFileData );  
	if (INVALID_HANDLE_VALUE == hFind) 
	{
		//RESMGR_E_LOG(L"Open image file failed!!!" );
		return;
	} 

	// 遍历Skin目录,查询有多少个皮肤风格
	do 
	{
		std::wstring wstrFileName = FindFileData.cFileName;
		size_t nPos = wstrFileName.find(IMAGEFILE_EXT);
		if ( (nPos != std::wstring::npos) && ((nPos +wcslen(IMAGEFILE_EXT))==wstrFileName.length()) )
		{
			wstrFileName = wstrFileName.substr(0, nPos);
			STRING_MAKELOWER(wstrFileName)
				m_setSkinStyle.insert(wstrFileName);
		}
	} while (FindNextFile(hFind, &FindFileData));

	FindClose( hFind );
	hFind = NULL;
}


/**
*@method  	SetCurrentSkinStyle
*@brief   	设置当前皮肤
*    
*@param   	const std::wstring & wstrSkin,当前皮肤
*@return  	bool,成功返回true,失败返回false
*/
bool CSkinManager::SetCurrentSkinStyle( const std::wstring& wstrSkin )
{
	//  如果和当前皮肤一样,就不用设置了。
	if ( m_CurrentSkinStyle == wstrSkin )
	{
		//RESMGR_I_LOG(L"The Skin be set is the current skin!!!" );
		return true;
	}

	std::wstring strTemp = wstrSkin;
	STRING_MAKELOWER(strTemp);

	SkinStyleIter iter = m_setSkinStyle.find(strTemp);
	if ( m_setSkinStyle.end() != iter )
	{
		m_CurrentSkinStyle = strTemp;

		//  卸载旧的皮肤图片
		Unload( ); 

		m_CurrentSkinStyle = wstrSkin;

		//  加载新的皮肤图片
		std::wstring wstrNewImgZipFile = wstrSkin + IMAGEFILE_EXT;
		LoadImageFile( wstrNewImgZipFile );

		//  加载新的颜色值
		std::wstring wstrNewColorFile = wstrSkin + COLORFILE_EXT;
		LoadColorFile( wstrNewColorFile );

		return true;
	}
	else
	{
		//  设置的皮肤文件不存在
		//RESMGR_E_LOG(L"The Skin be set isn't existed!!!" );
		return false;
	}

}


/**
*@method  	GetCurrentSkinStyle
*@brief   	获取当前皮肤
*    
*@param   	std::wstring & wstrSkin,当前皮肤
*@return  	void
*/
void CSkinManager::GetCurrentSkinStyle( std::wstring& wstrSkin )
{
	wstrSkin = m_CurrentSkinStyle;
}


/**
*@method  	GetAllSkinStyleCount
*@brief   	获取皮肤目录下的数量
*    
*@param   	void
*@return  	UINT
*/
UINT CSkinManager::GetAllSkinStyleCount( void )
{
	return (UINT)m_setSkinStyle.size();
}


/**
*@method  	GetAllSkinStyle
*@brief   	获取所有的皮肤
*    
*@param   	std::vector<std::wstring>& wstrSkin,皮肤数组
*@return  	void
*/
void CSkinManager::GetAllSkinStyle( std::vector<std::wstring>& wstrSkin )
{
	SkinStyleIter iter = m_setSkinStyle.begin();
	for (; iter != m_setSkinStyle.end(); ++iter)
	{
		wstrSkin.push_back((*iter));
	}
}

/**
*@method  	Load
*@brief   	加载图片压缩文件
*    
*@param   	wchar_t * szImageZipFile,图片压缩文件名
*@return  	void
*/
void CSkinManager::LoadImageFile( const std::wstring& wstrImageZipFile )
{

	SYSTEMTIME stSysTime;
	ZeroMemory( &stSysTime, sizeof( &stSysTime ) );
	GetLocalTime( &stSysTime ); 



	if( 0 == wstrImageZipFile.length() )
	{
		//RESMGR_E_LOG(L"Parame wstrImageZipFile is empty!!!" );
		return;
	}

	std::wstring wstrFile = GetSkinPath() + L"\\" + wstrImageZipFile;

	HZIP hZip = OpenZip( wstrFile.c_str(), NULL );
	if ( NULL == hZip )
	{
		//  打开皮肤zip文件错误,写日志
		//RESMGR_E_LOG(L"Open skin file failed!!!" );
		return;
	}

	//  获取图片的数量
	ZIPENTRY ze; 
	memset( &ze, 0, sizeof(ZIPENTRY) );
	GetZipItem( hZip, -1, &ze); 
	int nItems = ze.index;

	std::wstring wstrTemp;//= GetSkinPath() + L"\\Temp";
	{
		TCHAR lpPathBuffer[4096];
		GetTempPath(4096,				lpPathBuffer); 
		wstrTemp = lpPathBuffer;
		wstrTemp += L"\\Icool";;
	}


	for( int i = 0; i < nItems; ++i )
	{
		ZIPENTRY zeItem; 
		memset( &zeItem, 0, sizeof(zeItem) );
		GetZipItem( hZip, i, &zeItem );
		std::wstring wstrTempFile = wstrTemp + L"\\" + zeItem.name;

		STRING_MAKELOWER( wstrTempFile )
			if ( std::wstring::npos != wstrTempFile.find( L".gif") )
			{    
				UnzipItem( hZip, i, wstrTempFile.c_str() );

				Image *pImg = Image::FromFile( wstrTempFile.c_str() );
				Image img(wstrTempFile.c_str());

				if ( NULL != pImg )
				{ 
					std::wstring wstrImgName = zeItem.name;
					//  需要将文件名统一转换为小写
					STRING_MAKELOWER( wstrImgName )
						m_mapImage[wstrImgName] = pImg;
				} 
			} 
			else
			{   
				HGLOBAL hMem = GlobalAlloc( GMEM_FIXED, zeItem.unc_size );
				if ( NULL != hMem )
				{
					BYTE* pMem = (BYTE*)GlobalLock( hMem );
					memset( pMem, 0, zeItem.unc_size );
					UnzipItem( hZip, i, pMem, zeItem.unc_size );

					IStream* pIStream = NULL;
					HRESULT hr = CreateStreamOnHGlobal( hMem, FALSE, &pIStream );
					if ( S_OK == hr )
					{
						Image *pImg = Image::FromStream( pIStream );
						std::wstring wstrImgName = zeItem.name;

						//  需要将文件名统一转换为小写
						STRING_MAKELOWER( wstrImgName )
							m_mapImage[wstrImgName] = pImg;
						pIStream->Release();
					}
					else    //  CreateStreamOnHGlobal失败
					{
						//  CreateStreamOnHGlobal创建IStream错误,写日志

					}

					pMem = NULL;
					GlobalUnlock(hMem);
					GlobalFree(hMem);
					hMem = NULL;

				}
				else    //  GlobalAlloc失败
				{
				}
			}

	}

	//DeleteTempFile();

	CloseZip( hZip );

	SYSTEMTIME stSysTimeEnd;
	ZeroMemory( &stSysTimeEnd, sizeof( &stSysTimeEnd ) );
	GetLocalTime( &stSysTimeEnd ); 


	long nMS = stSysTimeEnd.wMilliseconds - stSysTime.wMilliseconds;
	nMS += (stSysTimeEnd.wSecond - stSysTime.wSecond)*1000;
	nMS += (stSysTimeEnd.wMinute - stSysTime.wMinute)*1000*60;
	nMS += (stSysTimeEnd.wHour - stSysTime.wHour)*1000*60*60;
	//RESMGR_I_LOG_1( L"unzip pay time is %d ms.", nMS );


	hZip = NULL;
}

/**
*@method  	LoadColorFile
*@brief   	加载颜色资源
*    
*@param   	const std::wstring & wstrImageZipFile
*@return  	void
*/
void CSkinManager::LoadColorFile( const std::wstring& wstrColorFile )
{
	if( 0 == wstrColorFile.length() )
	{
		//RESMGR_E_LOG(L"Parame wstrColorFile is empty!!!" );
		return;
	}

	//  构筑字符串文件名
	std::wstring wstrFileName = GetSkinPath()+L"\\"+wstrColorFile;

	FILE* fp;
	fp = _wfopen( wstrFileName.c_str(), L"r" );
	if( NULL == fp )
	{
		//RESMGR_E_LOG(L"Open color file failed!!!" );
		return;
	}

	std::fstream ifs(fp);
	std::string strLine;
	while( std::getline(ifs, strLine))
	{
		std::wstring wStr = String_A2W(strLine);
		size_t nEqulePos = wStr.find( '=' );
		if ( std::wstring::npos != nEqulePos )
		{
			std::wstring wstrId = wStr.substr( 0, nEqulePos );
			std::wstring wstrStr = wStr.substr
				( 
				nEqulePos+1, 
				wStr.length() 
				);
			m_mapColor[wstrId] = String2ARGB( wstrStr );
		}
		else
		{
			//读取的字符串错误
			//RESMGR_E_LOG(L"LRead string from color file Failed!!!" );
		}

	}

	ifs.close();
	fclose(fp);
	fp = NULL;
}


/**
*@method  	String2ARGB
*@brief   	将字符串转换为ARGB,字符串格式为(“A,R,G,B”),A、R、G、B
*@          是0~到255的数字
*    
*@param   	std::wstring str
*@return  	DWORD
*/
DWORD CSkinManager::String2ARGB( const std::wstring& str ) const
{
	if( 0 == str.length() )
	{
		//RESMGR_E_LOG(L"Function String2ARGB Parame str is empty!!!" );
		return 0;
	}

	DWORD dwRes = 0;
	DWORD dwA = 0;
	DWORD dwR = 0;
	DWORD dwG = 0;
	DWORD dwB= 0;
	size_t nPos = 0;
	std::wstring strItem;
	std::wstring strTemp = str;

	//  转换 
	nPos = strTemp.find(COMPART_SIGN);
	if ( std::wstring::npos != nPos )
	{
		strItem = strTemp.substr( 0, nPos );
		dwA = _wtol( strItem.c_str() );
		strTemp = strTemp.substr( nPos+1, strTemp.length() );
	}

	//  转换字体大小fSize
	nPos = strTemp.find(COMPART_SIGN);
	if ( std::wstring::npos != nPos )
	{
		strItem = strTemp.substr( 0, nPos );
		dwR = _wtol( strItem.c_str() );
		strTemp = strTemp.substr( nPos+1, strTemp.length() );
	}

	//  转换字体风格nStyle
	nPos = strTemp.find(COMPART_SIGN);
	if ( std::wstring::npos != nPos )
	{
		strItem = strTemp.substr( 0, nPos );
		dwG = _wtol( strItem.c_str() );
		strTemp = strTemp.substr( nPos+1, strTemp.length() );
	}

	//  转换字体风格nStyle
	dwB = _wtol( strTemp.c_str() );

	dwRes = ( (DWORD)dwA << 24 ) | ( (DWORD)dwR << 16 ) |( (DWORD)dwG << 8 ) | dwB;

	return dwRes;
}

/**
*@method  	Unload
*@brief   	卸载图片压缩文件
*    
*@return  	void
*/
void CSkinManager::Unload()
{
	for 
		( 
		ImageManagerIter iter = m_mapImage.begin(); 
	iter != m_mapImage.end(); 
	++iter 
		)
	{
		delete iter->second;
		iter->second = NULL;
	}

	m_mapImage.clear( );
	m_mapColor.clear();

	DeleteTempFile();
}

/**
*@method  	LoadImage
*@brief   	打开Image文件
*    
*@param   	const std::wstring & wstrUri,Image文件的名称
*@param   	Image * & pImg,返回的Image对象指针
*@return  	void
*/
bool CSkinManager::GetImage( const std::wstring& wstrUri, Image *&pImg )
{
	if( 0 == wstrUri.length() )
	{
		//RESMGR_E_LOG(L"Function GetImage Param wstrUri is empty!!!" );
		return false;
	}

	//  需要将文件名统一转换为小写
	std::wstring wstrImgName = wstrUri;
	STRING_MAKELOWER(wstrImgName)

		pImg = m_mapImage[wstrImgName];
	if ( NULL == pImg )
	{
		//  写日志
		//RESMGR_E_LOG(L"The image Getted isn't existed!!!" );
		return false;
	}

	return true;
}

/**
*@method  	GetColor
*@brief   	获取颜色值(Color)
*    
*@param   	const std::wstring wstrUri,颜色值的标识
*@param   	DWORD & dwArgb,颜色值(ARGB格式)
*@return  	extern "C" void RESMGR_API
*/
bool CSkinManager::GetColor
(
 const std::wstring& wstrUri,
 DWORD&  dwArgb
 )
{
	if( 0 == wstrUri.length() )
	{
		//RESMGR_E_LOG(L"Function GetColor Parame wstrUri is empty!!!" );
		return false;
	}

	ColorManagerIter iter = m_mapColor.find( wstrUri );
	if ( m_mapColor.end() != iter )
	{
		dwArgb = m_mapColor[wstrUri];

		return true;
	}
	else
	{
		//  获取的颜色不存在
		//RESMGR_E_LOG(L"The color Getted isn't existed!!!" );
		return false;
	}
}

/**
*@method  	GetColor
*@brief   	获取颜色值(Color)
*    
*@param   	const std::wstring & wstrUri
*@param   	BYTE & a
*@param   	BYTE & r
*@param   	BYTE & g
*@param   	BYTE & b
*@return  	void
*/
bool CSkinManager::GetColor
(
 const std::wstring& wstrUri,
 BYTE& a,
 BYTE& r,
 BYTE& g,
 BYTE& b
 )
{
	if( 0 == wstrUri.length() )
	{
		//RESMGR_E_LOG(L"Function GetColor Parame wstrUri is empty!!!" );
		return false;
	}

	DWORD dwArgb = 0;
	ColorManagerIter iter = m_mapColor.find( wstrUri );
	if ( m_mapColor.end() != iter )
	{
		dwArgb = m_mapColor[wstrUri];

		Gdiplus::Color cr(dwArgb);
		a = cr.GetA();
		r = cr.GetR();
		g = cr.GetG();
		b = cr.GetB();

		return true;
	}
	else
	{
		//  获取的颜色不存在
		//RESMGR_E_LOG(L"The color Getted isn't existed!!!" );
		return false;
	}
}

void CSkinManager::DeleteTempFile( void )
{
	std::wstring wstrTemp = GetSkinPath() + L"\\Temp";

	WIN32_FIND_DATA findData;
	memset( &findData, 0, sizeof(findData) );
	std::wstring wstrSearch = wstrTemp + L"\\*.*";
	std::vector<std::wstring> vtFileList;
	HANDLE hFind = FindFirstFile( wstrSearch.c_str(), &findData );
	if ( INVALID_HANDLE_VALUE != hFind )
	{
		do 
		{
			if ( ( 0 != _tcscmp( findData.cFileName, _T("..") ) ) && 
				( 0 != _tcscmp( findData.cFileName, _T(".")  ) )
				)
			{
				std::wstring wstrFileName = wstrTemp + L"\\" + findData.cFileName;
				//DeleteFile( wstrFileName.c_str() );
				vtFileList.push_back(wstrFileName);
			}
		} while ( FindNextFile(hFind, &findData ) );

		FindClose (hFind);
		hFind = INVALID_HANDLE_VALUE;
	}

	std::vector<std::wstring>::iterator iter = vtFileList.begin();
	for ( ; iter != vtFileList.end(); ++iter )
	{
		SetFileAttributes( iter->c_str(), FILE_ATTRIBUTE_NORMAL );
		DeleteFile( iter->c_str() );
	}

	::RemoveDirectory( wstrTemp.c_str() );

}

举报

相关推荐

0 条评论