Click here to Skip to main content
15,909,193 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi I need a code for rectangle move and arc or other thing
I've written this code in For the object must move

protected override void OnPaint(PaintEventArgs paintEvent)
{
    // get graphics object

    SolidBrush brush = new SolidBrush(Color.Blue);
    Pen pen = new Pen(Color.AliceBlue);
    Rectangle rectangle1 = new Rectangle( 400, 250, 25, 25 );//rectangle for circle
    g.DrawRectangle(pen, 30, 20, 450, 280);
    g.DrawArc(pen, rectangle1, 0, 360);//circle

}

private void button1_Click(object sender, EventArgs e)
{
    for (int i = 0; i < 100; i++)
         //move

}
Posted

1 solution

All you have to do is change the fixed coordinates in your DrawRectangle to variables, and alter the variables.:

private int rectLeft = 20;
private int rectTop = 30;

        protected override void OnPaint(PaintEventArgs paintEvent)
        {
            Graphics g = paintEvent.Graphics;
            Pen pen = new Pen(Color.AliceBlue);
            g.DrawRectangle(pen, rectLeft, rectTop, 450, 280);
            pen.Dispose();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
        rectLeft += 20;
        rectTop += 10;
        Invalidate();
        }
(I cut your code down a little to make it clearer)

Note that you are responsible for disposing any pens, brushes, etc. you create! Failure to do so will result in odd seeming problems...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Apr-11 0:36am    
Correct, a 5.
--SA

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