2

The control below draws a string in a rectangle. On mouse move there is a hit test on the string rectangle, and the string is redrawn via CreateGraphics. The irritating problem is that the text is not drawn the same as in the Paint handler; it appears to be displaced by about 1 pixel, and the effect is like a bold font. How can I create a graphics object exactly like the one in the Paint handler so the text is drawn the same way? Ordinarily you would invalidate and redraw everything in the Paint event, but I have potentially hundreds of of other drawing items and only want to draw the string. Should I try to do any drawing outside of the Paint event or is this a mistake?

Example control:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Test.TestModes
{
    public partial class ExampleControl: UserControl
    {
        private const string testString = "0123456789";
        private RectangleF stringRect = new RectangleF(10, 10, 100, 20);

        public ExampleControl()
        {
            InitializeComponent();
        }

        private void ExampleControl_Paint(object sender, PaintEventArgs e)
        {
            Font font = new Font("Arial", 12, FontStyle.Regular);

            e.Graphics.DrawString(testString, font, Brushes.Black, stringRect);

            font.Dispose();
        }

        private void DrawString(bool hit)
        {
            Font font = new Font("Arial", 12, FontStyle.Regular);

            using(Graphics g = CreateGraphics())
            {
                g.SetClip(ClientRectangle);
                if(hit)
                    g.DrawString(testString, font, Brushes.Red, stringRect);
                else
                    g.DrawString(testString, font, Brushes.Black, stringRect);
            }

            font.Dispose();
        }

        private void ExampleControl_MouseMove(object sender, MouseEventArgs e)
        {
            if(stringRect.Contains(e.Location))
                DrawString(true);
            else
                DrawString(false);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Invalidate();
        }


    }
}
5
  • 1
    Why are you using CreateGraphics anyway? Do all of your painting inside of the Pain event, that is what it is there for. Commented Jan 14, 2010 at 22:03
  • How would you only draw the string and not the rest of the control? Commented Jan 14, 2010 at 22:07
  • I would start by debuggin the PaintEventArgs' ClipRectangle's location & the ClientRectangle in the DrawString method and see if everything is matching up... Commented Jan 14, 2010 at 22:09
  • Just call Invalidate on the control, with the rectangle to be refreshed as the argument. msdn.microsoft.com/en-us/library/8dtk06x2.aspx Commented Jan 14, 2010 at 22:12
  • -- Then in Paint event do I test each drawing element and only draw if in the clip area? Commented Jan 14, 2010 at 22:26

1 Answer 1

9

It is the CreateGraphics() call that is getting you in trouble, indirectly. The problem is anti-aliasing of the text. A normal painting cycle erases the background before drawing something on top. That doesn't happen in your case, your draw text on top of existing text. The side effect is that the pixels uses to create the aliasing get darker each time your draw. The end result is bold looking, and noticeably jagged text outlines.

The fix is easy: start with a clean slate before you draw:

    using (Graphics g = CreateGraphics()) {
        g.Clear(this.BackColor);                <=== added
        g.SetClip(ClientRectangle);
        // etc..
    }

You'll now also get to encounter a problem in drawing known as "flicker". It might not yet be that noticeable yet, but it will when you do more drawing. Flicker is suppressed with double-buffering. A feature supported by Windows Forms, but only if you use standard drawing techniques. In other words: no CreateGraphics().

Sign up to request clarification or add additional context in comments.

2 Comments

Excellent, that's it. I am losing the DoubleBuffered effect however when I do this and getting some flickering as I move the mouse around. (I added DoubleBuffered = true in the load event). So I guess I will never be able to draw outside of the Paint event.
Well, you can double-buffer yourself. Use a Bitmap, Graphics.FromImage() and draw into that graphics context. Then DrawImage() the result. Still, just calling Invalidate() to let the regular Paint() event handler draw is better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.