System::Drawing::Text::PrivateFontCollection 未安装字体时无法使用

System::Drawing::Text::PrivateFontCollection not working when the font is not installed

我尝试从内存(嵌入式资源)添加字体并将其用于我的 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);

可用于将字体加载到 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