如何使用WinFormAnimation停止C#中的动画?

如何使用WinFormAnimation停止C#中的动画?,c#,winforms,animation,C#,Winforms,Animation,我有一个滚动文本,当按下加载按钮时,动画开始,但是当按下停止按钮时,动画不会停止,而是继续滚动 我已经创建了按钮名“加载” 当按下Load时,文本开始滚动 在加载按钮中,按钮的名称更改为“停止” 只有一个按钮“加载”。按钮的名称更改为“停止”。当按下“加载”按钮时,用户第二次按下同一按钮时,将进入“停止”按钮代码以停止动画 它进入“Stop.”按钮代码,即mf.button1.Text==“Stop.”其中Animator.Stop();用于停止动画,动画不会停止 但是当animator.sto

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

  • 我已经创建了按钮名“加载”
  • 当按下Load时,文本开始滚动
  • 在加载按钮中,按钮的名称更改为“停止”
  • 只有一个按钮“加载”。按钮的名称更改为“停止”。当按下“加载”按钮时,用户第二次按下同一按钮时,将进入“停止”按钮代码以停止动画

  • 它进入“Stop.”按钮代码,即mf.button1.Text==“Stop.”其中Animator.Stop();用于停止动画,动画不会停止

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

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

  • 使用WinFormAnimation;
    私有无效滚动标签()
    { 
    字符串textToScroll=示例;
    var持续时间=250000ul;
    var maxLabelChars=115;
    var标签=mf.label16;
    var winform=new WinFormAnimation.Path(0,100,动画的持续时间);
    var animator=新的动画师(winform);
    尝试
    {
    如果(mf.button1.Text==“加载”)
    {
    动画师,播放(
    新的安全调用程序(f=>
    {
    标签文本=
    textToScroll.Substring(
    (int)数学最大值(数学上限((texttoscoll.Length-maxLabelChars)/100f*f)-1,0),
    maxLabelChars);
    },标签号);
    mf.button1.Text=“停止。”
    }
    如果(mf.button1.Text==“停止”)
    {
    MessageBox.Show(“动画师停止!”);
    停止();
    }
    }
    catch(System.Reflection.targetingException ex){ex.Message.ToString();}
    }
    }
    
    我希望滚动在按下停止按钮时停止,在按下加载按钮时启动


    使用的库:

    将动画师移出
    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
        }
    }
    
    使用WinFormAnimation;
    私有路径winform=null;
    私有动画师Animator=null;
    私有void InitAnimator()
    {
    var持续时间=250000ul;
    winform=新路径(0,100,动画持续时间);
    动画师=新动画师(winform);
    }
    私有无效滚动标签()
    { 
    字符串textToScroll=示例;
    var maxLabelChars=115;
    var标签=mf.label16;
    if(winform==null)
    {
    InitAnimator();
    }        
    尝试
    {
    如果(mf.button1.Text==“加载”)
    {
    动画师,播放(
    新的安全调用程序(f=>
    {
    标签文本=
    textToScroll.Substring(
    (int)数学最大值(数学上限((texttoscoll.Length-maxLabelChars)/100f*f)-1,0),
    maxLabelChars);
    },标签号);
    mf.button1.Text=“停止。”
    }
    否则如果(mf.button1.Text==“停止”)
    {
    MessageBox.Show(“动画师停止!”);
    停止();
    }
    }
    catch(System.Reflection.targetInvocationEx)
    { 
    ex.Message.ToString();//这绝对没有任何作用
    }
    }
    

    这将是一个最低限度的解决方案。你应该保持一个状态,而不是依赖于检查按钮文本。除了例外,你应该做一些明智的事情。

    我尝试了@PalleDue示例,它似乎工作正常。因此,对animator实例的引用应该存在某种问题

    如果您有多个滚动标签和多个按钮,那么正确的方法是使用
    UserControl
    并在那里编写有关所有这些的代码。然后在你的表格中使用它。并且不要直接从表单中更改
    UserControl
    的内容。创建一个新的
    UserControl
    的整个想法就是封装逻辑

    在任何情况下,至少可以使用其中一个控件的
    Control.Tag
    属性来存储附加信息;在本例中,
    Animator
    实例。下面是一个简单的示例,我已经测试过并且可以正常工作(使用一个标签和一个按钮):

    private void按钮\u单击(对象发送者,事件参数e)
    {
    //获取对按钮和标签的引用
    var标签=标签1;
    var按钮=按钮1;
    //获取对负责此标签的动画师的引用
    var animator=label.Tag作为animator;
    尝试
    {
    if(animator?.CurrentStatus==AnimatorStatus.Playing)
    {
    //如果你在玩,停下来
    文本=@“播放”;
    停止();
    }
    其他的
    {
    //加载文本并计算标签的大小(以字符和预期行为单位)
    字符串textToScroll=@"Lorem Ipsum仅仅是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是该行业的标准虚拟文本,当时一家不知名的印刷商拿着一个打字机,争先恐后地制作了一本活字样本书。它不仅存活了五个世纪,而且还跨越了电子排版的历史,至今仍处于领先地位基本上没有变化。它在20世纪60年代随着包含Lorem Ipsum段落的Letraset表单的发布而流行,最近又随着包括Lorem Ipsum版本在内的Aldus PageMaker等桌面出版软件的发布而流行。”;
    var labelWidthInChars=(int)(label.Width/label.CreateGraphics().MeasureString(“A”,label.Font).Width);
    var labelRows=(int)Math.天花((十进制)textToScroll.Length/labelWidthInC
    
    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
        }
    }
    
    private void button_Click(object sender, EventArgs e)
    {
        // Get reference to the button and the label
        var label = label1;
        var button = button1;
    
        // Get a reference to the animator responsible for this label
        var animator = label.Tag as Animator;
    
        try
        {
            if (animator?.CurrentStatus == AnimatorStatus.Playing)
            {
                // If playing; stop
                button.Text = @"Play";
                animator.Stop();
            }
            else
            {
                // Load the text and calculate the size of the label in chars as well as expected rows
                string textToScroll = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
                var labelWidthInChars = (int)(label.Width / label.CreateGraphics().MeasureString("A", label.Font).Width);
                var labelRows = (int)Math.Ceiling((decimal)textToScroll.Length / labelWidthInChars);
    
                // Create a new animator and set it to animate from (0) to the (number of rows - 1) 
                // in 10 seconds with 10fps maximum callback frequency
                label.Tag = animator = new Animator(new Path(0, labelRows - 1, 10000), FPSLimiterKnownValues.LimitTen);
    
                button.Text = @"Stop";
                animator.Play(
                    new SafeInvoker<float>(f =>
                        {
                            label.Text = textToScroll.Substring(labelWidthInChars * (int)Math.Floor(f));
                        },
                        label));
            }
        }
        catch (Exception exception)
        {
            MessageBox.Show(exception.Message);
        }
    }