简体   繁体   中英

How to redraw a line using values from a trackBar in realtime?

I was just wondering if you could help me with this problem.

In my program I must use the paint event to draw a line which acts as a "turret". I must use a trackbar to choose the angle at which I will "fire" this turret. My problem is I'm not sure how to code it so that the "Turret" line is redrawn every time the value in the trackbar is changed. I tried using Refresh() which allowed the line to be drawn in realtime but it caused the form to flicker black and white and to redraw the line at different angles to what was chosen also.

Any help would be greatly appreciated. Thanks!

Here is a screenshot of the form, from the below code running in 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);
            }
        }

You can set the form to double buffered which should stop the flicker. this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |ControlStyles.AllPaintingInWmPaint |ControlStyles.OptimizedDoubleBuffer true); this.UpdateStyles();

In your form's constructor or OnLoad method

Or by setting the DoubleBufferered property to true. I usually perform a combination of both.

I think OptimizedDoubleBuffer is the same as the other three flags combined. However without the other two, DoubleBuffered seems to have no affect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM