溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#怎么實(shí)現(xiàn)loading提示控件

發(fā)布時(shí)間:2020-09-22 09:41:41 來(lái)源:億速云 閱讀:130 作者:小新 欄目:編程語(yǔ)言

C#怎么實(shí)現(xiàn)loading提示控件?這個(gè)問(wèn)題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見(jiàn)到的。希望通過(guò)這個(gè)問(wèn)題能讓你收獲頗深。下面是小編給大家?guī)?lái)的參考內(nèi)容,讓我們一起來(lái)看看吧!

自己畫一個(gè)轉(zhuǎn)圈圈的控件

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();
    }
    //計(jì)算各種圈圈相關(guān)
    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  垂直
        //解方程得兩點(diǎn)即為所求點(diǎn)
        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兩點(diǎn)重合會(huì)報(bào)錯(cuò),那樣就返回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);
      }
    }
  }
}

感謝各位的閱讀!看完上述內(nèi)容,你們對(duì)C#怎么實(shí)現(xiàn)loading提示控件大概了解了嗎?希望文章內(nèi)容對(duì)大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI