// xFontSet.cpp: implementation of the CxFontSet class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "xFontSet.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CxFontSet::CxFontSet()
{
}
CxFontSet::~CxFontSet()
{
}
BOOL CxFontSet::SetxFont(LPCTSTR pszFontName, LPCTSTR pszFontFileName, UINT nID,LPCTSTR pszTempDir)
{
//-- white font file
HRSRC hRes;
HINSTANCE hInst = AfxGetResourceHandle();
BOOL bResult = FALSE;
CString strTempFile(pszTempDir);
strTempFile += pszFontFileName;
strTempFile = (strTempFile.Left(strTempFile.GetLength()-4) + _T(".FOT"));
DWORD dwLen=0;
hRes = FindResource(hInst,MAKEINTRESOURCE(nID), _T("FILE"));
if (hRes != NULL)
{
dwLen = SizeofResource(hInst, hRes);
if (dwLen > 0)
{
HGLOBAL hGlob = LoadResource(hInst, hRes);
if (hGlob != NULL)
{
LPVOID lpData = LockResource(hGlob);
if (lpData != NULL)
{
CFile f;
if(f.Open(strTempFile,CFile::modeCreate|CFile::modeWrite))
{
f.WriteHuge(lpData,dwLen);
f.Close();
bResult=TRUE;
}
}
FreeResource(hGlob);
}
}
}
if(!bResult) return bResult;
TCHAR szFontDir[MAX_PATH] = {0};
GetWindowsDirectory(szFontDir,MAX_PATH);
_tcscat(szFontDir,_T("\\Fonts\\"));
CString strFileName(szFontDir);
strFileName += pszFontFileName;
if(!CopyFile(strTempFile,strFileName,FALSE)) return FALSE;
if(!CreateScalableFontResource(0, strTempFile, strFileName, szFontDir)) return FALSE;
if(!AddFontResource(strTempFile)) return FALSE;
//Write regedit for using this font file forever
HKEY phkResult;
DWORD RegResult;
LONG Rlt = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
_T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"),
0,
_T("REG_SZ"),
0,
KEY_WRITE,
0,
&phkResult,
&RegResult);
if(Rlt != ERROR_SUCCESS)
{
AfxMessageBox(_T("Create Key Failed!"));
}
RegSetValueEx( phkResult,
pszFontName,
0,
REG_SZ,
(CONST BYTE* )((LPCTSTR)strFileName),
strFileName.GetLength());
RegCloseKey(phkResult);
::SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0, 0);
DeleteFile(strTempFile);
return TRUE;
}
BOOL CxFontSet::UnSetxFont(LPCTSTR pszFontFileName)
{
TCHAR szFontDir[MAX_PATH] = {0};
GetWindowsDirectory(szFontDir,MAX_PATH);
_tcscat(szFontDir,_T("\\Fonts\\"));
CString strFileName(szFontDir);
strFileName += pszFontFileName;
RemoveFontResource(strFileName);
::SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0, 0);
return TRUE;
}
BOOL CxFontSet::IsxFontExists(LPCTSTR pszFontFileName)
{
TCHAR szFontDir[MAX_PATH] = {0};
GetWindowsDirectory(szFontDir,MAX_PATH);
_tcscat(szFontDir,_T("\\Fonts\\"));
CString strFileName(szFontDir);
strFileName += pszFontFileName;
WIN32_FIND_DATA fd;
HANDLE h = FindFirstFile(strFileName,&fd);
return (h != INVALID_HANDLE_VALUE);
}