ProgressBar顯示進度值,垂直或者水平滾動條


過去一段時間,在研究Windows的系統控件ProgressBar,一直奇怪為啥它不能顯示進度值,本以為是個很簡單的問題,結果搜索很久,也沒有找到好的解決方案,最后終於找到一個Perfect方案,特記錄一下。

<一>比較蹩腳的方案:

用戶自定義控件,在系統的ProgressBar上面放一個Label,在每次進度改變時,修改Label上的Text。

蹩腳的地方:有很明顯的強制植入感覺,系統控件的透明色Transparent也不是真正透明的,Label在ProgressBar上,可以很明顯的感覺到就像一坨屎丟在了大馬路上,很顯眼。

 

<二>比較完美的方案

集成系統ProgressBar,重新繪制,在每次進度改變的時候,刷新一次即可,並且可以修改滾動條的方向:水平滾動條或者垂直滾動條。代碼如下:

  1  public class ProgressBarWithValue : ProgressBar
  2     {
  3         private ProgressBarDirection direction = ProgressBarDirection.Horizontal;
  4         private bool showPercent = true;
  5         private string customText;
  6 
  7         public ProgressBarWithValue()
  8         {
  9             SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
 10             base.Size = new Size(200, 15);
 11         }
 12 
 13         protected override void OnPaint(PaintEventArgs e)
 14         {
 15             Rectangle rect = ClientRectangle;
 16             Graphics g = e.Graphics;
 17             if (direction == ProgressBarDirection.Horizontal)
 18                 ProgressBarRenderer.DrawHorizontalBar(g, rect);
 19             else
 20                 ProgressBarRenderer.DrawVerticalBar(g, rect);
 21             rect.Inflate(-3, -3);
 22             if (Value > 0)
 23             {
 24                 if (direction == ProgressBarDirection.Horizontal)
 25                 {
 26                     Rectangle clip = new Rectangle(rect.X, rect.Y, (int)Math.Round(((float)Value / Maximum) * rect.Width), rect.Height);
 27                     ProgressBarRenderer.DrawHorizontalChunks(g, clip);
 28                 }
 29                 else
 30                 {
 31                     int height = (int)Math.Round(((float)Value / Maximum) * rect.Height);
 32                     Rectangle clip = new Rectangle(rect.X, rect.Y + (rect.Height - height), rect.Width, height);
 33                     ProgressBarRenderer.DrawVerticalChunks(g, clip);
 34                 }
 35             }
 36 
 37             string text = showPercent ? (Value.ToString() + '%') : CustomText;
 38 
 39             if (!string.IsNullOrEmpty(text))
 40                 using (Font f = new Font(FontFamily.GenericSerif, 10))
 41                 {
 42                     SizeF len = g.MeasureString(text, f);
 43                     Point location = new Point(Convert.ToInt32((Width / 2) - len.Width / 2), Convert.ToInt32((Height / 2) - len.Height / 2));
 44                     g.DrawString(text, f, Brushes.Red, location);
 45                 }
 46         }
 47 
 48 
 49         /// <summary>
 50         /// 進度條方向,水平或者垂直
 51         /// </summary>
 52         public ProgressBarDirection Direction
 53         {
 54             get { return direction; }
 55             set
 56             {
 57                 if (direction != value)
 58                 {
 59                     direction = value;
 60                     Invalidate();
 61                 }
 62             }
 63         }
 64 
 65         /// <summary>
 66         /// 是否顯示進度,true表示顯示進度,否則顯示自定義的字符串
 67         /// </summary>
 68         public bool ShowPercent
 69         {
 70             get { return showPercent; }
 71             set
 72             {
 73                 showPercent = value;
 74             }
 75         }
 76 
 77         /// <summary>
 78         /// 自定義需要顯示的字符串
 79         /// </summary>
 80         public string CustomText
 81         {
 82             get { return customText; }
 83             set
 84             {
 85                 if (customText != value)
 86                 {
 87                     customText = value;
 88                     if (!showPercent)
 89                         Invalidate();
 90                 }
 91             }
 92         }
 93     }
 94 
 95     public enum ProgressBarDirection
 96     {
 97         /// <summary>
 98         /// 垂直方向
 99         /// </summary>
100         Vertical,
101         /// <summary>
102         /// 水平方向
103         /// </summary>
104         Horizontal,
105     }

參考地址:http://stackoverflow.com/questions/3529928/how-do-i-put-text-on-progressbar


免責聲明!

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



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