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繪制,動畫效果等學習。

 

后期如果使用更多的這種效果再來繼續更新!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM