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.youkuaiyun.com/Expert/topic/3955/3955083.xml?temp=.2840235
本文介绍了在应用程序中安装和卸载独特隐藏字体资源的步骤,包括复制字体文件、创建和添加字体资源、使用字体、卸载字体资源以及删除临时文件等,还给出了示例代码,并提供了论坛对应帖子链接。
1360





