且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用C#在计算器中创建类似的按钮动画

更新时间:2023-10-25 12:48:58

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

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 $的计时器c $ c>。确保 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;
      }