chatGPT实现的
using System;
using System.Drawing;
using System.Windows.Forms;
namespace DrawReverseLineDemo
{
public partial class MainForm : Form
{
// 可调参数
private int lineWidth = 5; // 轨迹线宽度
private int angle = 30; // 轨迹线角度
public MainForm()
{
InitializeComponent();
}
private void MainForm_Paint(object sender, PaintEventArgs e)
{
// 绘制轨迹线
Pen pen = new Pen(Color.GreenYellow, lineWidth);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
e.Graphics.DrawLine(pen, this.ClientRectangle.Width / 2, this.ClientRectangle.Height,
this.ClientRectangle.Width / 2 + (int)(Math.Tan(angle * Math.PI / 180) * this.ClientRectangle.Height), 0);
}
private void MainForm_Resize(object sender, EventArgs e)
{
this.Refresh(); // 窗口大小改变时重绘轨迹线
}
}
}
在该示例代码中,MainForm 继承自 Form,并提供了绘制轨迹线和重绘的方法。在 MainForm_Paint 中,我们使用 Graphics.DrawLine 方法绘制了一条绿色黄色的虚线轨迹线,其中线宽和样式由 Pen 对象的属性控制,而轨迹线的角度则使用 Math.Tan 函数计算。在 MainForm_Resize 方法中,我们在窗口大小改变时调用了 Refresh 方法,以便重绘轨迹线。
|