Anna Marie Rapa
Anna Marie Rapa

Reputation: 119

Rectangle moving with mouse

I am implementing a winform application and I need a rectangle moving around with the pointer. I am creating it on timer_tick as below. The only problem is that it is flickering. How can I eliminate the flickering?

 private void timer1_Tick(object sender, EventArgs e)
    {
      //Gets the Mouse position on the X-axis
        int posX = MousePosition.X;
        //Gets the mouse poisiton on the Y-Axis
        int posY = MousePosition.Y;

        Graphics g = Graphics.FromHwnd(IntPtr.Zero);

        mouseNewRect = new Rectangle(new Point(posX, posY), new
               Size(500, 500));
        g.DrawRectangle(new Pen(Brushes.Chocolate), mouseNewRect);
        this.Refresh();
    }

Upvotes: 0

Views: 107

Answers (1)

Anthony
Anthony

Reputation: 9571

Just to add more detail to my comment above, here's a sample control which demonstrates how to setup double-buffering as well as how to hook into the paint event handling properly.

class CustomControl : Control
{
    private bool mShouldDrawMouseRect;
    private Rectangle mMouseRect;

    public CustomControl()
    {
        this.mMouseRect.Width = 500;
        this.mMouseRect.Height = 500;

        this.SetStyle(
            ControlStyles.DoubleBuffer |
            ControlStyles.ResizeRedraw |
            ControlStyles.AllPaintingInWmPaint, 
            true);
    }

    protected override void OnMouseEnter(EventArgs e)
    {
        // No actual painting done here, just updating fields.
        this.mShouldDrawMouseRect = true;
        this.Invalidate();
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        // No actual painting done here, just updating fields.
        this.mShouldDrawMouseRect = false;
        this.Invalidate();
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        // No actual painting done here, just updating fields.
        this.mMouseRect.X = e.X;
        this.mMouseRect.Y = e.Y;
        this.Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        // ALL painting stays in the paint event, using the graphics provided
        if (this.mShouldDrawMouseRect)
        {
            // If you're just using named colors, we can access the static Pen objects.
            e.Graphics.DrawRectangle(Pens.Chocolate, this.mMouseRect);

            // If you create your own Pen, put it in a Using statement.
            // Including the following commented example:

            // using (var pen = new Pen(Color.Chocolate))
            // {
            //     e.Graphics.DrawRectangle(pen, this.mMouseRect);
            // }
        }
    }
}

Upvotes: 1

Related Questions