过去一段时间,在研究Windows的系统控件ProgressBar,一直奇怪为啥它不能显示进度值,本以为是个很简单的问题,结果搜索很久,也没有找到好的解决方案,最后终于找到一个Perfect方案,特记录一下。

<一>比较蹩脚的方案:

用户自定义控件,在系统的ProgressBar上面放一个Label,在每次进度改变时,修改Label上的Text。

蹩脚的地方:有很明显的强制植入感觉,系统控件的透明色Transparent也不是真正透明的,Label在ProgressBar上,可以很明显的感觉到就像一坨屎丢在了大马路上,很显眼。

<二>比较完美的方案

集成系统ProgressBar,重新绘制,在每次进度改变的时候,刷新一次即可,并且可以修改滚动条的方向:水平滚动条或者垂直滚动条。代码如下:

  public class ProgressBarWithValue : ProgressBar
{
private ProgressBarDirection direction = ProgressBarDirection.Horizontal;
private bool showPercent = true;
private string customText; public ProgressBarWithValue()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
base.Size = new Size(, );
} protected override void OnPaint(PaintEventArgs e)
{
Rectangle rect = ClientRectangle;
Graphics g = e.Graphics;
if (direction == ProgressBarDirection.Horizontal)
ProgressBarRenderer.DrawHorizontalBar(g, rect);
else
ProgressBarRenderer.DrawVerticalBar(g, rect);
rect.Inflate(-, -);
if (Value > )
{
if (direction == ProgressBarDirection.Horizontal)
{
Rectangle clip = new Rectangle(rect.X, rect.Y, (int)Math.Round(((float)Value / Maximum) * rect.Width), rect.Height);
ProgressBarRenderer.DrawHorizontalChunks(g, clip);
}
else
{
int height = (int)Math.Round(((float)Value / Maximum) * rect.Height);
Rectangle clip = new Rectangle(rect.X, rect.Y + (rect.Height - height), rect.Width, height);
ProgressBarRenderer.DrawVerticalChunks(g, clip);
}
} string text = showPercent ? (Value.ToString() + '%') : CustomText; if (!string.IsNullOrEmpty(text))
using (Font f = new Font(FontFamily.GenericSerif, ))
{
SizeF len = g.MeasureString(text, f);
Point location = new Point(Convert.ToInt32((Width / ) - len.Width / ), Convert.ToInt32((Height / ) - len.Height / ));
g.DrawString(text, f, Brushes.Red, location);
}
} /// <summary>
/// 进度条方向,水平或者垂直
/// </summary>
public ProgressBarDirection Direction
{
get { return direction; }
set
{
if (direction != value)
{
direction = value;
Invalidate();
}
}
} /// <summary>
/// 是否显示进度,true表示显示进度,否则显示自定义的字符串
/// </summary>
public bool ShowPercent
{
get { return showPercent; }
set
{
showPercent = value;
}
} /// <summary>
/// 自定义需要显示的字符串
/// </summary>
public string CustomText
{
get { return customText; }
set
{
if (customText != value)
{
customText = value;
if (!showPercent)
Invalidate();
}
}
}
} public enum ProgressBarDirection
{
/// <summary>
/// 垂直方向
/// </summary>
Vertical,
/// <summary>
/// 水平方向
/// </summary>
Horizontal,
}

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

最新文章

  1. Task.Factory.StartNew的用法
  2. File文件的使用
  3. JS高程2.在HTML中使用Javascript(1)
  4. Maven在Windows上的安装与配置
  5. [课程设计]Scrum 2.4 多鱼点餐系统开发进度(下单一览页面修复)
  6. Android PopupWindow 消失后的回掉方法
  7. 使用strace工具故障排查的5种简单方法
  8. Oracle Exadata体系笔记
  9. Maven的常用命令
  10. Python 模块(八) socketserver 以及 线程、进程
  11. 安卓MP3播放器开发实例(1)之音乐列表界面
  12. easyui datagrid 单元格编辑 自动聚焦 、全选
  13. idea中,发现某个java语法在低版本中不支持时的解决办法
  14. 快乐的一天从JAVA第一课开始,生活美滋滋!!!
  15. SQL LOAD TABLE tbl_name FROM MASTER语法 把表的拷贝从主服务器转移到从属服务器。
  16. 2nd,Python基础2——02
  17. box布局中文字溢出问题
  18. numpy.ravel() vs numpy.flatten()
  19. mybatis学习二 全局配置文件常用配置
  20. Slitaz定制

热门文章

  1. linux终端常用快捷键
  2. 篇二:JS身份证校验
  3. 【PHP】月末・月初の出力方法
  4. ED2k Resource
  5. JS事件-事件处理程序-笔记总结ing...
  6. jquery ajax beforeSend 提交等待问题
  7. Ajax 应用六个需要注意的事项
  8. URL转义
  9. php 操作数组 (合并,拆分,追加,查找,删除等)
  10. python之路十一