使用AddFontResource使用临时字体

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值