C ++无法在Windows 10中安装字体

时间:2017-03-03 21:57:17

标签: c++ windows fonts true-type-fonts

我的Windows C ++应用程序(Ubuntu,Roboto Regular和Roboto Thin)使用了两种字体,我尝试使用AddFontResource和AddFontResourceEx安装它们,但它在Windows 10的最新周年纪念版中也不起作用。在Windows Server 2008和2012中。 字体未安装。所以我创建了我的Font Installation C ++类。 在我的C ++ / VSNET2015函数中,我执行以下操作:

  1. 将.TTF字体文件复制到C:\ Windows \ Fonts
  2. 将.TTF字体文件添加到HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows NT \ CurrentVersion \ Fonts FontName(TrueType) - FontFile.ttf
  3. 调用AddFontResource(FontFileName)
  4. 发送WM_FONTCHANGE消息。
  5. 字体安装并且每次都有效,但是一直工作直到我重新启动Windows然后它没有。字体文件在那里,注册表项也可以在Windows字体资源管理器中显示字体,但它不能在我的应用程序中使用,并且无法从Windows字体资源管理器中删除字体,因为字体正在使用中(应用程序已关闭) )。

     /**********************************************************************/
    BOOL CInstallFont::InstallFont(CString strFontName, CString strFontFileName, BOOL bRegular)
    {
        m_strFontName = strFontName;
        m_strFontFileName = strFontFileName;
    
        if (IsFontInstalled() == TRUE)
            if (CanCreateFont(strFontName)) // try to create the font and if successful, then don't install it again
                return TRUE;
    
        /*****************************************************/
        /* Copy Font to Windows\Fonts folder                 */
        /*****************************************************/
        CPathW pwSrc;
        CPathW pwDest;
        CString strTmp, strTmp2;
    
        FolderUtils f;
    
        CString strFontsFolder = _T("");
    
        f.GetSpecialFolder(CSIDL_FONTS, strFontsFolder);
    
        strTmp2 = theApp.m_strAppFolder;
        pwSrc.Combine(strTmp2, strFontFileName);    
        pwDest.Combine(strFontsFolder, strFontFileName);
    
        BOOL bRet=CopyFile(pwSrc, pwDest, TRUE);
    
        if (bRet == FALSE)
        {
            strTmp = _T("Error: Cannot copy font file ");
            strTmp += strFontFileName;
            AfxMessageBox(strTmp, MB_ICONEXCLAMATION);
        }
        /*****************************************************/
        /* Add Font to Registry                              */
        /*****************************************************/
        CRegKey reg;
        LONG lRet=0;
    
        lRet = reg.Open(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"), KEY_READ | KEY_WRITE);
        if (lRet == ERROR_SUCCESS)
        {
            CString strFullFontName = strFontName;
    
            strFullFontName += _T(" (TrueType)");
            reg.SetStringValue(strFullFontName, strFontFileName); // For OpenType fonts you need to change the value here
        }
        reg.Close();
    
        /*****************************************************/
        //AddFontResource(strFontFileName);
        SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
    
        return TRUE;
    }
    
    /**********************************************************************/
    int CALLBACK CInstallFont::EnumFontFamExProc(ENUMLOGFONTEX* /*lpelfe*/, NEWTEXTMETRICEX* /*lpntme*/, int /*FontType*/, LPARAM lParam)
    {
        LPARAM* l = (LPARAM*)lParam;
        *l = TRUE;
        return TRUE;
    }
    
    /**********************************************************************/
    /* Test to see if the font can be created                             */
    BOOL CInstallFont::CanCreateFont(CString strFontName)
    {
        CFont font;
    
        BOOL bRet=font.CreateFont(
            16,                        // nHeight
            0,                         // nWidth
            0,                         // nEscapement
            0,                         // nOrientation
            FW_NORMAL,                   // nWeight
            FALSE,                     // bItalic
            FALSE,                     // bUnderline
            0,                         // cStrikeOut
            DEFAULT_CHARSET,           // nCharSet
            OUT_DEFAULT_PRECIS,        // nOutPrecision
            CLIP_DEFAULT_PRECIS,       // nClipPrecision
            ANTIALIASED_QUALITY,       // nQuality
            DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
            strFontName);          // lpszFacename
    
        font.DeleteObject();
    
        return bRet;
    }
    
    /**********************************************************************/
    BOOL CInstallFont::IsFontInstalled()
    {
        // Get the screen DC
        CDC dc;
        if (!dc.CreateCompatibleDC(NULL))
        {
            return false;
        }
        LOGFONT lf = { 0 };
        // Any character set will do
        lf.lfCharSet = DEFAULT_CHARSET;
    
        // Set the facename to check for
        wcscpy_s(lf.lfFaceName, m_strFontName.GetLength()*2, (LPCTSTR)m_strFontName);
    
        LPARAM lParam = 0;
        // Enumerate fonts
        ::EnumFontFamiliesEx(dc.GetSafeHdc(), &lf, (FONTENUMPROC)EnumFontFamExProc, (LPARAM)&lParam, 0);
        return lParam ? TRUE : FALSE;
    }
    

    我使用以下参数调用该函数

    CInstallFont instFont;
    
    instFont.InstallFont(_T("Roboto Light"), _T("Roboto-Light.ttf"));
    instFont.InstallFont(_T("Roboto Thin"), _T("Roboto-Thin.ttf"));
    instFont.InstallFont(_T("Roboto Regular"), _T("Roboto-Regular.ttf"));
    

0 个答案:

没有答案