十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章将为大家详细讲解有关C#如何实现简单的loading提示控件,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
专注于为中小企业提供网站建设、成都网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业资溪免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。自己画一个转圈圈的控件
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); Listcolors = new List (); 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#是一个简单、通用、面向对象的编程语言,它由微软Microsoft开发,继承了C和C++强大功能,并且去掉了一些它们的复杂特性,C#综合了VB简单的可视化操作和C++的高运行效率,以其强大的操作能力、优雅的语法风格、创新的语言特性和便捷的面向组件编程从而成为.NET开发的选语言,但它不适用于编写时间急迫或性能非常高的代码,因为C#缺乏性能极高的应用程序所需要的关键功能。
关于“C#如何实现简单的loading提示控件”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。