加载等待控件----------WinForm控件开发系列

  /// <summary>
  /// 加载进度控件
  /// </summary>
  [ToolboxItem(true)]
  [Description("加载进度控件")]
  [DefaultProperty("Active")]
  public partial class LoadProgressExt : Control
  {
    #region 属性

    private LoadProgressType progressType = LoadProgressType.Line;
    /// <summary>
    /// 加载进度条类型
    /// </summary>
    [Description("加载进度条类型")]
    [DefaultValue(typeof(LoadProgressType), "Line")]
    public LoadProgressType ProgressType
    {
      get { return this.progressType; }
      set
      {
        if (this.progressType == value)
          return;
        this.progressType = value;
        if (value == LoadProgressType.Line || value == LoadProgressType.Dot)
        {
          this.InitializeAngles();
          this.InitializeColors();
        }
        this.Invalidate();
      }
    }

    private int drawCircleRadius = 16;
    /// <summary>
    /// 画板半径大小(仅限Line、Dot、Arc、MultiArc)
    /// </summary>
    [Description("画板半径大小(仅限Line、Dot、Arc、MultiArc)")]
    [DefaultValue(16)]
    public int DrawCircleRadius
    {
      get
      {
        return this.drawCircleRadius;
      }
      set
      {
        if (this.drawCircleRadius == value && value < 1)
          return;
        this.drawCircleRadius = value;
        this.Invalidate();
      }
    }

    private int thickness = 4;
    /// <summary>
    /// 画笔粗细程度
    /// </summary>
    [Description("画笔粗细程度")]
    [DefaultValue(4)]
    public int Thickness
    {
      get
      {
        return this.thickness;
      }
      set
      {
        if (this.thickness == value || value < 1)
          return;
        this.thickness = value;
        this.Invalidate();
      }
    }

    private Color thicknessColor = Color.OliveDrab;
    /// <summary>
    /// 画笔颜色
    /// </summary>
    [Description("画笔颜色")]
    [DefaultValue(typeof(Color), "OliveDrab")]
    public Color ThicknessColor
    {
      get
      {
        return this.thicknessColor;
      }
      set
      {
        if (this.thicknessColor == value)
          return;
        this.thicknessColor = value;
        if (this.ProgressType == LoadProgressType.Line || this.ProgressType == LoadProgressType.Dot)
        {
          this.InitializeColors();
        }
        this.Invalidate();
      }
    }

    private int lineDotNumber = 8;
    /// <summary>
    /// 直线或圆点的数量(限制Line、Dot类型)
    /// </summary>
    [Description("直线或圆点的数量(限制Line、Dot类型)")]
    [DefaultValue(8)]
    public int LineDotNumber
    {
      get
      {
        return this.lineDotNumber;
      }
      set
      {
        if (this.lineDotNumber == value && value < 1)
          return;
        this.lineDotNumber = value;
        if (this.ProgressType == LoadProgressType.Line || this.ProgressType == LoadProgressType.Dot)
        {
          this.InitializeColors();
          this.InitializeAngles();
        }
        this.Invalidate();
      }
    }

    private int lineLenght = 8;
    /// <summary>
    /// 直线长度(仅限Line)
    /// </summary>
    [Description("直线长度(仅限Line)")]
    [DefaultValue(8)]
    public int LineLenght
    {
      get
      {
        return this.lineLenght;
      }
      set
      {
        if (this.lineLenght == value)
          return;
        this.lineLenght = value;
        this.Invalidate();
      }
    }

    private bool active = false;
    /// <summary>
    /// 是否激活
    /// </summary>
    [Description("是否激活")]
    [DefaultValue(false)]
    public bool Active
    {
      get
      {
        return this.active;
      }
      set
      {
        if (this.active == value)
          return;
        this.active = value;
        this.positiveProgress = 0;
        this.negativeProgress = 0;
        if (this.ProgressType == LoadProgressType.Line || this.ProgressType == LoadProgressType.Dot)
        {
          this.InitializeColors();
        }
        this.timer.Enabled = value;
        this.Invalidate();
      }
    }

    private bool textShow = false;
    /// <summary>
    /// 是否显示文本
    /// </summary>
    [Description("是否显示文本")]
    [DefaultValue(false)]
    public bool TextShow
    {
      get
      {
        return this.textShow;
      }
      set
      {
        if (this.textShow == value)
          return;
        this.textShow = value;
        this.Invalidate();
      }
    }

    /// <summary>
    /// 动画时间间隔
    /// </summary>
    [Description("动画时间间隔")]
    [DefaultValue(100)]
    public int Interval
    {
      get
      {
        return this.timer.Interval;
      }
      set
      {
        if (this.timer.Interval == value)
          return;
        this.timer.Interval = value;
      }
    }

    protected override Size DefaultSize
    {
      get
      {
        return new Size(40, 40);
      }
    }

    /// <summary>
    /// 定时器
    /// </summary>
    private Timer timer;
    /// <summary>
    /// 正向进度条值
    /// </summary>
    private int positiveProgress = 0;
    /// <summary>
    /// 负向进度条值
    /// </summary>
    private int negativeProgress = 0;
    /// <summary>
    /// 每条直线角度
    /// </summary>
    private List<double> lineAngles;
    /// <summary>
    /// 每条直线颜色
    /// </summary>
    private List<Color> lineColors;

    #endregion

    public LoadProgressExt()
    {
      this.SetStyle(ControlStyles.UserPaint, true);
      this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
      this.SetStyle(ControlStyles.ResizeRedraw, true);
      this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
      this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
      InitializeComponent();

      this.InitializeAngles();
      this.InitializeColors();

      this.timer = new Timer();
      this.timer.Interval = this.Interval;
      this.timer.Tick += new EventHandler(this.timer_Tick);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
      base.OnPaint(e);

      Graphics g = e.Graphics;
      g.SmoothingMode = SmoothingMode.AntiAlias;
      g.CompositingQuality = CompositingQuality.HighQuality;
      g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;

      #region 图案
      switch (this.ProgressType)
      {
        case LoadProgressType.Line:
          {
            #region
            int node = this.positiveProgress;
            for (int i = 0; i < this.LineDotNumber; i++)
            {
              node = node % this.LineDotNumber;
              PointF point1 = this.GetCoordinate(e.ClipRectangle, this.DrawCircleRadius - this.LineLenght, this.lineAngles[node]);
              PointF point2 = this.GetCoordinate(e.ClipRectangle, this.DrawCircleRadius, this.lineAngles[node]);

              Pen line_pen = new Pen(this.lineColors[i], this.Thickness);
              line_pen.StartCap = LineCap.Round;
              line_pen.EndCap = LineCap.Round;
              g.DrawLine(line_pen, point1, point2);
              line_pen.Dispose();

              node++;
            }
            break;
            #endregion
          }
        case LoadProgressType.Dot:
          {
            #region
            int node = this.positiveProgress;
            for (int i = 0; i < this.LineDotNumber; i++)
            {
              node = node % this.LineDotNumber;
              PointF point = this.GetCoordinate(e.ClipRectangle, this.DrawCircleRadius, this.lineAngles[node]);

              SolidBrush dot_sb = new SolidBrush(this.lineColors[i]);
              g.FillEllipse(dot_sb, point.X - this.Thickness / 2, point.Y - this.Thickness / 2, this.Thickness, this.Thickness);
              dot_sb.Dispose();

              node++;
            }
            break;
            #endregion
          }
        case LoadProgressType.Arc:
          {
            #region
            int arc_w = this.DrawCircleRadius * 2;
            int arc_h = this.DrawCircleRadius * 2;
            Rectangle arc_rect = new Rectangle((e.ClipRectangle.Width - arc_w) / 2, (e.ClipRectangle.Height - arc_h) / 2, arc_w, arc_h);

            Pen arc_pen = new Pen(this.ThicknessColor, this.Thickness);
            g.DrawArc(arc_pen, arc_rect, this.positiveProgress, 290);
            arc_pen.Dispose();
            break;
            #endregion
          }
        case LoadProgressType.MultiArc:
          {
            #region
            int multiArc_w = this.DrawCircleRadius * 2;
            int multiArc_h = this.DrawCircleRadius * 2;
            Rectangle multiArc_rect = new Rectangle((e.ClipRectangle.Width - multiArc_w) / 2, (e.ClipRectangle.Height - multiArc_h) / 2, multiArc_w, multiArc_h);

            Pen multiArc_pen = new Pen(this.ThicknessColor, this.thickness);
            g.DrawArc(multiArc_pen, multiArc_rect, this.positiveProgress, 90);//外1弧度

            int out2 = this.positiveProgress + 180;
            if (out2 > 360)
              out2 -= 360;
            g.DrawArc(multiArc_pen, multiArc_rect, out2, 90);//外2弧度

            Rectangle interior_rect = new Rectangle(multiArc_rect.X + multiArc_rect.Width / 4, multiArc_rect.Y + multiArc_rect.Height / 4, multiArc_rect.Width / 2, multiArc_rect.Height / 2);
            int interior1 = this.negativeProgress + 90;
            if (interior1 > 360)
              interior1 -= 360;
            g.DrawArc(multiArc_pen, interior_rect, interior1, 90);//内1弧度

            int interior2 = interior1 + 180;
            if (interior2 > 360)
              interior2 -= 360;
            g.DrawArc(multiArc_pen, interior_rect, interior2, 90);//内2弧度

            multiArc_pen.Dispose();
            break;
            #endregion
          }
        case LoadProgressType.Bar:
          {
            #region
            int line_c = 9;//线条数量
            int bar_w = this.Thickness * line_c;
            int bar_h = this.Thickness * line_c;
            int bar_x = (e.ClipRectangle.Width - bar_w) / 2;
            int bar_y = (e.ClipRectangle.Height - bar_h) / 2;
            int line_min_h = bar_h / 2;

            Pen bar_pen = new Pen(this.ThicknessColor, this.Thickness);
            for (int i = 0; i < line_c; i++)
            {
              if (i % 2 == 0)
              {
                int num = i / 2;//只绘制偶数线条
                float differ = Math.Abs(this.positiveProgress - num);//指定线条与进度值线条的差用于计算缩放比例

                float reality_h = bar_h - differ * (bar_h / 4);//线条要绘制的高度
                if (reality_h < line_min_h)
                  reality_h = line_min_h;

                float line_x1 = bar_x + i * this.Thickness;
                float line_y1 = bar_y + (bar_h - reality_h) / 2;
                float line_x2 = line_x1;
                float line_y2 = bar_y + (bar_h - reality_h) / 2 + reality_h;
                g.DrawLine(bar_pen, line_x1, line_y1, line_x2, line_y2);
              }
            }
            bar_pen.Dispose();
            break;
            #endregion
          }
      }
      #endregion

      #region 文本
      if (this.TextShow && !String.IsNullOrWhiteSpace(this.Text))
      {
        SolidBrush text_sb = new SolidBrush(this.ForeColor);
        StringFormat text_sf = new StringFormat();
        text_sf.Alignment = StringAlignment.Center;
        text_sf.LineAlignment = StringAlignment.Far;
        text_sf.Trimming = StringTrimming.EllipsisCharacter;
        SizeF text_sizef = g.MeasureString(this.Text, this.Font);
        g.DrawString(this.Text, this.Font, text_sb, new RectangleF(0.0f, e.ClipRectangle.Bottom - text_sizef.Height, (float)e.ClipRectangle.Width, text_sizef.Height), text_sf);
      }
      #endregion

    }

    /// <summary>
    /// 进度定时器
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void timer_Tick(object sender, EventArgs e)
    {
      switch (this.ProgressType)
      {
        case LoadProgressType.Arc:
          {
            this.positiveProgress += 25;
            if (this.positiveProgress > 360)
              this.positiveProgress -= 360;
            break;
          }
        case LoadProgressType.MultiArc:
          {
            this.positiveProgress += 25;
            if (this.positiveProgress > 360)
              this.positiveProgress -= 360;

            this.negativeProgress -= 28;
            if (this.negativeProgress < 0)
              this.negativeProgress += 360;
            break;
          }
        case LoadProgressType.Bar:
          {
            this.positiveProgress += 1;
            if (this.positiveProgress > 4)
              this.positiveProgress -= 4;
            break;
          }
        default:
          {
            this.positiveProgress += 1;
            if (this.positiveProgress > this.LineDotNumber - 1)
              this.positiveProgress -= this.LineDotNumber - 1;
            break;
          }
      }
      this.Invalidate();
    }

    /// <summary>
    /// 初始化每条直线或圆点角度
    /// </summary>
    /// <returns></returns>
    private void InitializeAngles()
    {
      this.lineAngles = new List<double>();
      double avgAngle = 360d / (double)this.LineDotNumber;
      for (int i = 0; i < this.LineDotNumber; i++)
      {
        this.lineAngles.Add((i + 1) * avgAngle);
      }
    }

    /// <summary>
    /// 初始化每条直线或圆点颜色
    /// </summary>
    /// <returns></returns>
    private void InitializeColors()
    {
      this.lineColors = new List<Color>();

      byte transparent = 0;//颜色透明度
      byte transparentIncrement = (byte)(byte.MaxValue / this.LineDotNumber);//颜色透明度增量
      for (int i = 0; i < this.LineDotNumber; i++)
      {
        Color color = this.ThicknessColor;
        if (this.Active && i > 0)
        {
          transparent += transparentIncrement;
          transparent = Math.Min(transparent, byte.MaxValue);
          color = Color.FromArgb(transparent, Math.Min(this.ThicknessColor.R, byte.MaxValue), Math.Min(this.ThicknessColor.G, byte.MaxValue), Math.Min(this.ThicknessColor.B, byte.MaxValue));
        }
        this.lineColors.Add(color);
      }
    }

    /// <summary>
    /// 已绘图区中心坐标根据角度获取圆环上对应坐标
    /// </summary>
    /// <param name="rect">绘图区</param>
    /// <param name="radius">圆环半径</param>
    /// <param name="angle">角度</param>
    /// <returns>圆环上对应坐标</returns>
    private PointF GetCoordinate(Rectangle rect, int radius, double angle)
    {
      //经纬度转化成弧度(弧度=经纬度 * Math.PI / 180d);
      double dblAngle = angle * Math.PI / 180d;
      return new PointF(rect.Width / 2 + radius * (float)Math.Cos(dblAngle), rect.Height / 2 + radius * (float)Math.Sin(dblAngle));
    }

    protected override void Dispose(bool disposing)
    {
      if (disposing && (components != null))
      {
        components.Dispose();
        if (this.timer != null)
          this.timer.Dispose();
      }
      base.Dispose(disposing);
    }

    /// <summary>
    /// 直线或圆点的角度和颜色
    /// </summary>
    [Description("直线或圆点的角度和颜色")]
    public class AnglesColor
    {
      /// <summary>
      /// 直线或圆点的角度
      /// </summary>
      [Description("直线或圆点的角度")]
      public double Angles { get; set; }
      /// <summary>
      /// 直线或圆点的颜色
      /// </summary>
      [Description("直线或圆点的颜色")]
      public Color Color { get; set; }
    }

  }

  /// <summary>
  /// 加载进度条类型
  /// </summary>
  [Description("加载进度条类型")]
  public enum LoadProgressType
  {
    /// <summary>
    /// 直线
    /// </summary>
    Line,
    /// <summary>
    /// 圆球
    /// </summary>
    Dot,
    /// <summary>
    ///圆弧
    /// </summary>
    Arc,
    /// <summary>
    /// 多圆弧
    /// </summary>
    MultiArc,
    /// <summary>
    /// 条形
    /// </summary>
    Bar
  }

源码下载:加载等待控件.zip

原文地址:https://www.cnblogs.com/tlmbem/p/11204610.html

时间: 2024-10-10 02:41:16

加载等待控件----------WinForm控件开发系列的相关文章

自定义加载等待框(MBProgressHUD)

一.网上下载  MBProgessHUD 类文件,直接导入到工程即可 二.示例分析 在我的工程中示例如下: 1)在ShowImageViewController.h头文件代码如下: #import <UIKit/UIKit.h> #import "MBProgressHUD.h" @interface ShowImageViewController : UIViewController<MBProgressHUDDelegate> { NSString     

IOS开发UI篇之──自定义加载等待框(MBProgressHUD)

本文转载至 http://blog.csdn.net/xunyn/article/details/8064984 原文地址http://www.189works.com/article-89289-1.html MBProgressHUD 下载地址是: http://github.com/matej/MBProgressHUD 这里介绍一下网友开源的MBProgressHUD类,实现等待框, 一.网上下载  MBProgessHUD 类文件,直接导入到工程即可 二.示例分析 在我的工程中示例如下

按钮样式加载等待

使用插件Ladda,基于bootstrap的样式实现加载等待的效果: 首先引用.css以及.js文件: <link rel="stylesheet" href="css/bootstrap.min.css"> <link rel="stylesheet" href="dist/ladda-themeless.min.css"> <script src="dist/spin.min.js&

页面加载时让其显示笼罩层与加载等待图片

页面加载时让其显示笼罩层与加载等待图片(结局比较完美,过程很坎坷,所以一定总结整理下,备用): 用了ajax异步,是因为js内容不能即时的显示出来,因为js是单线程,要把队列中的任务执行完后才会执行刚才对js的处理 要用beforeSend,complete的时候必须要用ajax异步 beforeSend: function () {},//程序一开始便会执行该函数,使用该方法必须使用异步ajax complete: function () {},//complete在success或error

iOS UIAlertView中UIActivityindicatorView风火轮提示加载等待

参考:http://stackoverflow.com/questions/18729220/uialertview-addsubview-in-ios7 1.SignInViewController.h #import <UIKit/UIKit.h> @interface SignInViewController : UIViewController<UIAlertViewDelegate>{ UIAlertView *remoteAlertView; } @end 2.Sign

jquery Mobile点击显示加载等待效果

点击某个按钮或链接时,触发等待加载效果: Xml代码   <script> <!-- $(document).bind("mobileinit", function(){ }); $( function() { //默认设置,一个小圈圈在转 $('#default').live( 'tap', function() { $.mobile.loadingMessageTextVisible = false; $.mobile.showPageLoadingMsg();

Jquery ajax加载等待执行结束再继续执行下面代码操作

Jquery等待ajax执行完毕再继续执行下面代码的效果,具体代码如下,其实就是将 jquery ajax 函数的 async 参数设置为 false 即可,该参数默认为 true: $(document).ready(function(){ loadphpernote(); window.open('http://www.phpernote.com'); }); function loadphpernote(){ var url='http://www.phpernote.com/ajax.p

BitmapImage处理网络图片,例如阿里云获取的图片。异步加载到需要显示的控件上。提升速度非常明显。

想直接把网络图片赋给控件,又要下载又要缓存,速度非常慢.不流畅. 需要进行处理,异步加载会显著提升速度.方法如下: public static BitmapImage ByteArrayToBitmapImage(byte[] byteArray) { BitmapImage bmp = null; try { bmp = new BitmapImage(); bmp.BeginInit(); bmp.StreamSource = new MemoryStream(byteArray); bmp

ASP.NET中页面加载时文本框(texbox控件)内有文字获得焦点时文字消失

代码如下: <asp:TextBox ID="TextBox1" runat="server" Height="26px" MaxLength="10" Width="166px" Text="请输入用户名" OnFocus="javascript:if(this.value=='请输入用户名') {this.value='';this.style.color='#000