如何绘制按钮运动的轨迹?

时间:2018-10-21 06:46:29

标签: c# winforms trail

我正在尝试制作一个通过trackbar控制3个不同按钮的移动的应用程序。

按钮以圆形的方式向中心点移动。我打算做的是绘制按钮运动的轨迹:每次按钮更改其“着陆”像素时的位置颜色。

我正在使用WFA项目C#进行编码。

这是按钮移动的代码:

private void trackBar1_Scroll(object sender, EventArgs e)
    {
        //161, 114
        double sinX, cosX, sinY, cosY, sinZ, cosZ;
        sinX = Math.Sin(trackBar1.Value / 57.29);
        cosX = Math.Cos(trackBar1.Value / 57.29);
        sinX = Math.Truncate((360 - trackBar1.Value)/4 * sinX);
        cosX = Math.Truncate((360 - trackBar1.Value) / 4 * cosX);
        button1.Location = new System.Drawing.Point(161 + (int)sinX, 114 + (int)cosX);

        sinY = Math.Sin((trackBar1.Value + 120) / 57.29);
        cosY = Math.Cos((trackBar1.Value + 120)/ 57.29);
        sinY = Math.Truncate((360 - trackBar1.Value) / 4 * sinY);
        cosY = Math.Truncate((360 - trackBar1.Value) / 4 * cosY);
        button2.Location = new System.Drawing.Point(161 + (int)sinY, 114 + (int)cosY);

        sinZ = Math.Sin((trackBar1.Value + 240) / 57.29);
        cosZ = Math.Cos((trackBar1.Value + 240) / 57.29);
        sinZ = Math.Truncate((360 - trackBar1.Value) / 4 * sinZ);
        cosZ = Math.Truncate((360 - trackBar1.Value) / 4 * cosZ);
        button3.Location = new System.Drawing.Point(161 + (int)sinZ, 114 + (int)cosZ);

    } 

Moves like this towards a center point.

1 个答案:

答案 0 :(得分:0)

在您的表单中生成可以保存所有要点的文件 列表跟踪=新的List();

将按钮的第一个位置保存在构造函数的列表中:

public MyForm()
{
    InitializeComponent();
    // Make sure you put this code after the InitializeComponent:
    this.track.Add(button3.Location)
}

然后在移动事件中将按钮的位置存储在该列表中:

private void trackBar1_Scroll(object sender, EventArgs e)
{
    //rest of your code
    button3.Location = new System.Drawing.Point(161 + (int)sinZ, 114 + (int)cosZ);
    this.track.Add(button3.Location)
} 

,然后按如下所示处理Form.Paint方法:

private void MyForm_Paint(object sender, PaintEventArgs e)
{
    Pen pen = new Pen(Color.Red);
    foreach(Point point in track)
    {
        Rectangle rect = new Rectangle(point, new Size(1,1));
        e.Graphics.DrawRectangle(pen, rect);
    }
}