Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
SQL
i trying in winforms to draw an ellipse with mouse event ,
when mouse is down ,move and up my code goes like this :


C#
   void panel1_MouseDown(object sender, MouseEventArgs e)
{
    startpoint.X = e.X;
    startpoint.Y = e.Y;

    if (m_drawrectiangle == true)
    {
        m_shape = new Rectiangle(0, 0, this);
        m_shape.Xpos = e.X;
        m_shape.Ypos = e.Y;
        draw = true;
    }
        if (m_draweliipse == true)
        {
            m_shape = new Circle(0, 0, this);
            m_shape.Xpos = e.X;
            m_shape.Ypos = e.Y;
            draw = true;

        }

}
void panel1_MouseUp(object sender, MouseEventArgs e)
{

    if (m_shape != null)
    {
        if(m_shape.Area()==0)
        {
            this.Cursor = Cursors.Default;
            draw = false;
        }
        if (draw == true)
        {
            shapes.Add(m_shape);
            this.Cursor = Cursors.Default;
            draw = false;
            Invalidate();
        }
    }
}
void panel1_MouseMove(object sender, MouseEventArgs e)
{

    this.Text = "(" + e.X + ", " + e.Y + ")";

    if (draw==true  && m_drawrectiangle==true )
    {
        int x = (e.X < 0) ? 0 : e.X;
        int y = (e.Y < 0) ? 0 : e.Y;

        //switch places
        m_shape.Xpos = (x < startpoint.X) ? x : startpoint.X;
        m_shape.Ypos = (y < startpoint.Y) ? y : startpoint.Y;

       ( (Rectiangle)m_shape).Width = Math.Abs(x - startpoint.X);
      ( (Rectiangle) m_shape).Height = Math.Abs(y - startpoint.Y);


        Invalidate(); //re-paint
    }
    if ( draw==true && m_draweliipse==true )
    {
        int x = (e.X < 0) ? 0 : e.X;
        int y = (e.Y < 0) ? 0 : e.Y;

        //switch places
        m_shape.Xpos = (x < startpoint.X) ? x : startpoint.X;
        m_shape.Ypos = (y < startpoint.Y) ? y : startpoint.Y;
    double deltax=Math.Pow(x-startpoint.X,2);
    double deltay=Math.Pow(y-startpoint.Y,2);
    ((Circle)m_shape).Diam =(decimal)Math.Sqrt(deltax +deltay);//typecast safe
        Invalidate(); //re-paint

    }
}

my main problem is that when i save the circle or rectinagle in the list, everytime i drawing a new circle/rectiangle ,the shape in the list Flickering... here my Onpaint event:
C#
protected override void OnPaint(PaintEventArgs e)
{

    if(draw==true)
    {
        m_shape.draw();
    }
    if (shapes.Count > 0)
    {
        foreach (shape a in shapes)
        {
            a.draw();
            if (m_drawarea == true)
            {
                string text1 = a.Area().ToString("0. 00");
                Font font1 = new Font("Arial", 8, FontStyle.Bold, GraphicsUnit.Point);
                using (Graphics rectangledraw = CreateGraphics())
                {
                    rectangledraw.DrawString(text1, font1, Brushes.Blue, (float)a.Xpos, (float)a.Ypos);

                }
            }

        }
    }


also here is my circle class the draw part:
C#
   public override void draw()
        {
            using (Graphics rectangledraw = m_canavs.CreateGraphics())
            {
                RectangleF rectF1 = new RectangleF(Xpos, Ypos ,(float)m_diam, (float)m_diam);
                rectangledraw.DrawEllipse(new Pen(Color.Black,2),Rectangle.Round(rectF1));
            }
        }
    }
}

can anybody tell me what i am doing wrong ? because i have no idea what to do?
also doublebuffer only get it worse...
Posted

1 solution

Flickering can be prevented by using double buffering: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doublebuffered%28v=vs.110%29.aspx[^].

Do you understand why? Should be pretty obvious.

Are you saying that double buffering only makes it worse? It should not. But, as I can see, you do it wrong. Totally wrong. Your main mistake is using Graphics rectangledraw = m_canavs.CreateGraphics(). You should not do it. Instead, you should use the instance o Canvas passed to you through the argument PaintEventArgs e of OnPaint.

Fix it, and, changes are, it will work for you. Maybe you made some other mistakes, but I cannot see any right now.

By the way, this is just silly:
C#
if (draw==true  && m_drawrectiangle==true ) //...

This is the same as
C#
if (draw && m_drawrectiangle) //...

No, the problem is not making code shorter, the problem is lack of understanding of what you are checking and the role of Boolean.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900