Winform--实现加载数据时动态等待/加载效果

最近在遇到数据加载耗时比较长,想用一个动态等待效果来告诉用户程序还没死,以下是实现过程。

实现两种效果:一种条形进度条,不停滚动;一种有几个红点在动态循环。原本想上传视频更清楚一点,无奈不太会用,丢人了……

第一种效果:

条形进度条

优势:极其简单,直接使用的是winform里面的控件—progressbar,将其style属性设置为Marquee,MarqueeAnimationSpeed属性定义其滚动速度。在合适的地方调用它就能实现功能了。该控件还可以跟BackGroundWorker配合使用,实时显示加载进度。

缺点:太过依赖于控件了,我们都知道并不是所有时候都能用控件的,比如说progressbar能使用的平台是受限的(见MSDN)。

问题:还不知道如何改变中间绿色滚动的宽度。

第二种效果:

红色转圈

优势:仅使用picturebox、timer两种比较常见的控件,可替代性强。

缺点:代码量要多一点,理解难度要大一点。

问题:还没完全看懂代码,比如如何改变点的大小、形状等等。

引用Drawing来绘制:

  1 using System.Drawing.Drawing2D;
  2 #region 等待界面
  3         private int count = -1;
  4         private ArrayList images = new ArrayList();
  5         public Bitmap[] bitmap = new Bitmap[8];
  6         private int _value = 1;
  7         private Color _circleColor = Color.Red;
  8         private float _circleSize = 0.8f;      
  9 
 10         private int width = 200;//设置圆的宽
 11         private int height = 200;////设置圆的高
 12 
 13         public Bitmap DrawCircle(int j)
 14         {
 15             const float angle = 360.0F / 8;
 16             Bitmap map = new Bitmap(150, 150);
 17             Graphics g = Graphics.FromImage(map);
 18 
 19             g.TranslateTransform(width / 2.0F, height / 2.0F);
 20             g.RotateTransform(angle * _value);
 21             g.InterpolationMode = InterpolationMode.HighQualityBicubic;
 22             g.SmoothingMode = SmoothingMode.AntiAlias;
 23             int[] a = new int[8] { 25, 50, 75, 100, 125, 150, 175, 200 };
 24             for (int i = 1; i <= 8; i++)
 25             {
 26                 int alpha = a[(i + j - 1) % 8];
 27                 Color drawColor = Color.FromArgb(alpha, _circleColor);
 28                 using (SolidBrush brush = new SolidBrush(drawColor))
 29                 {
 30                     float sizeRate = 3.5F / _circleSize;
 31                     float size = width / (6 * sizeRate);
 32                     float diff = (width / 10.0F) - size;
 33                     float x = (width / 80.0F) + diff;
 34                     float y = (height / 80.0F) + diff;
 35                     g.FillEllipse(brush, x, y, size, size);
 36                     g.RotateTransform(angle);
 37                 }
 38             }
 39             return map;
 40         }
 41 
 42         public void Draw()
 43         {
 44             for (int j = 0; j < 8; j++)
 45             {
 46                 bitmap[7 - j] = DrawCircle(j);
 47             }
 48         }
 49 
 50         protected override void OnResize(EventArgs e)
 51         {
 52             SetNewSize();
 53             base.OnResize(e);
 54         }
 55 
 56         protected override void OnSizeChanged(EventArgs e)
 57         {
 58             SetNewSize();
 59             base.OnSizeChanged(e);
 60         }
 61 
 62         private void SetNewSize()
 63         {
 64             int size = Math.Max(width, height);
 65             pictureBox.Size = new Size(size, size);
 66         }
 67 
 68         public void set()
 69         {
 70             for (int i = 0; i < 8; i++)
 71             {
 72                 Draw();
 73                 Bitmap map = new Bitmap((bitmap[i]), new Size(120, 110));
 74                 images.Add(map);
 75             }
 76             pictureBox.Image = (Image)images[0];
 77             pictureBox.Size = pictureBox.Image.Size;
 78         }
 79        
 80         private void Timer_Tick(object sender, EventArgs e)
 81         {
 82             set();
 83             count = (count + 1) % 8;
 84             pictureBox.Image = (Image)images[count];
 85         }
 86         
 87         private void StartWaiting()
 88         {
 89             timer1.Start();
 90             pictureBox.Visible = true;
 91 
 92             progressBar1.Visible = true;
 93             progressBar1.Enabled = true;
 94         }
 95 
 96         private void StopWaiting()
 97         {
 98             timer1.Stop();
 99             pictureBox.Visible = false;
100 
101             progressBar1.Visible = false;
102             progressBar1.Enabled = false;
103         }
104 
105         #endregion
View Code

代码参考:http://www.jb51.net/article/46069.htm

需求加强对drawing类的学习,GDI绘制,动画效果等学习。

 

后期如果使用更多的这种效果再来继续更新!

posted @ 2017-11-23 12:54  EasonDongH  阅读(8444)  评论(1编辑  收藏  举报