按钮动画控件----------WinForm控件开发系列

2021-02-08 05:14

阅读:326

标签:strong   div   ima   str   public   rms   wing   return   width   

技术图片

  public partial class ButtonExt : Button
  {
    private AnimationTimer _Animation;
    /// 
    /// 动画组件对象
    /// 
    [Description("动画组件对象")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public AnimationTimer Animation
    {
      get { return _Animation; }
      set { _Animation = value; }
    }

    protected override Size DefaultSize
    {
      get
      {
        return new Size(100, 40);
      }
    }
    public ButtonExt()
    {
      InitializeComponent();
      this.BackColor = System.Drawing.Color.OliveDrab;
      this.FlatAppearance.BorderSize = 0;
      this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
      this.ForeColor = System.Drawing.Color.White;

      this._Animation = new AnimationTimer(this, new AnimationOptions());
      this.Animation.AnimationIng += new AnimationTimer.AnimationHandel(Animation_AnimationIng);
    }

    protected void Animation_AnimationIng(object sender, AnimationEventArgs e)
    {
      ButtonExt control = (ButtonExt)sender;
      control.Width = (int)((double)control.original_w + e.Transform * e.progressTime);
      control.Height = (int)((double)control.original_h + e.Transform * e.progressTime);
      int x = (int)((this.original_w - control.Width) / 2.0);
      int y = (int)((this.original_h - control.Height) / 2.0);
      control.Location = new Point((int)this.original_x + x, (int)this.original_y + y);
    }

    protected override void OnMouseEnter(EventArgs e)
    {
      base.OnMouseEnter(e);

      this.loadOriginal(this.original_isload);
      this._Animation.AT = AnimationType.ElasticOut;
      this._Animation.Start(true, this._Animation.UsedTime);
    }

    protected override void OnMouseLeave(EventArgs e)
    {
      base.OnMouseLeave(e);
      this._Animation.AT = AnimationType.BackIn;
      this._Animation.Start(false, this._Animation.UsedTime);
    }


    private bool original_isload = false;
    private double original_w = 0.0;// 动画对象开始制定属性原始值
    private double original_h = 0.0;// 动画对象开始制定属性原始值
    private int original_x = 0;// 动画对象开始制定属性原始值
    private int original_y = 0;// 动画对象开始制定属性原始值
    private void loadOriginal(bool _isload)
    {
      if (!_isload)
      {
        this.original_w = this.Width;
        this.original_h = this.Height;
        this.original_x = this.Location.X;
        this.original_y = this.Location.Y;
        this.original_isload = true;
      }

    }


    ///  
    /// 清理所有正在使用的资源。
    /// 
    /// 如果应释放托管资源,为 true;否则为 false。
    protected override void Dispose(bool disposing)
    {
      if (disposing && (components != null))
      {
        components.Dispose();
        if (this._Animation != null)
        {
          this._Animation.Dispose();
        }
      }
      base.Dispose(disposing);
    }
  }

 源码下载:按钮动画控件.zip

按钮动画控件----------WinForm控件开发系列

标签:strong   div   ima   str   public   rms   wing   return   width   

原文地址:https://www.cnblogs.com/tlmbem/p/11204624.html


评论


亲,登录后才可以留言!