C#如何实现loading提示控件简单的实例

本文通过实例代码给大家介绍了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 colors = new List();      for (int i = 0; i = 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提示控件简单的实例的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2438314.html

(0)
上一篇 2025年3月3日 11:37:27
下一篇 2025年2月26日 07:57:55

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论