views:

176

answers:

1

I'm experiencing an issue with GDI+ while custom painting dashed rectangles.

The vertical portion of dashed rectangles appear as solid, continuous lines when the window size is increased or when scrolling up/down. Moving the mouse faster results in fewer and fewer solid sections. Curiously the horizontal lines do not exhibit this behaviour and appear as expected.

So far two non-optimal solutions have been to set ResizeRedraw = true or to call Invalidate() during OnResize() and OnScroll(). I'd of course like to avoid this as what I am really drawing is more complex and these slow calls destroy the fluid experience. I've also tried invalidating only the newly shown area to no avail - only a full Invalidate seems to work.

Any pointers on how to work this out?

Demo code:

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

public class Form1 : Form
{
    static void Main()
    {
        Application.Run(new Form1());
    }

    public Form1()
    {
        this.ClientSize = new System.Drawing.Size(472, 349);

        DoubleBuffered = true;
        //ResizeRedraw = true;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        int dimensions = 70;

        using ( Pen pen = new Pen(Color.Gray) )
        {
            pen.DashStyle = DashStyle.Dash;

            for ( int x = 0; x < 20; ++x )
            {
                for ( int y = 0; y < 20; ++y )
                {
                    Rectangle rect = new Rectangle(x * dimensions, y * dimensions, dimensions, dimensions);

                    e.Graphics.DrawRectangle(pen, rect);
                }
            }
        }
    }
}
+1  A: 

I think there are two problems: there appears to be an area at the edge of the window where rectangles aren't drawn correctly; and you're drawing the rectangles over each other, so the dashing won't work properly.

Replace your OnPaint loop with the following:

   for (int y = 0; y < Height; y += dimensions)
   {
       e.Graphics.DrawLine(pen, 0, y, Width, y);
   }
   for (int x = 0; x < Width; x += dimensions)
   {
       e.Graphics.DrawLine(pen, x, 0, x, Height);
   }
Mark
Thanks, that did the trick Mark. Both problems mentioned seem to hold true. The solid line appears when only drawing a single rectangle on the left edge, but not on the right. Also, any overdrawn vertical sections also display this behaviour (only the rightmost rectangle's right line does looks correct). Since my rep is too low to edit a post, can someone modify the condition in the first for loop to 'y < Height' instead of 'y < Width'?
Kevin Pullin