开发者

Slowing down animation in C#

I have a class project that uses Windows Forms to create a GUI that controls a second form. The second form is a DrawingForm with a bitmap. Using a backgroundworker, I am drawing random, continuous Bezier curves all over the bitmap. It is a simple program, so it is able to draw them quickly, hundreds per second. I would like to add a slider bar that would allow me to control how fast the lines paint. In other words, I dont want to set up each curve to be drawn on a timer, which would cause it to appear to stop and start hundreds of times a second. I have exhausted myself searching google, any tips on how to do this would be awesome. Thanks!

Edit: Here is a code snippet. This code is in my class for my drawing form. Its constructor is called from my main GUI/user control class.

    // this is the code executed by the background thread
    // it can run continously without hanging the user interface thread
    // except that it draws to a bit开发者_运维技巧map (with the bMapDC) instead of to the form
    private void backgroundWorkerDrawing_DoWork(object sender, DoWorkEventArgs e)
    {

        for (int i = 0; i < 100000000; i++)
        {
            if (scribbleOn == true)
            {
                curveColor = changeColor(curveColor);
                Pen pen = new Pen(curveColor, penThickness);

                if (i == 0) // initial curve should start in center, the rest of the points will be random
                {
                    lastX = GUI.rand.Next(0, bMapWidth); //used to store the x coordinate where the curve ends
                    lastY = GUI.rand.Next(0, bMapHeight); //used to store the y coordinate where the curve ends

                    bMapDC.DrawBezier(pen, initialX, initialY, GUI.rand.Next(0, bMapWidth), GUI.rand.Next(0, bMapHeight),
                    GUI.rand.Next(0, bMapWidth), GUI.rand.Next(0, bMapHeight), lastX, lastY);
                }

                if (i > 0) // used for all curves after the first one. 
                {
                    int tempX = GUI.rand.Next(0, bMapWidth); //used to store the x coordinate where the curve ends
                    int tempY = GUI.rand.Next(0, bMapHeight); //used to store the y coordinate where the curve ends

                    bMapDC.DrawBezier(pen, lastX, lastY, GUI.rand.Next(0, bMapWidth), GUI.rand.Next(0, bMapHeight),
                    GUI.rand.Next(0, bMapWidth), GUI.rand.Next(0, bMapHeight), tempX, tempY);

                    lastX = tempX; // sets the last x coordinate of the last curve for next loop
                    lastY = tempY;  // sets the last y coordinate of the last curve for next loop
                }
                pen.Dispose(); // free up resources from the pen object
            }   
            else i = 0;
        }
    }

    // timer event handler causes the form to be repreatedly invalidated
    // This causes the paint event handler to keep going off,
    // which causes the bMap that is continously being drawn to
    // by the background thread to be continously redisplayed in the form.
    // We will see other ways to do this that may be better.
    private void timerInvalidate_Tick(object sender, EventArgs e)
    {
        Invalidate();
    }

    private void DrawingForm_Shown(object sender, EventArgs e)
    {
        lock (bMap)
        {
            bMapHeight = bMap.Height;  // set the vars that keep track of the size of the bMap
            bMapWidth = bMap.Width;

            initialX = bMapWidth / 2; // start the curve at the center of the bMap
            initialY = bMapHeight / 2;
            bMapDC = Graphics.FromImage(bMap);  // setup the DC (device context) to allow drawing to the bMap)
            backgroundWorkerDrawing.RunWorkerAsync();   // start the background thread
            timerInvalidate.Enabled = true;  // start the timer that will cause periodic Invalidates
        }
    }


You can make thread and use sleep

private Thread SimulaciaArts;
public Animation(){
public SpleepValue { get; set;}

SimulaciaArts = new Thread(new ThreadStart(simuluj));
}

public void simuluj(){

//anything
Thread.Sleep(SleepValue);

}

and in gui you must use delegate

 delegate void Invoker();
 private void setSpeed()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new Invoker(setSpeed));
                return;
            }
 Simulation.SleepValue=Speed;
}

Hope it is good.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜