簡體   English   中英

未安裝字體時,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/zh-CN/40834a16-2378-4aeb-a499-25f835fe5738/opentype-font-as-embedded-resource?forum=winforms

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM