CreateTextFormat 메서드는 fontFamilyName 매개 변수를 필요로합니다. 기본 UI 글꼴을 사용하는 IDWriteTextFormat은 어떻게 만듭니 까?DirectWrite에서 기본 UI 글꼴로 텍스트를 그리는 방법은 무엇입니까?
1
A
답변
2
여기에있는 모든 코드는 아무런 점검없이 수행됩니다 (이 예제에서는 너무 많은 메소드가 HRESULT를 반환하므로이 예제가 부 풀릴 수 있습니다).
시스템 전체 글꼴을 획득하기위한이를 사용해야합니다
을 (이것은 또 다른 유래 질문에서입니다!)
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!
비트를 this 질문을 변경했습니다.
이 솔루션은 정말 원시이며 제 생각에는 못생긴 것입니다. (윈도우 10
조차 확인하지 않고//just the same as above except the hfont, instead use
NONCLIENTMETRICS ncm;
ncm.cbSize = sizeof(ncm);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 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"%s\n", 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);
은, 코드는 아무 문제없이 내 컴퓨터에서 실행하고 나에게 최초의 솔루션과 같은 결과를 제공합니다
대체 솔루션이 IDWriteFont을받을 수 있나요 (추한 보이지만 괜찮습니다) , 글꼴 이름은 Segoe UI입니다.
출처 : General Microsoft DirectWrite API documentation
는 "기본 UI 글꼴"또는 응용 프로그램에서 사용되는 글꼴과 시스템 글꼴을 의미합니까? –
시스템 글꼴 (기본 Windows 응용 프로그램의 UI 요소에 사용되는 글꼴)을 의미합니다. – eyelash
SystemParametersInfo/SPI_GETNONCLIENTMETRICS를 호출하고 거기에 반환 된 메시지 상자 글꼴을 사용합니다. –