Search code examples
c++mfcgdi+antialiasing

How to resolve aliased text in MFC controls


I'm currently in the second stage of development for a data management project I'm involved with; at the moment, we're in the stage of revamping the GUI (we're going for an office 2007-themed GUI).

At the moment I've only got experience with Dialog-based MFC projects, from which this migrates from, where we drew controls onto the dialog, then got handles to them using the GetDlgItem command, using the IDs of the controls.

Now, we're using SDI, instead of a dialog-based project, and thus, it's required to procedurally create the controls. I've had no problems creating and showing controls in the ChildView area; however, the text is very low quality and aliased. I googled the problem, but was unable to find anything particularly pertinent to the problem, indeed, the only thing which bore any real relevance was using GDI+ to draw anti-aliased text, which is fine for simple text, but I need MFC controls such as CEdit and CListBox.

Here is an image to help illustrate my problem: enter image description here

I am creating both the static GDI+ text and the MFC control in the OnPaint function, as follows:

void CChildView::OnPaint() 
{
    CPaintDC dc(this); // device context for painting
    Gdiplus::Graphics graphics(dc);

    static Gdiplus::FontFamily* fontFamily = new Gdiplus::FontFamily( _T("Segoe UI") );
    static Gdiplus::Font* font = new Gdiplus::Font( fontFamily, 12, Gdiplus::FontStyle::FontStyleRegular, Gdiplus::Unit::UnitPixel );
    static Gdiplus::SolidBrush* solidBrush = new Gdiplus::SolidBrush( Gdiplus::Color::RoyalBlue );

    Gdiplus::PointF point( 10, 10 );
    graphics.SetTextRenderingHint( Gdiplus::TextRenderingHint::TextRenderingHintAntiAlias );
    graphics.DrawString( _T("Hello, World!"), 13, font, point, solidBrush );

    // TODO: Add your message handler code here
    CEdit* pEditBox = new CEdit();
    pEditBox->CreateEx( WS_EX_CLIENTEDGE, _T("EDIT"), _T("Why is this text so poor?"), WS_BORDER | WS_CHILD | WS_VISIBLE, CRect(100, 100, 300, 200), this, UINT_MAX-9 );


    // Do not call CWnd::OnPaint() for painting messages
}

At the moment, I am wondering if it necessary to derive each of the controls and change their rendering methods to use GDI+ antialiased text. Hopefully their is an easier way to resolve this problem.

I will be extremely grateful for any help/advice.

Thanks in advance!

EDIT: Just for reference, I used to following to resolve my problem:

    CFont* pFont = new CFont();
    pFont->CreatePointFont( 120, _T("Segoe UI") );
    CEdit* pEditBox = new CEdit();
    pEditBox->CreateEx( WS_EX_CLIENTEDGE, _T("EDIT"), _T("Test Edit"), WS_BORDER | WS_CHILD | WS_VISIBLE, CRect(100, 100, 300, 200), this, UINT_MAX-9 );
    pEditBox->SetFont( pFont );

Alternatively, the following allows for the default font for the window to be used:

CEdit* pEditBox = new CEdit();
pEditBox->CreateEx( WS_EX_CLIENTEDGE, _T("EDIT"), _T("This text is no longer poor"), WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE, CRect(100, 100, 300, 200), this, UINT_MAX-9 );

if( ::IsWindow( pEditBox->GetSafeHwnd() ) )
{
    ::SendMessage( pEditBox->GetSafeHwnd(), WM_SETFONT, (WPARAM)(HFONT)GetStockObject(DEFAULT_GUI_FONT), FALSE );
}

Solution

  • Change the font used by the edit box, it's just the default one that looks so ugly.

    If you want to get the "standard" font, see e.g. SystemParametersInfo with SPI_GETNONCLIENTMETRICS argument. Of course, you can use any other font if you want to.