Createtextformat方法需要一个fontFamilyName参数。 如何创build使用默认UI字体的IDWritetextformat?
Pango / Cairo / Fontconfig:在Windows中使Unicode字体回退/链接正常工作
C#FontFamily不显示新的字体
以X11位图字体呈现string
Windows(IE和Firefox)上的像素化字体(font-face)
当通过SSL访问网页时,IE8和IE9中的字体不会加载
请注意,这里的所有代码都是在没有任何检查的情况下完成的(这里有太多的方法返回HRESULT,会吹起这个例子!)。
要获取系统宽字体,您应该使用以下命令:
(这是从另一个stackoverflow问题!)
NONCLIENTMETRICS ncm; ncm.cbSize = sizeof(ncm); SystemParametersInfo(SPI_GETNONCLIENTMETRICS,ncm.cbSize,&ncm,0); HFONT hFont = CreateFontIndirect(&(ncm.lfMessageFont)); //
对于油漆现在使用这个:
HDC hdc = BeginPaint(...); //Creates a device context SelectObject(hdc,hFont); //Your font is Now set for the current device context //do something DeleteObject(hFont); //Don't forget to do this at the end!
有点改变这个问题!
这个解决方案真的很粗糙,在我看来是丑陋的。
替代解决方案获得IDWriteFont(看起来很丑但很好):
//just the same as above except the hfont,instead use NONCLIENTMETRICS ncm; ncm.cbSize = sizeof(ncm); SystemParametersInfo(SPI_GETNONCLIENTMETRICS,0); IDWriteFactory *dwriteFactory_; DWriteCreateFactory( DWRITE_FACTORY_TYPE_SHARED,__uuidof(IDWriteFactory),reinterpret_cast<IUnkNown**>(&dwriteFactory_) ); IDWriteGdiInteroP* gdiInterop = NULL; dwriteFactory_->GetGdiInterop(&gdiInterop); IDWriteFont* sys_font = nullptr; gdiInterop->CreateFontFromLOGFONT(&ncm.lfMessageFont,&sys_font); //Now we have it! //The text format can Now be aquired like this //We need the font family of our font IDWriteFontFamily* family = nullptr; sys_font->GetFontFamily(&family); //Now we have to get the "localized" name of our family IDWriteLocalizedStrings* font_family_name = nullptr; family->GetFamilyNames(&font_family_name); UINT32 index = 0; UINT32 length = 0; BOOL exists = false; font_family_name->FindLocaleName(L"en-us",&index,&exists); font_family_name->GetStringLength(index,&length); wchar_t* name = new wchar_t[length + 1]; font_family_name->GetString(index,name,length + 1); wprintf(L"%sn",name); //Some user defined stuff DWRITE_FONT_WEIGHT font_weight = DWRITE_FONT_WEIGHT_BLACK; DWRITE_FONT_STYLE font_style = DWRITE_FONT_STYLE_ITALIC; DWRITE_FONT_STRETCH font_stretch = DWRITE_FONT_STRETCH_EXPANDED; IDWritetextformat* text_format = nullptr; dwriteFactory_->Createtextformat(name,nullptr,font_weight,font_style,font_stretch,10.0,L"en-us",&text_format);
即使没有检查,代码在我的电脑上运行没有任何问题,并给我与第一个解决方案(Windows 10,字体家族名称是Segoe UI )相同的结果。
来源: 一般Microsoft DirectWrite api文档
CreateIndirectFont文档
如何枚举字体系列,Microsoft文档
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。