SoFunction
Updated on 2025-03-06

C# implements simple loading prompt control instance code

Draw a circle control yourself

using System;
using ;
using ;
using ;
using ;
using .Drawing2D;
using ;
using ;
using ;
using ;
namespace 
{
  public partial class LoadControl : Control
  {
    Color beginColor = ;
    Color endColor = ;
    int wid = 10;
    int curindex = 0;
    Timer timer;
    int instervel = 200;
    string loadStr = "loading....";
    public LoadControl()
    {
      InitializeComponent();
      SetStyle( | |, true);
       = new Size(40, 80);
      if (!DesignMode)
      {
        Start();
      }
    }
    public void Start()
    {
      if (timer == null)
      {
        timer = new Timer();
         = instervel;
         += Timer_Tick;
      }
       = true;
    }
    public void Stop()
    {
      if (timer != null)
      {
         = false;
      }
    }
    void Timer_Tick(object sender, EventArgs e)
    {
      curindex++;
      curindex = curindex >= wid ? 0 : curindex;
      Refresh();
    }
    // Calculate various circle related    Point getPoint(double d, double r, Point center)
    {
      int x = (int)(r * (d *  / 180.0));
      int y = (int)(r * (d *  / 180.0));
      return new Point( + x,  - 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();
      (new Point[] { c, d, e, f });
      ();
      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 Distance formula        //(X-xa)*(xb-xa)+(Y-ya)*(yb-ya)=0 Vertical        //Solve two points of the equation and you are looking for        var cx =  - ( - ) * R / Distance(pointa, pointb);
        var cy =  + ( - ) * R / Distance(pointa, pointb);
        var dx =  + ( - ) * R / Distance(pointa, pointb);
        var dy =  - ( - ) * R / Distance(pointa, pointb);
        pointc = new Point((int)cx, (int)cy);
        pointd = new Point((int)dx, (int)dy);
        return true;
      }
      catch
      {
        //If the points A and B overlap, an error will be reported, and then false will be returned.        return false;
      }
    }
    double Distance(double xa, double ya, double xb, double yb)
    {
      double L;
      L = ((xa - xb, 2) + (ya - yb, 2));
      return L;
    }
    double Distance(Point pa, Point pb)
    {
      return Distance(, , , );
    }
    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);
    }
    //Calculate the gradient color    Color[] getColors()
    {
      int dr = (int)(( - ) / (double)wid);
      int dg = (int)(( - ) / (double)wid);
      int db = (int)(( - ) / (double)wid);
      List<Color> colors = new List<Color>();
      for (int i = 0; i < wid; i++)
      {
        (( + dr * i,  + dg * i,  + db * i));
      }
      return ();
    }
    //Draw circles    void drawRect(Graphics g)
    {
      int r = (int)( / 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;
        (new SolidBrush(colors[cindex]), p);
      }
    }
    //Draw string    void drawString(Graphics g)
    {
      if ( >= ) return;
      Rectangle rect = new Rectangle(new Point(, 0), new Size( - , ));
      StringFormat sf = new StringFormat();
       = ;
       = ;
      (loadStr, Font, , rect,sf);
    }
    protected override void OnPaint(PaintEventArgs pe)
    {
      (pe);
      Graphics g = ;
       = ;
       = ;
      drawRect(g);
      drawString(g);
    }
    protected override void OnSizeChanged(EventArgs e)
    {
      (e);
      if ( > )
      {
        Size = new Size(, );
      }
    }
  }
}

Summarize

The above is the example code of the simple loading prompt control for C# implementation introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message. The editor will reply to everyone in time!