开发者

Anti-aliasing text in Windows Forms

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 开发者_如何学Pythoneither 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!


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.


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.


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.


I know this is a very old question but as some people are still using WinForms (including myself) but I thought I'd add a useful bit of information here.

Setting Graphics.CompositingQuality to HighQuality in addition to TextRenderingHint to AntiAlias has a significant impact on the quality of anti-aliased text in WinForms. I have at least used this trick to produce custom high-quality Labels and other form controls. I find that without it, the quality that WinForms produces is not really up to scratch for modern UIs, and the result was significantly poorer quality than my mockups in Photoshot/Designer. With this set the text appears much smoother and crisper.

An example of how I'd implement this on a Label:

public class AntiAliasingLabel : Label
{
    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
        e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), 0, 0);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜