tags:

views:

89

answers:

2

Hello,

I wanto to use antialiased fonts in a C# application. I have read here how can be done with the Paint event of each form:

public class SmoothingFonts : System.Windows.Forms.Form
{
    ...

    private void InitializeComponent()
    {
        ...

        this.Paint += this.SmoothingFonts_Paint;
    }

    private void SmoothingFonts_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
        Font TextFont = new Font("Verdana", 25, FontStyle.Italic);
        e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
        e.Graphics.DrawString("Sample Text", TextFont, Brushes.Black, 20, 150);
    }
}

This could get cumbersome because we already have many forms done. Also, in every form we use several forms types. Can the setting for TextRenderingHint be set globally?


EDIT 1

The example cited, eventhough it's outdated as correctly pointed by nobugz, helped me to realise that the best TextRenderingHint to be used in this system is AntiAliasGridFit. The hardware platform is closed, so I can assure that it is the best configuration for this system will be the best for all the systems where the application will be deployed.

Can I set the default setting for the TextRenderingHint anywhere? I'm running the application in a Windows Embedded Standard (former Windows XP Embedded) image.


EDIT 2

I found that in a XP system you can set the type of TextRenderingHint used by the default by the registry. It is not a by application method, but a by user method. The registry keys are:

HKCU\Control Panel\Desktop\FontSmoothing      {2 for activating font smoothing}
HKCU\Control Panel\Desktop\FontSmoothingType  {1 for Antialiasing, 2 for ClearType}

Thanks for the support!

A: 

You can use GDI text rendering using TextRenderer if not printing it later. Other wise just right a similar class with a DrawText method with take Graphics as parameter and sets rendering hint everytime it is called

  TextRenderer.DrawText(    e.Graphics,
                            "Text",
                            font,
                            textRect,
                            ForeColor,
                            BackColor
                            );
affan
This is to write text, but we are defining the windows forms labels with the designer. Is there an easy solution for using both?
yeyeyerman
You should enable ClearType fonts on you computer that will improve string drawing all over system. Its available in desktop properties "Advance Effects" i think on xp. Basically TextRenderer uses GDI+ text rendering and normal e.Graphics.DrawString uses system render.
affan
+1  A: 

The article you linked manages to be both irrelevant and outdated. ClearTypeGridFit is already the default anti-aliasing drawing mode on machines that have ClearType enabled. And you should use the TextRenderer class for text rendering, Graphics.DrawString() has problems.

You can double-check that you are getting ClearType anti-aliasing by using SysInternals' ZoomIt utility. You should be able to see the reddish and bluish fringes drawn along the font outline when you zoom in.

Hans Passant
Thanks for the info. I understand that if I manage to set up ClearType in my system, the fonts will display OK. Any point to do that?
yeyeyerman
Not sure, you didn't describe your operating system. ClearType has been available for a long time, at least since XP I think. Try superuser.com if you have questions about enabling it.
Hans Passant
How would I set the default `TextRenderHint` to `AntiAliasGridFit`?
yeyeyerman
The default is an operating system setting. You'd have to use the Display control panel applet and disable ClearType.
Hans Passant