Winforms System::Drawing::Text::PrivateFontCollection未安装字体时不工作

Winforms System::Drawing::Text::PrivateFontCollection未安装字体时不工作,winforms,fonts,c++-cli,privatefontcollection,Winforms,Fonts,C++ Cli,Privatefontcollection,我尝试从内存(嵌入式资源)添加字体,并将其用于我的windows窗体(c++/cli)应用程序。。。代码正在运行,但当计算机上未安装指定字体时,文本框将使用默认字体而不是我的自定义字体。CompatibleTextRenderingDefault设置为true System::Drawing::Text::PrivateFontCollection^ privateFont = gcnew System::Drawing::Text::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);
System::Drawing::Text::PrivateFontCollection^privateFont=gcnew System::Drawing::Text::PrivateFontCollection();
IO::Stream ^fontStream=Reflection::Assembly::GetExecutionGassembly()->GetManifestResourceStream(“textfont.otf”);
数组^fontData=gcnew数组(fontStream->Length);
fontStream->Read(fontData,0,(int)fontStream->Length);
fontStream->Close();
pin_ptr fontAddress=&fontData[0];
privateFont->AddMemoryFont((IntPtr)fontAddress,fontData->Length);
此->文本框\u用户名->Font=gcnew System::Drawing::Font(safe_cast(privateFont->Families[0]),9.74999f,System::Drawing::FontStyle::Bold);
此->文本框\密码->Font=gcnew System::Drawing::Font(安全强制转换(privateFont->Families[0]),9.749999F,System::Drawing::FontStyle::Bold);
可用于将字体加载到PrivateFontCollection的两种方法(AddFontFile、AddMemoryFont)都不完全支持OpenType字体。改用TrueType字体


另请参见

您希望发生什么?操作系统不知道该字体,因此它使用默认的系统字体作为备用。@Igor感谢您的快速回复。有没有办法从资源中加载字体并在不安装的情况下使用它?是否使用Qt?我不知道如何在那里使用..net和GDI+渲染来使用内存字体。。我在这里找到了这个方法,我知道它应该适用于未安装的字体,因为我从内存加载了.otf文件。