C#/Winform实现Win8MetroLoading动画
2020-12-13 02:23
标签:des winform c style class blog 非常喜欢Metro风格的界面,所以想模仿一下一些UI效果的实现,网上找到了很多,但都是CSS3,WPF等实现,对于XAML和CSS3一窍不通,无奈下只有自己开始写。 下面是源码: Dot.cs Common.cs Form1.cs Form1.Designer.cs PS:有些地方肯定还有一些不协调,没办法,笔者审美有限,请大家通过代码内常量进行微调。欢迎大神们指点…… C#/Winform实现Win8MetroLoading动画,搜素材,soscw.com C#/Winform实现Win8MetroLoading动画 标签:des winform c style class blog 原文地址:http://www.cnblogs.com/CoffeeMX/p/3765304.html 1 using System.Drawing;
2
3 namespace MetroLoading
4 {
5 ///
1 using System;
2 using System.Drawing;
3
4 namespace MetroLoading
5 {
6 public static class Common
7 {
8 ///
1 using System;
2 using System.Drawing;
3 using System.Windows.Forms;
4 using System.Linq;
5 using System.Threading;
6
7 namespace MetroLoading
8 {
9 public partial class Form1 : Form
10 {
11 #region 字段
12
13 //点数组
14 private Dot[] _dots;
15
16 //数据tmr
17 private System.Threading.Timer _actionTmr;
18 //绘图tmr
19 private System.Windows.Forms.Timer _graphicsTmr;
20
21 //圆心
22 public static readonly Point CircleCenter = new Point(75, 75);
23 //半径
24 public static readonly int CircleRadius = 50;
25 //点大小
26 private static readonly Size DotSize = new Size(8, 8);
27
28 //是否绘制:用于状态重置时挂起与恢复绘图
29 private bool isDrawing = true;
30
31 //timer计数:用于延迟启动每个点
32 private int timerCount = 0;
33
34 #endregion 字段
35
36 #region 常量
37
38 //动作间隔
39 private const int actionInterval = 30;
40 //绘制间隔
41 private const int drawInterval = 1;
42
43 //计数基数:用于计算每个点启动延迟:index * timerCountRadix
44 private const int timerCountRadix = 40;
45
46 #endregion 常量
47
48 #region 构造
49
50 public Form1()
51 {
52 InitializeComponent();
53
54 //双缓冲,禁擦背景
55 this.SetStyle(
56 ControlStyles.AllPaintingInWmPaint
57 | ControlStyles.UserPaint
58 | ControlStyles.OptimizedDoubleBuffer,
59 true);
60
61 //初始化绘图timer
62 _graphicsTmr = new System.Windows.Forms.Timer();
63 _graphicsTmr.Interval = 1;
64 //Invalidate()强制重绘,绘图操作在OnPaint中实现
65 _graphicsTmr.Tick += (sender1, e1) => this.pictureBox1.Invalidate(false);
66
67 //初始化"点"
68 _dots = new Dot[5];
69 for (int i = 0; i i)
70 _dots[i] = new Dot();
71 }
72
73 #endregion 构造
74
75 //检查是否重置
76 private bool CheckToReset()
77 {
78 return _dots.Count((d) => d.Opacity > 0) == 0;
79 }
80
81 #region 事件处理
82
83 //开关
84 private void btnSwitch_Click(object sender, EventArgs e)
85 {
86 if (_graphicsTmr.Enabled)
87 {
88 _graphicsTmr.Stop();
89 _actionTmr.Dispose();
90 }
91 else
92 {
93 _graphicsTmr.Start();
94
95 //初始化动作timer
96 _actionTmr = new System.Threading.Timer(
97 (state) =>
98 {
99 //动画动作
100 for (int i = 0; i )
101 if (timerCount++ > i * timerCountRadix)
102 _dots[i].DotAction();
103
104 //是否重置
105 if (CheckToReset())
106 {
107 //重置前暂停绘图
108 isDrawing = false;
109
110 timerCount = 0;
111
112 for (int i = 0; i )
113 _dots[i].Reset();
114
115 //恢复绘图
116 isDrawing = true;
117 }
118
119 _actionTmr.Change(actionInterval, System.Threading.Timeout.Infinite);
120 },
121 null, actionInterval, System.Threading.Timeout.Infinite);
122 }
123 }
124
125 //重绘
126 private void pictureBox1_Paint(object sender, PaintEventArgs e)
127 {
128 if (isDrawing)
129 {
130 //抗锯齿
131 e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
132
133 using (Bitmap bmp = new Bitmap(200, 200))
134 {
135 //缓冲绘制
136 using (Graphics bufferGraphics = Graphics.FromImage(bmp))
137 {
138 //抗锯齿
139 bufferGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
140 for (int i = 0; i i)
141 {
142 RectangleF rect = new RectangleF(
143 new PointF(_dots[i].Location.X - DotSize.Width / 2, _dots[i].Location.Y - DotSize.Height / 2),
144 DotSize);
145
146 bufferGraphics.FillEllipse(new SolidBrush(Color.FromArgb(_dots[i].Opacity, Color.White)), rect);
147 }
148 }
149
150 //贴图
151 e.Graphics.DrawImage(bmp, new PointF(0, 0));
152 }//bmp disposed
153 }
154 }
155
156 #endregion 事件处理
157
158 }//end of class Form1
159 }//end of namespace
1 namespace MetroLoading
2 {
3 partial class Form1
4 {
5 ///
文章标题:C#/Winform实现Win8MetroLoading动画
文章链接:http://soscw.com/essay/25499.html