tags:

views:

227

answers:

3

I have a WinForms form, and would like to make it so that all text (e.g. labels, buttons) is anti-aliased. This is driving me crazy because I can't find anything on google, which either means it's so obvious or I'm way off-base.

My best idea has been to override OnPaint in my main form, but this doesn't seem to change anything.

protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
    base.OnPaint(e);
}

Help!

A: 

If I remember correctly, anti-aliasing only works when calling DrawString on the Graphics object, after you set the anti-aliasing mode as you described in your answer.

Standard WinForms controls don't use this method, so a simple override won't work.

Gerrie Schenck
Actually, overriding OnPaint like hvjackson did for the form but for a user control subclassing the Button class has some effects.
Sorin Comanescu
All depends on how the control draws itself I guess.
Gerrie Schenck
A: 

This is because what you did only affects text drawn directly on the form. I guess you would have to override OnPaint in user controls subclassing labels, buttons, etc.

Sorin Comanescu
I guess I just assumed that after painting itself, the Form would pass the same Graphics instance on to its sub-controls to paint themselves with, and so by setting the TextRenderingHint before anything gets painted, it would carry through. But basically you're saying that each control is painted with its own Graphics instance, so there is no way to do this globally, short of subclassing every control I use?
Henry Jackson
Pretty sure the form and contained controls don't share the same graphics context.
Sorin Comanescu
+1  A: 

It sounds like you don't want anti-aliasing so much as sub-pixel rendering which is used by ClearType. More specifically you may want to check out the TextRenderingHint Enumeration member ClearTypeGridFit.

Afcrowe