简体   繁体   English

C#形状的绘制路径

[英]Draw Path Of Shape C#

So I basically have have a picturebox set to a certain location (61, 361) and I have the below code in a timer, so whenever it is enabled it will increment the location of the x and y axis with a certain amount. 因此,我基本上将图片框设置为某个位置(61、361),并且在计时器中添加了以下代码,因此无论何时启用它,它都会将x和y轴的位置增加一定数量。 I just need help to code it so it will trace a path preferably like a dotted line if possible. 我只需要帮助对其编码,以便在可能的情况下最好跟踪一条虚线。 Thanks in advance for the help. 先谢谢您的帮助。 It moves in a parabolic shape btw. 它以抛物线形移动。

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

To achieve a path following the changes you have applied to your picturebox you save each point in a List 要在您应用于图片框的更改之后获得一条路径,请将每个点保存在一个列表中

NOTE: As you wrote "pictureBox" i assumed you are using Forms and not WPF 注意:当您编写“ pictureBox”时,我假设您使用的是窗体而不是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]);
            }
        }));
    }
}

For a dotted Line you could only draw every second segment of the Line, but that's something you can figure out yourself. 对于虚线,您只能绘制线的第二部分,但是您可以自己弄清楚。

Preview: 预习: 在此处输入图片说明

Hope this helps: 希望这可以帮助:

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);
}

you can just modify the dot above to what you need 您可以将上面的点修改为所需的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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