【问题标题】:System::Drawing::Text::PrivateFontCollection not working when the font is not installed未安装字体时 System::Drawing::Text::PrivateFontCollection 不起作用
【发布时间】:2016-08-27 10:27:56
【问题描述】:

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

【问题讨论】:

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

标签: winforms fonts c++-cli privatefontcollection


【解决方案1】:

可用于将字体加载到 PrivateFontCollection 的两种方法(AddFontFile、AddMemoryFont)不完全支持 OpenType 字体。请改用 TrueType 字体。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    相关资源
    最近更新 更多