如何顺利动画Windows窗体的位置,不同的速度? [英] How to smoothly animate Windows Forms location with different speeds?

查看:260
本文介绍了如何顺利动画Windows窗体的位置,不同的速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图动画顺利一些Windows窗体的位置,但我有,如果我想要的速度是可变的一些问题。换句话说,如果我想允许用户选择动画首选速度。



我发现下面的文章,帮助我颇有几分执行动画我一直在寻找,我的表。这似乎在各方面都比一个BackgroundWorker或线程的方法,我在过去的尝试更好:
http://www.vcskicks.com/animated-windows-form.html



我唯一的问题现在是,如果要保持一个平滑的动画我要Ø对动画不同的速度。有两个值是在我的代码很重要, FPS PX 。 FPS代表帧每秒(还有什么)和PX代表像素移动窗体的数量。



问题1):要拥有最流畅可能的动画,我宁愿在一个时间表格移动1像素,但我不认为我将能够以最快的速度,因为我想这样的移动方式。 fps值提高到一个非常高的价值似乎并没有采取任何作用,它就像有一个极限,超过该限制,将不会有明显的差异。我敢肯定有应该是一个很好的解释



我的问题在这里:你对这个问题,或者唯一的解决办法什么好的解决办法是改变PX值并通过更多的移动形式大于1px的,如果我想更快的运动?



第2题)如果对上述问题的解决办法是改变在PX值。因此,我发现(通过测试不同的值),一个FPS值等于300就足够我的需要移动的形式缓慢和尽可能快地我想它。然后,如果我想10的速度,1,2,3,4,5,6,7,8,9和10个像素移动的形式提供慢速和快速流畅的动画,就像我想它。如果我想5的速度,我可以用2,4,6,8,10,例如



在这里我的问题是:有没有办法使用值的任何问题像300 FPS? ?是否有这样的值的任何不良后果。



和这里是我当前的代码:

 公共部分Form1类:表格{

布尔DIR = TRUE;

公共Form1的(){
的InitializeComponent();

位置=新的点(二分之一千二百八 - 宽度,二分之八百 - 身高/ 2);
}

私人无效的button1_Click(对象发件人,EventArgs五){
双FPS = 300;
INT PX = 1;
长lastTicks = 0;
长currentTicks = 0;
双间隔=(双)Stopwatch.Frequency / FPS;

,而(DIR左< =二分之一千二百八十零:左> =2分之1280 - 宽度?){
Application.DoEvents();

currentTicks = Stopwatch.GetTimestamp();

如果(currentTicks> = lastTicks +间隔){
lastTicks = Stopwatch.GetTimestamp();

this.Location =新的点(DIR左+ PX:左 - PX,顶部?);

this.Invalidate(); //刷新形式
}

的Thread.Sleep(1); //释放了CPU的
}

DIR = DIR!;
}

}

注意:这只是示例代码,用于测试目的,不是真正的代码,但做我的客人,如果你想指出的移植这在实际应用的时候,我应该考虑一些非常重要的事情。


< DIV CLASS =h2_lin>解决方案

试试这个实现我之前的建议(不占FPS):

 公共部分Form1类:表格
{
私人布尔去;
私人INT DX = 1;
公共Form1中()
{
的InitializeComponent();
}
保护覆盖无效的OnPaint(PaintEventArgs的E)
{
base.OnPaint(E);
如果(去)
{
this.Location =新的点(this.Location.X + DX,this.Location.Y);
如果(Location.X小于10 || Location.X> 1200)
{
去= FALSE;
DX = -dx;
}
,否则
{
this.Invalidate();
}
}
}
私人无效的button1_Click(对象发件人,EventArgs五)
{
去= TRUE;
this.Invalidate();
}
}



我想你可能将不得不到外面去的WinForms的要获得更高的FPS。


I've been trying to smoothly animate some Windows Form location but I'm having some issues if I want the speed to be variable. In other words, if I want to allow the user to select the preferred speed for animation.

I've found the following article that helped me quite a bit to perform the animation I was looking for, for my form. It seems better in every way than a BackgroundWorker or Threads approach I tried in the past: http://www.vcskicks.com/animated-windows-form.html

My only problem now, is to maintain a smooth animation if I want o have different speeds for the animation. There are two values that are important in my code, FPS and PX. FPS represents frames per second (what else) and PX represents the number of pixels to move the form.

Problem 1) To have the smoothest possible animation, I would prefer to have the form move 1px at a time but I don't think that I will be able to move the form as fast as I want like that. Increasing the FPS value to a very high value doesn't seem to take any effect, it's like there's a limit and above that limit, there will be no visible differences. I'm sure there's a good explanation for that.

My question here is: Do you have any good solution for this problem or the only solution is to change the PX value and move the form by more than 1px if I want a faster movement?

Problem 2) If the solution for the question above is to change the PX value accordingly, I found out (by testing different values) that a FPS value equal to 300 would suffice my needs to move the form as slow and as fast as I want it to. Then, if I wanted 10 speeds, moving the form by 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10 pixels provides slow and fast smooth animations, just as I want it. If I wanted 5 speeds, I could use 2, 4, 6, 8, 10, for instance.

My question here is: Is there any problem to use a value like 300 for FPS? Are there any bad consequences for such a value?

And here's my current code:

public partial class Form1 : Form {

    bool dir = true;

    public Form1() {
        InitializeComponent();

        Location = new Point(1280/2 - Width, 800/2 - Height/2);
    }

    private void button1_Click(object sender, EventArgs e) {
        double FPS = 300;
        int PX = 1;
        long lastTicks = 0;
        long currentTicks = 0;
        double interval = (double)Stopwatch.Frequency / FPS;

        while(dir ? Left <= 1280/2 : Left >= 1280/2 - Width) {
            Application.DoEvents();

            currentTicks = Stopwatch.GetTimestamp();

            if(currentTicks >= lastTicks + interval) {
                lastTicks = Stopwatch.GetTimestamp();

                this.Location = new Point(dir ? Left + PX : Left - PX, Top);

                this.Invalidate(); //refreshes the form
            }

            Thread.Sleep(1); //frees up the cpu
        }

        dir = !dir;
    }

}

Note: This is just sample code, for testing purposes, not real code but be my guest if you want to point out some very important things that I should consider when porting this to the real application.

解决方案

Try this implementation of my prior suggestion (without accounting for FPS):

public partial class Form1 : Form
{
    private bool go;
    private int dx = 1;
    public Form1()
    {
        InitializeComponent();
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        if (go)
        {
            this.Location = new Point(this.Location.X + dx, this.Location.Y);
            if (Location.X < 10 || Location.X > 1200)
            {
                go = false;
                dx = -dx;
            }
            else
            {
                this.Invalidate();
            }
        }
    }
    private void button1_Click(object sender, EventArgs e)
    {
        go = true;
        this.Invalidate();
    }
}

I think you will likely have to go outside of winforms to get higher FPS.

这篇关于如何顺利动画Windows窗体的位置,不同的速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆