欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现简单的loading提示控件实例代码

程序员文章站 2023-12-10 17:06:58
自己画一个转圈圈的控件 using system; using system.collections.generic; using system.compon...

自己画一个转圈圈的控件

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提示控件实例代码,希望对大家有所帮助