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.
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.