如何使用WinFormAnimation在C#中停止动画? [英] How to Stop Animation in C# using WinFormAnimation?

查看:425
本文介绍了如何使用WinFormAnimation在C#中停止动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个滚动文本,当按下加载按钮时,动画开始,但是当按下停止按钮时,动画没有停止,而是继续滚动。

I have a scrolling text, when the Load button is pressed, the animation starts, however it does not stop when Stop button is pressed, it keeps on scrolling.


  1. 我已经创建了按钮名称 Load。

  2. 按下加载键后,文本开始滚动。

  3. 在加载按钮中,按钮名称更改为停止。

只有1个按钮加载。按钮的名称更改为停止。当按下加载按钮,因此用户第二次按下相同的按钮时,它将进入停止按钮代码以停止动画。

There is only 1 button "Load". The name of the button changes to Stop. when Load button is pressed, so the second time user presses the same button, it goes into stop button code to stop the animation.


  1. 进入停止。 mf.button1.Text == Stop的按钮代码。其中Animator.Stop();

  1. It goes into "Stop." button code that is mf.button1.Text == "Stop." where Animator.Stop(); is used to stop the animation, the animation does not stop.

但是当animator.stop();停止时,动画不会停止。在加载代码中的安全调用程序之后立即使用,它确实会在那里停止。

However when the animator.stop(); is used right after safe invoker in load code, it does stops there.

我希望用户停止滚动文本。

I want the user to stop the scrolling text.



using WinFormAnimation;
private void ScrollLabel()
{ 
            string textToScroll = sample;
            var durationOfAnimation = 250000ul;
            var maxLabelChars = 115;
            var label = mf.label16;
            var winform = new WinFormAnimation.Path(0, 100, durationOfAnimation);
            var animator = new Animator(winform);

            try
            {
                if (mf.button1.Text == "Load.")
                {
                    animator.Play(
                    new SafeInvoker<float>(f =>
                    {
                        label.Text =
                            textToScroll.Substring(
                                (int)Math.Max(Math.Ceiling((textToScroll.Length - maxLabelChars) / 100f * f) - 1, 0),
                                maxLabelChars);
                    }, label));
                    mf.button1.Text = "Stop."
                }

                    if (mf.button1.Text == "Stop.")
                    {
                        MessageBox.Show("Animator Stop!");
                        animator.Stop();
                    }

            }
            catch (System.Reflection.TargetInvocationException ex) { ex.Message.ToString(); }
        }
    }

我希望在停止时停止滚动

I expect the scroll to be stopped when the Stop Button is pressed and Start when Load Button is pressed.

使用的库: https://falahati.github.io/WinFormAnimation/

推荐答案

将动画制作者移至 ScrollLabel

using WinFormAnimation;

private Path winform = null;
private Animator animator = null;

private void InitAnimator()
{
    var durationOfAnimation = 250000ul;
    winform = new Path(0, 100, durationOfAnimation);
    animator = new Animator(winform);
}

private void ScrollLabel()
{ 
    string textToScroll = sample;
    var maxLabelChars = 115;
    var label = mf.label16;
    if (winform == null)
    {
        InitAnimator();
    }        
    try
    {
        if (mf.button1.Text == "Load.")
        {
            animator.Play(
                    new SafeInvoker<float>(f =>
                    {
                        label.Text =
                            textToScroll.Substring(
                                (int)Math.Max(Math.Ceiling((textToScroll.Length - maxLabelChars) / 100f * f) - 1, 0),
                                maxLabelChars);
                    }, label));
            mf.button1.Text = "Stop."
        }
        else if (mf.button1.Text == "Stop.")
        {
            MessageBox.Show("Animator Stop!");
            animator.Stop();
        }

    }
    catch (System.Reflection.TargetInvocationException ex) 
    { 
        ex.Message.ToString(); // This does absolutely nothing
    }
}

最小的解决方案。您应该保持状态,而不是依赖于检查按钮文本。而且您应该对异常做些明智的事情。

This would be a bare minimum solution. You should keep a state instead of relying on checking button texts. And you should do something sensible with the exception.

这篇关于如何使用WinFormAnimation在C#中停止动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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