gdi/gdiplus如何加载字体

对于gdi画法:

云风的博客上这么说(http://blog.codingnow.com/2006/01/font.html):

以前做游戏想用隶书,但是并非每台机器上都装有这个字体,所以有时候需要给用户提供一个。在自己软件的路径下放上字体文件,直接调用 CreateFont 这个 API 是不认的。

这种情况下可以使用 AddFontResource ,然后调用
PostMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0); 就可以了。
当不用这个字体时再调用 RemoveFontResource 卸掉。

 

BOOL InstallFont(void)
{
	//检查字体是否安装
	LOGFONT lf={0};
	lf.lfCharSet = GB2312_CHARSET;
	HWND hDesktop = ::GetDesktopWindow();
	HDC hDC=::GetDC(hDesktop);
	EnumFontFamiliesEx(hDC,&lf,(FONTENUMPROC)EnumFontProc,0,0);
	::ReleaseDC(hDesktop,hDC);
	if (!g_bExistFont) //不存在字体 安装字体
	{
		const TCHAR AryFontFileName[][16] = 
		{
			ZLT("msyh.ttf"),		//微软雅黑
			ZLT("msyhbd.ttf"),	//微软雅黑粗体
		};
		const long lFontCount = ARRAYSIZE(AryFontFileName);
		CString strWindowsFontsPath = CrnGetSpecialFolder(CSIDL_FONTS);
		TCHAR szSrcPath[MAX_PATH] = {0}, szDscPath[MAX_PATH] = {0};
		for (long i = 0; i < lFontCount; ++i)
		{
			wsprintf(szSrcPath, ZLT("%sconfig\\font\\%s"), __main_home__, AryFontFileName[i]);
			wsprintf(szDscPath, ZLT("%s\\%s"), strWindowsFontsPath, AryFontFileName[i]);
			
			if(::AddFontResource(szSrcPath)!=0)
				//::PostMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
			if(!::CopyFile(szSrcPath, szDscPath, FALSE))
			{
				AfxMessageBox(ZLT("安装微软雅黑字体失败,程序可能无法正常运行!"));
			}
		}
	}
	return TRUE;
}


 

对于gdiplus:

使用PrivateFontCollection.AddFontFile()方法加载字体,然后得到FontFamliy来创建gdi+ font

BOOL UIPlusFont::LoadFont(LPCTSTR lpszFontFilePath, LPTSTR lpszFontFamilyName, PrivateFontCollection& pfc)
{
	if(lpszFontFilePath==NULL || lpszFontFilePath[0]==_T('') || lpszFontFamilyName==NULL || lpszFontFamilyName[0]==_T(''))
		return FALSE;
	int                   nFound = 0;
    int                   nCount = 0;
    FontFamily*           pFontFamily = NULL;

 
	pfc.AddFontFile(lpszFontFilePath);
 
    nCount = pfc.GetFamilyCount();
    pFontFamily = (FontFamily*)new FontFamily[nCount];
    pfc.GetFamilies(nCount, pFontFamily, &nFound);
    if(nFound<=0)
	    return FALSE;
   
    for(int j = 0; j < nFound; ++j)
    {
       pFontFamily[j].GetFamilyName(lpszFontFamilyName);
    }
	
    delete[] pFontFamily;
	pFontFamily = NULL;
	
	if(_tcscmp(lpszFontFamilyName, ZLT("微软雅黑")) != 0)
		return FALSE;
	else
		return TRUE;
}


 

UIPlusFont::REF_TYPE UIPlusFont::Create(long lSize,long lFontStyle,LPCTSTR lpName)
{
	LOGFONT logFont= {lSize,lFontStyle};
	CovertToLocalFontName(logFont.lfFaceName,lpName);

	FontFamily fontFamily(logFont.lfFaceName);
	Font* pFont = new Font(&fontFamily,(Gdiplus::REAL)lSize,lFontStyle,UnitPixel);
    
	if (pFont->GetLastStatus()!=Ok) 
	{
		//delete pFont;
		//pFont = NULL;
		TCHAR szAppPath[MAX_PATH] = {0};
		::GetModuleFileName(NULL, szAppPath, MAX_PATH);
		for(int l=lstrlen(szAppPath);l>0&&szAppPath[l]!=ZLT('\\');szAppPath[l--]=0);
		TCHAR szFontFilePath[MAX_PATH] = {0};
		wsprintf(szFontFilePath, ZLT("%sfont\\MSYH.TTF"), szAppPath);
		
		PrivateFontCollection pfc;
		TCHAR szFamilyName[32] = {0};
		LoadFont(szFontFilePath, szFamilyName, pfc);
		pFont = new Font(szFamilyName,(Gdiplus::REAL)lSize,lFontStyle,UnitPixel, &pfc);
		if(pFont->GetLastStatus()!=Ok)
		{
			FontFamily fontFamily(ZLT("宋体"));
			pFont = new Font(&fontFamily,(Gdiplus::REAL)lSize,lFontStyle,UnitPixel);
			UMyChartLog::LogError(ZLT("[UIPlusFont::Create] lSize=%d,lFontStyle=%d,Error=%d"),lSize,lFontStyle,pFont->GetLastStatus());
			//return UIPlusFont::REF_TYPE(NULL);
		}
	}

	UIPlusFont::REF_TYPE pRefFont=UIPlusFont::Create(pFont);
	g_UIPlusFontCache.insert(std::pair<LOGFONT,UIPlusFont::REF_TYPE>(logFont,pRefFont));

	return pRefFont;
}


 

你可能感兴趣的:(gdi/gdiplus如何加载字体)