System :: Drawing :: Text :: PrivateFontCollection在未安装字体时不起作用

时间:2016-05-02 15:29:48

标签: winforms fonts c++-cli privatefontcollection

我尝试从内存中添加字体(嵌入式资源)并将其用于我的Windows窗体(c ++ / cli)应用程序......代码正常工作,但是当计算机上没有安装指定的字体时,文本框正在使用默认字体而不是我的自定义字体。 CompatibleTextRenderingDefault设置为true。

    System::Drawing::Text::PrivateFontCollection^ privateFont = gcnew System::Drawing::Text::PrivateFontCollection();

    IO::Stream^ fontStream = Reflection::Assembly::GetExecutingAssembly()->GetManifestResourceStream("textfont.otf");
    array<Byte>^ fontData = gcnew array<Byte>(fontStream->Length);
    fontStream->Read(fontData, 0, (int)fontStream->Length);
    fontStream->Close();

    pin_ptr<byte> fontAddress = &fontData[0];
    privateFont->AddMemoryFont((IntPtr)fontAddress, fontData->Length);

    this->TextBox_Username->Font = gcnew System::Drawing::Font(safe_cast<System::Drawing::FontFamily^>(privateFont->Families[0]), 9.749999F, System::Drawing::FontStyle::Bold);
    this->TextBox_Password->Font = gcnew System::Drawing::Font(safe_cast<System::Drawing::FontFamily^>(privateFont->Families[0]), 9.749999F, System::Drawing::FontStyle::Bold);

1 个答案:

答案 0 :(得分:0)

可用于将字体加载到PrivateFontCollection的两种方法(AddFontFile,AddMemoryFont)都不完全支持OpenType字体。请改用TrueType字体。

另见https://social.msdn.microsoft.com/Forums/en-US/40834a16-2378-4aeb-a499-25f835fe5738/opentype-font-as-embedded-resource?forum=winforms