使用AddFontResource使用临时字体

王朝vc·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

To install unique hidden font resources follow these steps:

1)Copy the TrueType font file to a temporary file with a unique filename such as

"ttfont01.ttf" that can be owned by the instance of the application.

2)Call the CreateScalableFontResource() function to create a uniquely named temporary

hidden font resource file that can also be owned by the instance of the application.

3)Call the AddFontResource() function to install this uniquely named font resource file for

this instance of the application.

4)Use the font in the application as desired.

5)When the instance of the application terminates or is otherwise finished with the font

file,it should uninstall the font resource by calling the RemoveFontResource() function

until it fails.

6)Lastly,the instance of the application should delete the temporary font resource file and

the temporary TrueType font file that it created.

the code maybe such as:

CString sSourceDir = "D:\\TT6\\"; //temporary dir

//.TTF file name.

CString sFontFileName = "TT6.TTF"; //temporary font file

//Font description (as it will appear in Control Panel).

CString sFontName = "金梅粗圓體(TrueType)";

TCHAR sWinDir [MAX_PATH];

GetWindowsDirectory(sWinDir,MAX_PATH);

CString sFontDir(sWinDir);

sFontDir += "\Fonts\";

CString sFOTFile = sFontDir;

sFOTFile += (sFontFileName.Left(sFontFileName.GetLength()-4) + ".FOT");

//Copying file is used to install font file

CopyFile((sSourceDir + sFontFileName),(sFontDir + sFontFileName),FALSE);

BOOL Result;

Result = CreateScalableFontResource(0, sFOTFile, sFontFileName, sFontDir);

Result = AddFontResource(sFOTFile);

//Write regedit for using this font file forever

HKEY phkResult;

DWORD RegResult;

LONG Rlt = RegCreateKeyEx(HKEY_LOCAL_MACHINE,

"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts", 0, "REG_SZ",

0, KEY_WRITE, 0,&phkResult ,&RegResult);

if(Rlt != ERROR_SUCCESS)

{

AfxMessageBox("Create Key Failed!");

}

RegSetValueEx(phkResult, sFontName, 0, REG_SZ, (CONST BYTE*

)((LPCTSTR)sFontFileName), sFontFileName.GetLength());

RegCloseKey(phkResult);

::SendMessage (HWND_BROADCAST, WM_FONTCHANGE, 0, 0);

DeleteFile((sFontDir + sFontFileName));

论坛对应的帖子:http://community.csdn.net/Expert/topic/3955/3955083.xml?temp=.2840235

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航