none
VC6.0如何显示外部字体? RRS feed

  • 问题

  •  CFont *pFont;
     pFont=new CFont;
     CFont *pOldFont;

     pFont->CreateFont(12,0,0,0,FW_NORMAL,0,0,0,DEFAULT_CHARSET,0,0,0,0, "方正静蕾简体");
     pOldFont=dc.SelectObject(pFont);

     dc.SetBkMode(TRANSPARENT);
     dc.SetTextColor(RGB(200,0,0));
     dc.TextOut(100,140,"TEST");

     dc.SelectObject(pOldFont);

    我想用我导入系统目录C:\WINDOWS\Fonts的字体,显示“TEST”。但是不知道如何实现求高手!

    2012年2月24日 23:46

答案

  • /*
    MSDN:
    
      Any application that adds or removes fonts from the system font table should notify other windows of the change by sending a WM_FONTCHANGE message to all top-level windows in the operating system. The application should send this message by calling the SendMessage function and setting the hwnd parameter to HWND_BROADCAST. 
    
      When an application no longer needs a font resource that it loaded by calling the AddFontResource function, it must remove that resource by calling the RemoveFontResource function. 
    
      This function installs the font only for the current session. When the system restarts, the font will not be present. To have the font installed even after restarting the system, the font must be listed in the registry.
    */
    // 定义为成员变量
    CFont font;
    TCHAR szPath[MAX_PATH];
    
    // 初始化函数
    _tcscpy(szPath, _T("%s"), _T("F://11.ttf"));
    LOGFONT lf;
    lf.lfHeight = 60;
    lf.lfWidth = 30;
    lf.lfEscapement = 0;
    lf.lfOrientation = 0;
    lf.lfWeight = 90; 
    lf.lfItalic = 0; 
    lf.lfUnderline = 0;
    lf.lfStrikeOut = 0; 
    lf.lfCharSet = DEFAULT_CHARSET; 
    lf.lfOutPrecision = 0; 
    lf.lfClipPrecision = CLIP_STROKE_PRECIS;
    lf.lfQuality = 0;
    lf.lfPitchAndFamily = 0; 
    _tcscpy(lf.lfFaceName, _T("XXX")); // 这里就是字体名 
    font.CreateFontIndirect(&lf);
    ASSERT(font.GetSafeHandle());
    
    // 之后就可以调用下面的代码来设置字体了
    if(!AddFontResource(szPath))
    {
      AfxMessageBox(_T("Load font failed."));
      return ;
    }
    ::SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
    GetDlgItem(IDOK)->SetFont(&font);
    
    // 最后不需要的时候释放字体资源
    RemoveFontResource(szPath);
    
    

    给您一段参考的代码,可以加载字体文件,不过需要注意的是_tcscpy(lf.lfFaceName, _T("XXX")); 这里的字体名不要写错了,不清楚的话,用直接打开字体文件,看看Font Name:后面的具体是什么名字,不是该字体文件的文件名。

    希望上面的代码对您有帮助。


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    • 已标记为答案 yshkcj 2012年2月25日 6:53
    2012年2月25日 5:01
    版主

全部回复

  • 你看一下你的字体属于哪个字符集

    设置好字符集就可以使用了

    也就是DEFAULT_CHARSET这个参数


    新浪微博http://weibo.com/xianglitian,欢迎围观

    2012年2月25日 1:02
  • 依旧行不通哟。
    2012年2月25日 1:30
  • /*
    MSDN:
    
      Any application that adds or removes fonts from the system font table should notify other windows of the change by sending a WM_FONTCHANGE message to all top-level windows in the operating system. The application should send this message by calling the SendMessage function and setting the hwnd parameter to HWND_BROADCAST. 
    
      When an application no longer needs a font resource that it loaded by calling the AddFontResource function, it must remove that resource by calling the RemoveFontResource function. 
    
      This function installs the font only for the current session. When the system restarts, the font will not be present. To have the font installed even after restarting the system, the font must be listed in the registry.
    */
    // 定义为成员变量
    CFont font;
    TCHAR szPath[MAX_PATH];
    
    // 初始化函数
    _tcscpy(szPath, _T("%s"), _T("F://11.ttf"));
    LOGFONT lf;
    lf.lfHeight = 60;
    lf.lfWidth = 30;
    lf.lfEscapement = 0;
    lf.lfOrientation = 0;
    lf.lfWeight = 90; 
    lf.lfItalic = 0; 
    lf.lfUnderline = 0;
    lf.lfStrikeOut = 0; 
    lf.lfCharSet = DEFAULT_CHARSET; 
    lf.lfOutPrecision = 0; 
    lf.lfClipPrecision = CLIP_STROKE_PRECIS;
    lf.lfQuality = 0;
    lf.lfPitchAndFamily = 0; 
    _tcscpy(lf.lfFaceName, _T("XXX")); // 这里就是字体名 
    font.CreateFontIndirect(&lf);
    ASSERT(font.GetSafeHandle());
    
    // 之后就可以调用下面的代码来设置字体了
    if(!AddFontResource(szPath))
    {
      AfxMessageBox(_T("Load font failed."));
      return ;
    }
    ::SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
    GetDlgItem(IDOK)->SetFont(&font);
    
    // 最后不需要的时候释放字体资源
    RemoveFontResource(szPath);
    
    

    给您一段参考的代码,可以加载字体文件,不过需要注意的是_tcscpy(lf.lfFaceName, _T("XXX")); 这里的字体名不要写错了,不清楚的话,用直接打开字体文件,看看Font Name:后面的具体是什么名字,不是该字体文件的文件名。

    希望上面的代码对您有帮助。


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    • 已标记为答案 yshkcj 2012年2月25日 6:53
    2012年2月25日 5:01
    版主
  • 太感谢您了!

    2012年2月25日 6:53