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