我如何绘制按钮移动的轨迹?

How do i draw the trail of a button movement?

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

按钮以圆周方式向中心点移动。我打算做的是绘制按钮移动的轨迹:每次按钮更改其位置颜色时,它 "lands" 所在的像素。

我正在使用 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.

在你的表单中生成一个可以容纳所有点的文件 列表轨道 = new 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);
    }
}