该控件是继承于 Button 基类开发的。动画主要利用定时器完成。
1 /// <summary> 2 /// Button动画扩展 3 /// </summary> 4 [Description("Button动画扩展")] 5 public partial class ButtonExt : Button 6 { 7 #region 新增属性 8 9 private AnimationTimer animation = null; 10 /// <summary> 11 /// 动画组件对象 12 /// </summary> 13 [Description("动画组件对象")] 14 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 15 public AnimationTimer Animation 16 { 17 get { return this.animation; } 18 set { this.animation = value; } 19 } 20 21 #endregion 22 23 #region 重写属性 24 25 [DefaultValue(FlatStyle.Flat)] 26 public new FlatStyle FlatStyle 27 { 28 get 29 { 30 return base.FlatStyle; 31 } 32 set 33 { 34 base.FlatStyle = value; 35 } 36 } 37 38 [DefaultValue(false)] 39 public new bool UseVisualStyleBackColor 40 { 41 get 42 { 43 return base.UseVisualStyleBackColor; 44 } 45 set 46 { 47 base.UseVisualStyleBackColor = value; 48 } 49 } 50 51 [DefaultValue(typeof(Color), "245, 168, 154")] 52 public override Color BackColor 53 { 54 get 55 { 56 return base.BackColor; 57 } 58 set 59 { 60 base.BackColor = value; 61 } 62 } 63 64 [DefaultValue(typeof(Color), "White")] 65 public override System.Drawing.Color ForeColor 66 { 67 get 68 { 69 return base.ForeColor; 70 } 71 set 72 { 73 base.ForeColor = value; 74 } 75 } 76 77 [Browsable(false)] 78 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] 79 protected new bool DesignMode 80 { 81 get 82 { 83 if (this.GetService(typeof(IDesignerHost)) != null || System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime) 84 { 85 return true; //界面设计模式 86 } 87 else 88 { 89 return false;//运行时模式 90 } 91 } 92 } 93 94 protected override Size DefaultSize 95 { 96 get 97 { 98 return new Size(100, 40); 99 } 100 } 101 102 #endregion 103 104 #region 字段 105 106 private bool original_isload = false; 107 private double original_w = 0.0;// 动画对象开始制定属性原始值 108 private double original_h = 0.0;// 动画对象开始制定属性原始值 109 private int original_x = 0;// 动画对象开始制定属性原始值 110 private int original_y = 0;// 动画对象开始制定属性原始值 111 112 #endregion 113 114 public ButtonExt() 115 { 116 InitializeComponent(); 117 118 this.BackColor = System.Drawing.Color.OliveDrab; 119 this.FlatAppearance.BorderSize = 0; 120 this.FlatStyle = FlatStyle.Flat; 121 this.ForeColor = Color.White; 122 123 this.animation = new AnimationTimer(this, new AnimationOptions() { EveryNewTimer = false }); 124 this.animation.Animationing += new AnimationTimer.AnimationEventHandler(this.Animation_Animationing); 125 } 126 127 #region 重写 128 129 protected override void OnMouseEnter(EventArgs e) 130 { 131 base.OnMouseEnter(e); 132 133 if (this.DesignMode) 134 return; 135 136 this.loadOriginal(this.original_isload); 137 this.animation.AnimationType = AnimationTypes.ElasticOut; 138 this.animation.Start(AnimationIntervalTypes.Add, this.animation.AnimationUsedTime); 139 } 140 141 protected override void OnMouseLeave(EventArgs e) 142 { 143 base.OnMouseLeave(e); 144 145 if (this.DesignMode) 146 return; 147 148 this.animation.AnimationType = AnimationTypes.BackIn; 149 this.animation.Start(AnimationIntervalTypes.Subtrac, this.animation.AnimationUsedTime); 150 } 151 152 /// <summary> 153 /// 清理所有正在使用的资源。 154 /// </summary> 155 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> 156 protected override void Dispose(bool disposing) 157 { 158 if (disposing && components != null) 159 { 160 components.Dispose(); 161 } 162 if (disposing) 163 { 164 if (this.animation != null) 165 this.animation.Dispose(); 166 } 167 base.Dispose(disposing); 168 } 169 #endregion 170 171 #region 私有方法 172 173 protected void Animation_Animationing(object sender, AnimationEventArgs e) 174 { 175 ButtonExt control = (ButtonExt)sender; 176 control.Width = (int)((double)control.original_w + e.AllTransformValue * e.ProgressValue); 177 control.Height = (int)((double)control.original_h + e.AllTransformValue * e.ProgressValue); 178 int x = (int)((this.original_w - control.Width) / 2.0); 179 int y = (int)((this.original_h - control.Height) / 2.0); 180 control.Location = new Point((int)this.original_x + x, (int)this.original_y + y); 181 } 182 183 private void loadOriginal(bool _isload) 184 { 185 if (!_isload) 186 { 187 this.original_w = this.Width; 188 this.original_h = this.Height; 189 this.original_x = this.Location.X; 190 this.original_y = this.Location.Y; 191 this.original_isload = true; 192 } 193 } 194 195 #endregion 196 197 }