简体   繁体   中英

How to move a ball drawn with graphics in C#

hey guys I have a SplitContainer on a WindowsForms object on my Bounce Game project. I used the Panel child of splitContainer1 to draw a circle inside a pictureBox1 inside the second panel of the splitContainer1. Here is the method that I used

 private void Panel2_Paint(object sender, PaintEventArgs e)
        {
             //use the panel's paint event to paint
            //specify smoothing properties
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.FillEllipse(Brushes.Blue, pictureBox1.ClientRectangle);

        }

The product of the above code is shown below在此处输入图像描述 I am trying to move the picture box down with respects to the button click but nothing happens. Here is the button click move code

private void button1_Click(object sender, EventArgs e)
        {
            //Console write the location of the picture box
            Console.WriteLine(pictureBox1.Location.X + " " + pictureBox1.Location.Y);
            /*define a new Point for the picturebox based on its current coords
              notice that X is constant as I am just trying to move the container down
              along the y axis*/
            Point point= new Point(pictureBox1.Location.X,pictureBox1.Location.Y+5);
            // notice that I have only adjusted the Y co ordinate
            //reassign the new location to the picture box
            pictureBox1.Location = point;
        }

NB:

It looks like the control position is moving so I need to clear all the elements drawn inside the picture box and redraw

I used the pictureBox paint event to draw the circle inside it and then update the location of the picturebox as a control. A delegate method solved the issue for me..

public Form1()
        {
            InitializeComponent();
            //disable maximization of the form
            this.MaximizeBox = false;
            //make sure the picture box is at the center of the form
            this.StartPosition = FormStartPosition.CenterScreen;
            pictureBox1.Paint += (o, e) =>
            {
                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
                e.Graphics.FillEllipse(Brushes.Green,pictureBox1.ClientRectangle);
            };
        }

and then move the control with this method

 private void button1_Click(object sender, EventArgs e)
        {
            //Console write the location of the picture box
            MessageBox.Show(pictureBox1.Location.X + " " + pictureBox1.Location.Y);
            /*define a new Point for the picturebox based on its current coords
             notice that X is constant as I am just trying to move the container down
             along the y axis*/
            Point point= new Point(pictureBox1.Location.X,pictureBox1.Location.Y-5);
            // notice that I have only adjusted the Y co ordinate
            //reassign the new location to the picture box
            pictureBox1.Location = point;
            //redraw the ball inside the new location of the picture box
             
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM