简体   繁体   中英

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

I tried to add font from memory (embedded resource) and use it for my windows form (c++/cli) application... The code is working, but when the specified font is not installed on the computer the textbox is using the default font instead of my custom font. CompatibleTextRenderingDefault is set to 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);

Both methods (AddFontFile, AddMemoryFont) available to load font into PrivateFontCollection do not fully support OpenType fonts. Use TrueType font instead.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM