绘制形状C的路径#

时间:2018-05-16 10:02:04

标签: c# timer

所以我基本上已将一个图片框设置到某个位置(61,361),并且我在计时器中有以下代码,所以无论何时启用它都会增加x轴和y轴的位置一定量。我只是需要帮助来编码它,所以它会跟踪一条路径,如果可能的话,最好像虚线一样。在此先感谢您的帮助。它以抛物线形状移动。

private void SimulationTimer_Tick(object sender, EventArgs e)
{
  Ball.Location = new Point(Ball.Location.X + x, Ball.Location.Y - y);
}

2 个答案:

答案 0 :(得分:0)

要获得您已应用于图片框的更改后的路径 保存列表中的每个点

注意:当您编写“pictureBox”时,我假设您使用的是Forms而不是WPF

public partial class Form1 : Form
{
    private List<Point> _points; // List of Points
    private Timer _timer; // The Timer
    private Graphics _g; // The Graphics object which is responsible for drawing the anything onto the Form

    public Form1()
    {
        _points = new List<Point>();
        _timer = new Timer();
        _timer.Tick += Timer_Tick;
        InitializeComponent();
        _g = CreateGraphics();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        _timer.Start();
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        _points.Add(pictureBox1.Location); // Add the current location to the List
        // Invoke the GUI Thread to avoid Exceptions
        pictureBox1.Invoke(new Action(() =>
        {
            pictureBox1.Location = new Point(pictureBox1.Location.X + 2, pictureBox1.Location.Y + 2);
        }));
        Pen blackPen = new Pen(Color.Black, 3);
        Invoke(new Action(() =>
        {
            for (int i = 1; i < _points.Count; i++) // Start at 1 so if you hav less than 2 points it doesnt draw anything
            {
                _g.DrawLine(blackPen, _points[i - 1], _points[i]);
            }
        }));
    }
}

对于虚线,你只能绘制线的每一段,但这是你可以自己弄清楚的。

预览: enter image description here

答案 1 :(得分:0)

希望这会有所帮助:

private void SimulationTimer_Tick(object sender, EventArgs e)
{
  System.Drawing.Point current =new System.Drawing.Point();
  current =  Ball.Location;
  Ball.Location = new Point(Ball.Location.X + x, Ball.Location.Y - y);        

  PictureBox dot = new PictureBox();
  dot.BackColor = Color.Red;
  dot.Location = current;
  dot.Height= 5;
  dot.Width = 5;
  this.Controls.Add(dot);
}

您只需将上方的点修改为您需要的内容

即可