C#实现简单的loading提示控件实例代码
自己画一个转圈圈的控件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ExerciseUIPrj.controls { public partial class LoadControl : Control { Color beginColor = Color.Blue; Color endColor = Color.Red; int wid = 10; int curindex = 0; Timer timer; int instervel = 200; string loadStr = "loading...."; public LoadControl() { InitializeComponent(); SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint|ControlStyles.OptimizedDoubleBuffer, true); this.MinimumSize = new Size(40, 80); if (!DesignMode) { Start(); } } public void Start() { if (timer == null) { timer = new Timer(); timer.Interval = instervel; timer.Tick += Timer_Tick; } timer.Enabled = true; } public void Stop() { if (timer != null) { timer.Enabled = false; } } void Timer_Tick(object sender, EventArgs e) { curindex++; curindex = curindex >= wid ? 0 : curindex; Refresh(); } //计算各种圈圈相关 Point getPoint(double d, double r, Point center) { int x = (int)(r * Math.Cos(d * Math.PI / 180.0)); int y = (int)(r * Math.Sin(d * Math.PI / 180.0)); return new Point(center.X + x, center.Y - y); } GraphicsPath getPath(Point a, Point b) { Point c, d, e, f; int h = 2; Vertical(a, b, h, out c, out d); Vertical(b, a, h, out e, out f); GraphicsPath path = new GraphicsPath(); path.AddPolygon(new Point[] { c, d, e, f }); path.CloseAllFigures(); return path; } bool Vertical(Point pointa, Point pointb, double R, out Point pointc, out Point pointd) { pointc = new Point(); pointd = new Point(); try { //(X-xa)^2+(Y-ya)^2=R*R 距离公式 //(X-xa)*(xb-xa)+(Y-ya)*(yb-ya)=0 垂直 //解方程得两点即为所求点 var cx = pointa.X - (pointb.Y - pointa.Y) * R / Distance(pointa, pointb); var cy = pointa.Y + (pointb.X - pointa.X) * R / Distance(pointa, pointb); var dx = pointa.X + (pointb.Y - pointa.Y) * R / Distance(pointa, pointb); var dy = pointa.Y - (pointb.X - pointa.X) * R / Distance(pointa, pointb); pointc = new Point((int)cx, (int)cy); pointd = new Point((int)dx, (int)dy); return true; } catch { //如果A,B两点重合会报错,那样就返回false return false; } } double Distance(double xa, double ya, double xb, double yb) { double L; L = Math.Sqrt(Math.Pow(xa - xb, 2) + Math.Pow(ya - yb, 2)); return L; } double Distance(Point pa, Point pb) { return Distance(pa.X, pa.Y, pb.X, pb.Y); } GraphicsPath getPath(double d, double r, Point c) { var p1 = getPoint(d, r / 2.0, c); var p2 = getPoint(d, r, c); return getPath(p1, p2); } //算渐变色 Color[] getColors() { int dr = (int)((endColor.R - beginColor.R) / (double)wid); int dg = (int)((endColor.G - beginColor.G) / (double)wid); int db = (int)((endColor.B - beginColor.B) / (double)wid); List<Color> colors = new List<Color>(); for (int i = 0; i < wid; i++) { colors.Add(Color.FromArgb(beginColor.R + dr * i, beginColor.G + dg * i, beginColor.B + db * i)); } return colors.ToArray(); } //画圈圈 void drawRect(Graphics g) { int r = (int)(Size.Height / 2.0); Point center = new Point(r, r); var colors = getColors(); int findex = curindex; for (int i = 0; i < wid; i++) { double d = (360.0 / wid) * i; var p = getPath(d, r, center); int cindex = findex + i; cindex = cindex >= wid ? cindex - wid : cindex; g.FillPath(new SolidBrush(colors[cindex]), p); } } //画字符串 void drawString(Graphics g) { if (Size.Height >= Size.Width) return; Rectangle rect = new Rectangle(new Point(Size.Height, 0), new Size(Size.Width - Size.Height, Size.Height)); StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; g.DrawString(loadStr, Font, Brushes.Black, rect,sf); } protected override void OnPaint(PaintEventArgs pe) { base.OnPaint(pe); Graphics g = pe.Graphics; g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; drawRect(g); drawString(g); } protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); if (Size.Height > Size.Width) { Size = new Size(Size.Height, Size.Height); } } } }
总结
以上所述是小编给大家介绍的C#实现简单的loading提示控件实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
栏 目:C#教程
下一篇:C# 文件上传下载(Excel导入,多线程下载)功能的实现代码
本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/5513.html
您可能感兴趣的文章
- 01-10C#通过反射获取当前工程中所有窗体并打开的方法
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10C#停止线程的方法
- 01-10WinForm实现仿视频播放器左下角滚动新闻效果的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的方法
- 01-10C#实现清空回收站的方法
- 01-10C#实现读取注册表监控当前操作系统已安装软件变化的方法
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 01-11ajax实现页面的局部加载
- 01-10C#中split用法实例总结
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 04-02jquery与jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10SublimeText编译C开发环境设置