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

 更新时间:2017年09月03日 11:50:39   作者:季末的寂寞  
本文通过实例代码给大家介绍了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<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提示控件实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

相关文章

  • C#基于Socket的TCP通信实现聊天室案例

    C#基于Socket的TCP通信实现聊天室案例

    这篇文章主要为大家详细介绍了C#基于Socket的TCP通信实现聊天室案例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • C#影院售票系统毕业设计(4)

    C#影院售票系统毕业设计(4)

    这篇文章主要介绍了C#影院售票系统毕业设计,学习内容是总结销售信息的保存以及加载销售信息,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2015-11-11
  • 详解C# Socket异步通信实例

    详解C# Socket异步通信实例

    本篇文章主要介绍了C# Socket异步通信,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • C# 如何获取处于运行中的Excel、Word对象

    C# 如何获取处于运行中的Excel、Word对象

    这篇文章主要介绍了C# 获取处于运行中的Excel、Word对象操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • C#线程间不能调用剪切板的解决方法

    C#线程间不能调用剪切板的解决方法

    这篇文章主要介绍了C#线程间不能调用剪切板的解决方法,需要的朋友可以参考下
    2014-07-07
  • C#使用融合通信API发送手机短信功能

    C#使用融合通信API发送手机短信功能

    融合云通信服务平台,为企业提供全方位通信服务,发送手机短信是其一项核心功能,本文介绍了如何使用融合云通信服务平台的API通过C#实现发送手机短信的功能,感兴趣的朋友跟随小编一起看看吧
    2024-11-11
  • 利用unity代码C#封装为dll的步骤分享

    利用unity代码C#封装为dll的步骤分享

    这篇文章主要给大家介绍了关于利用unity代码C#封装为dll的相关资料,文中通过图文将实现的方法介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-05-05
  • C# 减少嵌套循环的两种方法

    C# 减少嵌套循环的两种方法

    最近在解决性能优化的问题,看到了一堆嵌套循环,四五层级的循环真的有点过分了,在数据量成万,十万级别的时候,真的非常影响性能。本文介绍了C# 减少嵌套循环的两种方法,帮助各位选择适合自己的优化方案,优化程序性能
    2021-06-06
  • C#使用zxing/zbar/thoughtworkQRcode解析二维码的示例代码

    C#使用zxing/zbar/thoughtworkQRcode解析二维码的示例代码

    zxing是谷歌开源的二维码库,zbar,thoughtworkQRcode也是开源的,三者之间比较各有优劣,本文将通过一个案例demo源码,带来认识学习下这三者的实际解码效果,感兴趣的可以了解一下
    2023-07-07
  • C#编写ActiveX网页截图控件

    C#编写ActiveX网页截图控件

    这篇文章主要介绍了C#编写ActiveX网页截图控件,作为学习C#编写ActiveX的一个简单入门教程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-06-06

最新评论