c++direct2ddirectwrite

Text drawn with DWrite is blurry


I was recently developing my own GUI library. The window is created using Win32 API and then Direct2D RenderTarget was created inside. All the drawing (buttons, labels, etc.) happens inside the RenderTarget. Everything is fine except for the quality of the text. When I look at visual studio buttons for example, the text looks so clear compared to DirectWrite method DrawTextW().

Here is an image example:

image

I use DirectWrite to directly draw text. `

ID2D1SolidColorBrush* brush;
RenderTarget->CreateSolidColorBrush(D2D1::ColorF(red, green, blue, alpha), &brush);

IDWriteTextFormat* format;
HRESULT h = WriteFactory->CreateTextFormat(std::wstring(font.begin(), font.end()).c_str(), NULL, fontWeight,
    fontStyle, DWRITE_FONT_STRETCH_NORMAL, fontSize, L"", &format);

// Center the text horizontally and vertically.
format->SetTextAlignment(textAllignment);
format->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);

// Draw text
RenderTarget->DrawTextW(std::wstring(text.begin(), text.end()).c_str(), std::wstring(text.begin(), text.end()).size(), format, D2D1::RectF(xPos, yPos, xPos+width, yPos+height), brush);

brush->Release();
format->Release();

`

I was just wondering, is this something that I should just accept and move on or do I have to tweak something with DWriteFactory?


Solution

  • It appears that you have high-DPI monitor and haven't defined proper manifest for your executable. By default Windows will think that your app is designed for old 96ppi systems and so each "pixel" will span four physical pixels if your monitor uses 192ppi for example.

    Check this for how to add high-DPI awareness to your application.

    In Visual Studio 2015 and above there is a flag in project settings: "Configuration Properties > Manifest Tool > Input and Output > DPI Awareness"