如何使用C#在计算器中创建类似的按钮动画 [英] How to create button animation similar in calculator with C#

查看:133
本文介绍了如何使用C#在计算器中创建类似的按钮动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您将鼠标悬停在按钮上时,有人可以像在Win7计算器中那样创建winform动画吗,当前我使用一堆图像,然后在backgroundworker中将其循环,但是我认为它是错误的,这是我的代码:

Can someone help about create a winform animation like in Win7 Calculator when you hover mouse over button, currently i use bunch of image then looping it in backgroundworker, but i think its wrong, this is my code:

这会在鼠标进入时发生

private void bgTurnOn_DoWork(object sender, DoWorkEventArgs e)
{
    Label labelSender = (Label)e.Argument;
    int ii = labelSender.ImageIndex;
    for (int i = ii + 4; i <= 11; i++)
    {
        if (labelSender.AllowDrop)
        {
            labelSender.ImageIndex = i;
            Thread.Sleep(40);
        }
    }
}

而这在鼠标离开时

private void bgTurnOff_DoWork(object sender, DoWorkEventArgs e)
{
    Label labelSender = (Label)e.Argument;
    int ii = labelSender.ImageIndex;
    for (int i = ii; i >= 0; i--)
    {
        if (!labelSender.AllowDrop)
        {
            labelSender.ImageIndex = i;
            Thread.Sleep(80);
        }
    }
}

注意:我只是使用AllowDrop所以我不必费心声明新变量,我有42个按钮,所以我认为我需要更有效的解决方案。

note: I just use AllowDrop so I do not bother to declare new variable, i have 42 button, so i think i need more efficient solution.

推荐答案

它似乎您想要发光效果,因此可以使用下一个想法:

It seems that you want a glow effect, so you can use the next idea:


  • 制作一个 OpacityPictureBox:PictureBox 支持不透明度(级别为1-100或0-1的两倍)。有关更多信息,请参见

  • 中添加两个公共const int值 MaxOpacity MinOpacity OpacityPictureBox 类,用于从外部轻松安全地进行范围检查。值可以是0、100或0、1或其他值,具体取决于您实现的不透明度。

  • 制作一个 AnimatedPictureBox:UserControl 包含1个 PictureBox 名为 pbNormal 和1个 OpacityPictureBox 名为 opbHover ,都 Dock = DockStyle.Fill 和一个名为 timer 。确保 pbNormal 低于 opbHover

  • 具有三个公共属性:

    • 普通类型为图片的类型,其委派为 pbNormal.Image

    • 悬停类型为 Image 委托给 opbHover.Image

    • AnimationInterval int 分配到 timer.Interval

    • Make an OpacityPictureBox : PictureBox which supports opacity (in levels of 1-100 or double 0-1). See this for more information.
    • Add two public const int values of MaxOpacity and MinOpacity to the OpacityPictureBox class, for easy and safe range checks from the outside. The values might be 0, 100 or 0, 1, or something else, depending on your implementation of opacity.
    • Make an AnimatedPictureBox : UserControl which holds 1 PictureBox named pbNormal and 1 OpacityPictureBox named opbHover, both Dock = DockStyle.Fill, and one timer named timer. Make sure that pbNormal is below opbHover.
    • Have three public properties:
      • Normal of type Image which delegates into pbNormal.Image
      • Hover of type Image which delegates into opbHover.Image
      • AnimationInterval of type int which delgates into timer.Interval

      代码:

      private void Animate(int animationDirection)
      {
          this._animationDirection = animationDirection;
          this.timer.Start();
      }
      




      • 覆盖 OnMouseEnter OnMouseLeave

        • Override OnMouseEnter and OnMouseLeave:
        • 代码:

           protected override void OnMouseEnter(EventArgs e)
           {
               this.Animate(1);
               base.OnMouseEnter(e);
           }
          
           protected override void OnMouseLeave(EventArgs e)
           {
               this.Animate(-1);
               base.OnMouseEnter(e);
           }
          




          • 收听定时器勾选事件,并带有以下内容:

            • Listen to timer.Tick event and with this:
            • 代码:

              private void timer_Tick(object sender, EventArgs e)
              {
                  var hoverOpacity = this.opbHover.Opacity + this._animationDirection;
              
                  if (hoverOpacity < OpacityPictureBox.MinOpacity ||
                      hoverOpacity > OpacityPictureBox.MaxOpacity)
                  {
                      this.timer.Stop();
                      return;
                  }
              
                  this.opbHover.Opacity = hoverOpacity;
              }
              

              这篇关于如何使用C#在计算器中创建类似的按钮动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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