2

我只是想知道你是否可以帮助我解决这个问题。

在我的程序中,我必须使用绘画事件来绘制一条充当“炮塔”的线。我必须使用轨迹栏来选择我将“开火”这个炮塔的角度。我的问题是我不确定如何对其进行编码,以便每次更改轨迹栏中的值时都会重绘“炮塔”线。我尝试使用 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);
            }
        }
4

1 回答 1

0

您可以将表单设置为双缓冲,这应该会停止闪烁。 this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |ControlStyles.AllPaintingInWmPaint |ControlStyles.OptimizedDoubleBuffer true); this.UpdateStyles();

在表单的构造函数或 OnLoad 方法中

或者通过将DoubleBufferered属性设置为 true。我通常将两者结合起来。

我认为OptimizedDoubleBuffer与其他三个标志的组合相同。但是没有其他两个,DoubleBuffered似乎没有影响。

于 2015-10-31T22:39:51.187 回答