C#實(shí)現(xiàn)簡(jiǎn)單的loading提示控件實(shí)例代碼
自己畫一個(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);
      }
    }
  }
}
總結(jié)
以上所述是小編給大家介紹的C#實(shí)現(xiàn)簡(jiǎn)單的loading提示控件實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
上一篇:C# 中SharpMap的簡(jiǎn)單使用實(shí)例詳解
欄 目:C#教程
下一篇:C# 文件上傳下載(Excel導(dǎo)入,多線程下載)功能的實(shí)現(xiàn)代碼
本文標(biāo)題:C#實(shí)現(xiàn)簡(jiǎn)單的loading提示控件實(shí)例代碼
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5513.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
 - 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
 - 01-10C#停止線程的方法
 - 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法
 - 01-10C#通過重寫Panel改變邊框顏色與寬度的方法
 - 01-10C#實(shí)現(xiàn)清空回收站的方法
 - 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
 


閱讀排行
- 1C語言 while語句的用法詳解
 - 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
 - 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
 - 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
 - 5c語言計(jì)算三角形面積代碼
 - 6什么是 WSH(腳本宿主)的詳細(xì)解釋
 - 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
 - 8正則表達(dá)式匹配各種特殊字符
 - 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
 - 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
 
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
 - 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
 - 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
 - 01-10C#停止線程的方法
 - 01-10C#實(shí)現(xiàn)清空回收站的方法
 - 01-10C#通過重寫Panel改變邊框顏色與寬度的
 - 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
 
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
 - 01-10C#中split用法實(shí)例總結(jié)
 - 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
 - 04-02jquery與jsp,用jquery
 - 01-10delphi制作wav文件的方法
 - 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
 - 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
 - 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 


