gpt4 book ai didi

c# - 在 C# 中减慢动画速度

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:31 28 4
gpt4 key购买 nike

我有一个类项目,它使用 Windows 窗体创建一个控制第二个窗体的 GUI。第二种形式是带有位图的 DrawingForm。使用 backgroundworker,我在整个位图中绘制随机的、连续的贝塞尔曲线。这是一个简单的程序,所以它能够以每秒数百次的速度快速绘制它们。我想添加一个 slider ,让我可以控制线条绘制的速度。换句话说,我不想将每条曲线设置为在计时器上绘制,这会导致它看起来每秒停止和启动数百次。我已经筋疲力尽地搜索谷歌,关于如何做到这一点的任何提示都会很棒。谢谢!

编辑:这是一个代码片段。这段代码在我的类里面用于我的绘图形式。它的构造函数是从我的主 GUI/用户控件类调用的。

    // 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 bitmap (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
}
}

最佳答案

你可以创建线程并使用 sleep

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

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

public void simuluj(){

//anything
Thread.Sleep(SleepValue);

}

并且在 gui 中你必须使用委托(delegate)

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

希望一切顺利

关于c# - 在 C# 中减慢动画速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6233461/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com