我只是想知道您是否可以帮我解决这个问题。
在我的程序中,我必须使用 paint 事件来绘制一条充当“炮塔”的线。我必须使用轨迹栏来选择我将“开火”这个炮塔的角度。我的问题是我不确定如何编写代码以便每次更改轨迹栏中的值时重新绘制“炮塔”线。我尝试使用 Refresh(),它允许实时绘制线条,但它会导致表单黑白闪烁,并以与所选内容不同的角度重新绘制线条。
如有任何帮助,我们将不胜感激。谢谢!
这是表单的屏幕截图,来自以下在 Visual Basic 2010 中运行的代码
public partial class Form1 : Form
{
double xEnd = 0;
double yEnd = 0;
double xOrigin = 30;
double yOrigin = 450;
double xHor = 30;
double yHor = 350;
double xVert = 130;
double yVert = 450;
double lineLength = 100;
public Form1()
{
xEnd = xOrigin + lineLength;
yEnd = yOrigin;
InitializeComponent();
}
private void LineDrawEvent(object sender, PaintEventArgs paint)
{
Graphics drawSurface = paint.Graphics;
Pen turretLine = new Pen(Color.Blue);
Pen graphHorizontal = new Pen(Color.Red);
Pen graphVertical = new Pen(Color.Red);
Pen firedLine = new Pen(Color.Blue);
drawSurface.DrawLine(graphVertical, (int)xOrigin, (int)yOrigin, (int)xHor, (int)yHor);
drawSurface.DrawLine(graphHorizontal, (int)xOrigin, (int)yOrigin, (int)xVert, (int)yVert);
double angleInRadians = ConvertDegsToRads((double)trckBarAngle.Value);
xEnd = xOrigin + lineLength * Math.Cos(angleInRadians);
yEnd = yOrigin - lineLength * Math.Sin(angleInRadians);
drawSurface.DrawLine(turretLine, (int)xOrigin, (int)yOrigin, (int)xEnd, (int)yEnd);
}
private void trckBarAngle_Scroll(object sender, EventArgs e)
{
lblAngle.Text = "Angle is:" + Convert.ToString((double)trckBarAngle.Value / 2);
}
private double ConvertDegsToRads(double degrees)
{
return degrees * (Math.PI / 180.0);
}
}
您可以将表单设置为双缓冲,这样可以停止闪烁。this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |ControlStyles.AllPaintingInWmPaint |ControlStyles.OptimizedDoubleBuffer
真的);
this.UpdateStyles();
在表单的构造函数或 OnLoad 方法中
或者通过将 DoubleBufferered
属性设置为 true。我通常将两者结合起来。
我认为 OptimizedDoubleBuffer
与其他三个标志的组合相同。然而,如果没有其他两个,DoubleBuffered
似乎没有任何影响。
我是一名优秀的程序员,十分优秀!