|
| 1 | +/***************************************************************************************************** |
| 2 | +文件名: MAnimatedButton.cs |
| 3 | +作 者: MHF |
| 4 | +描 述: 当前类为自定义的多样式动态button控件 |
| 5 | +版 本: 0.1.0 |
| 6 | +日 期: 2021.2.28 |
| 7 | +功 能: |
| 8 | +*****************************************************************************************************/ |
| 9 | +using System; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.ComponentModel; |
| 12 | +using System.Data; |
| 13 | +using System.Drawing; |
| 14 | +using System.Drawing.Drawing2D; |
| 15 | +using System.Linq; |
| 16 | +using System.Text; |
| 17 | +using System.Threading; |
| 18 | +using System.Windows.Forms; |
| 19 | + |
| 20 | +namespace MControl.Controls.Buttons |
| 21 | +{ |
| 22 | + [DefaultEvent("Click")] |
| 23 | + public partial class MAnimatedButton : UserControl |
| 24 | + { |
| 25 | + public MAnimatedButton() |
| 26 | + { |
| 27 | + InitializeComponent(); |
| 28 | + |
| 29 | + //使用双缓冲解决快速刷新时闪烁问题 |
| 30 | + this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); |
| 31 | + this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); |
| 32 | + this.SetStyle(ControlStyles.UserPaint, true); |
| 33 | + /* m_timerAnimated.Interval = 10; |
| 34 | + m_timerAnimated.Elapsed += Timer_Elapsed;*/ |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + #region 字段 |
| 39 | + Bitmap m_bitmap = null; //控件默认的样式 |
| 40 | + Bitmap m_bitmapAnimated = null; //控件动画的样式 |
| 41 | + int m_nBorderRadius = 10; //控件圆角半径 |
| 42 | + Color m_ColorBack = Color.Red; //控件背景色 |
| 43 | + Color m_ColorText = Color.Black; //文本颜色 |
| 44 | + Font m_FontText = new Font("宋体", 12, FontStyle.Regular); |
| 45 | + string m_strText = "Button"; //控件文本 |
| 46 | + System.Timers.Timer m_timerAnimated = new System.Timers.Timer(); //动画定时器 |
| 47 | + private System.Threading.Timer timerClose; |
| 48 | + bool m_bMouseState = false; //鼠标状态 false为进入,True为离开 |
| 49 | + int m_nLineLen = 0; //动态线条长度 |
| 50 | + #endregion |
| 51 | + |
| 52 | + |
| 53 | + #region 属性 |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// 获取或设置控件的文本。 |
| 57 | + /// </summary> |
| 58 | + /// <returns> |
| 59 | + /// 控件的文本。 |
| 60 | + /// </returns> |
| 61 | + [Category("自定义属性"), Description("设置控件的文本。")] |
| 62 | + public string MText |
| 63 | + { |
| 64 | + get |
| 65 | + { |
| 66 | + return m_strText; |
| 67 | + } |
| 68 | + set |
| 69 | + { |
| 70 | + m_strText = value; |
| 71 | + PaintImage(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// 获取或设置控件的文本字体。 |
| 77 | + /// </summary> |
| 78 | + /// <returns> |
| 79 | + /// 控件的文本字体。 |
| 80 | + /// </returns> |
| 81 | + [Category("自定义属性"), Description("设置控件的文本字体。")] |
| 82 | + public Font FontText |
| 83 | + { |
| 84 | + get |
| 85 | + { |
| 86 | + return m_FontText; |
| 87 | + } |
| 88 | + set |
| 89 | + { |
| 90 | + m_FontText = value; |
| 91 | + PaintImage(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// 获取或设置窗体边框圆角半径。 |
| 97 | + /// </summary> |
| 98 | + /// <returns> |
| 99 | + /// 窗体边框圆角半径 |
| 100 | + /// </returns> |
| 101 | + [Category("自定义属性"), Description("设置窗体边框圆角直径。")] |
| 102 | + public int BorderRadius |
| 103 | + { |
| 104 | + get |
| 105 | + { |
| 106 | + return m_nBorderRadius * 2; |
| 107 | + } |
| 108 | + set |
| 109 | + { |
| 110 | + if(value / 2 > this.Width || value / 2 > this.Height) |
| 111 | + { |
| 112 | + m_nBorderRadius = ((this.Width > this.Height) ? this.Height : this.Width ); |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + m_nBorderRadius = value / 2; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// 获取或设置控件的背景色。 |
| 123 | + /// </summary> |
| 124 | + /// <returns> |
| 125 | + /// 控件背景色 |
| 126 | + /// </returns> |
| 127 | + [Category("自定义属性"), Description("设置控件的背景色。")] |
| 128 | + public virtual Color MBackColor |
| 129 | + { |
| 130 | + get |
| 131 | + { |
| 132 | + return m_ColorBack; |
| 133 | + } |
| 134 | + set |
| 135 | + { |
| 136 | + m_ColorBack = value; |
| 137 | + PaintImage(); |
| 138 | + } |
| 139 | + } |
| 140 | + #endregion |
| 141 | + |
| 142 | + #region 方法 |
| 143 | + |
| 144 | + private void PaintImage() |
| 145 | + { |
| 146 | + m_bitmap = new Bitmap(this.Width, this.Height); //创建画布 |
| 147 | + Graphics l_graphics = Graphics.FromImage(m_bitmap); //创建画图对象 |
| 148 | + //l_graphics.SmoothingMode = SmoothingMode.AntiAlias; //抗锯齿 |
| 149 | + //l_graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; |
| 150 | + /*********************************** <绘制Button主体> **********************************************/ |
| 151 | + GraphicsPath graphicsPath = new GraphicsPath(); |
| 152 | + int x = 0; |
| 153 | + int y = 0; |
| 154 | + int w = Width - 1; |
| 155 | + int h = Height - 1; |
| 156 | + int a = 16; |
| 157 | + graphicsPath.AddArc(x, y, m_nBorderRadius, m_nBorderRadius, 180, 90); //绘制左上角 |
| 158 | + graphicsPath.AddArc(w - m_nBorderRadius, y, m_nBorderRadius, m_nBorderRadius, 270, 90); //绘制右上角 |
| 159 | + graphicsPath.AddArc(w - m_nBorderRadius, h - m_nBorderRadius, m_nBorderRadius, m_nBorderRadius, 0, 90); //绘制右下角 |
| 160 | + graphicsPath.AddArc(x, h - m_nBorderRadius, m_nBorderRadius, m_nBorderRadius, 90, 90); //绘制左下角 |
| 161 | + graphicsPath.CloseAllFigures(); |
| 162 | + l_graphics.FillPath(new SolidBrush(m_ColorBack), graphicsPath); |
| 163 | + |
| 164 | + /*********************************** <绘制Button文本> **********************************************/ |
| 165 | + SizeF l_sizeF = l_graphics.MeasureString(m_strText, m_FontText); //计算文本的宽高 |
| 166 | + l_graphics.DrawString(m_strText, m_FontText, new SolidBrush(m_ColorText), new PointF((this.Width - l_sizeF.Width) / 2, (this.Height - l_sizeF.Height) / 2)); |
| 167 | + l_graphics.Dispose(); |
| 168 | + |
| 169 | + } |
| 170 | + |
| 171 | + private void Timer_Elapsed(object sender) |
| 172 | + { |
| 173 | + |
| 174 | + //Console.WriteLine(DateTime.Now.ToString("ss:fff")); |
| 175 | + |
| 176 | + if (m_bMouseState) |
| 177 | + { |
| 178 | + if (m_nLineLen <= 0) |
| 179 | + { |
| 180 | + timerClose.Dispose(); |
| 181 | + //m_timerAnimated.Enabled = false; |
| 182 | + Console.WriteLine("停止2 " + DateTime.Now.ToString("ss:fff")); |
| 183 | + return; |
| 184 | + } |
| 185 | + m_nLineLen -= this.Width / 10; |
| 186 | + } |
| 187 | + else |
| 188 | + { |
| 189 | + if (m_nLineLen >= this.Width) |
| 190 | + { |
| 191 | + timerClose.Dispose(); |
| 192 | + //m_timerAnimated.Enabled = false; |
| 193 | + Console.WriteLine("停止1 " + DateTime.Now.ToString("ss:fff")); |
| 194 | + return; |
| 195 | + } |
| 196 | + m_nLineLen += this.Width / 10; |
| 197 | + } |
| 198 | +/* this.Invoke(new Action(() => |
| 199 | + { |
| 200 | + Invalidate(); |
| 201 | + Update(); |
| 202 | + }));*/ |
| 203 | + } |
| 204 | + |
| 205 | + protected override void OnMouseEnter(EventArgs e) |
| 206 | + { |
| 207 | + base.OnMouseEnter(e); |
| 208 | + |
| 209 | + m_bMouseState = false; |
| 210 | + timerClose = new System.Threading.Timer(new TimerCallback(Timer_Elapsed), this, 0, 1); |
| 211 | +/* m_timerAnimated.Start(); |
| 212 | + m_timerAnimated.Enabled = true;*/ |
| 213 | + Console.WriteLine("开始1 " + DateTime.Now.ToString("ss:fff")); |
| 214 | + } |
| 215 | + |
| 216 | + protected override void OnMouseLeave(EventArgs e) |
| 217 | + { |
| 218 | + base.OnMouseLeave(e); |
| 219 | + m_bMouseState = true; |
| 220 | + timerClose = new System.Threading.Timer(new TimerCallback(Timer_Elapsed), this, 0, 1); |
| 221 | + Console.WriteLine("开始2 " + DateTime.Now.ToString("ss:fff")); |
| 222 | + /* m_bMouseState = true; |
| 223 | + m_timerAnimated.Enabled = true;*/ |
| 224 | + } |
| 225 | + |
| 226 | + protected override void OnPaint(PaintEventArgs e) |
| 227 | + { |
| 228 | + base.OnPaint(e); |
| 229 | + |
| 230 | + if (m_bitmap == null) |
| 231 | + { |
| 232 | + PaintImage(); |
| 233 | + } |
| 234 | + e.Graphics.DrawImage(m_bitmap, new PointF(0, 0)); |
| 235 | +/* if(m_bitmapAnimated != null) |
| 236 | + { |
| 237 | + e.Graphics.DrawImage(m_bitmapAnimated, new PointF(0, 0)); |
| 238 | + }*/ |
| 239 | + Pen l_pen = new Pen(Color.DodgerBlue, 3f); |
| 240 | + e.Graphics.DrawLine(l_pen, 0, this.Height - 3, m_nLineLen,this.Height - 3); |
| 241 | + } |
| 242 | + |
| 243 | + protected override void OnSizeChanged(EventArgs e) |
| 244 | + { |
| 245 | + base.OnSizeChanged(e); |
| 246 | + |
| 247 | + BorderRadius = BorderRadius; |
| 248 | + PaintImage(); |
| 249 | + } |
| 250 | + #endregion |
| 251 | + } |
| 252 | +} |
0 commit comments