Wednesday, April 2, 2008

How do I disable font anti-aliasing (font smoothing) for a window?

This can be accomplished by creating a font with the NONANTIALIASED_QUALITY flag in the fdwQuality parameter. Then set the font by sending the WM_SETFONT message.
void DisableFontSmoothing(HWND hwnd)
{
HFONT font;

font = CreateFont(10, 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
NONANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
"MS Sans Serif");
SendMessage(hwnd, WM_SETFONT, (LPARAM)font, 0);
}
Be sure that you do not delete the font or else it will revert back. If you do not know what the font parameters will be at compile time, then you could modify this function to use WM_CHOOSEFONT_GETLOGFONT and WM_CHOOSEFONT_SETLOGFONT messages with a command to set the NONANTIALIASED_QUALITY flag in between.

No comments: