C#listview进度条如何显示
这篇文章主要讲解了“C# listview进度条如何显示”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C# listview进度条如何显示”吧!
成都创新互联专业为企业提供海港网站建设、海港做网站、海港网站设计、海港网站制作等企业网站建设、网页设计与制作、海港企业网站模板建站服务,10多年海港做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
看看具体的实现过程。
程序比较简单,就是重载了listview的实现,不过很实用!
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Drawing; //C# listview进度条显示namespace WindowsApplication1 { class ListViewEx:System.Windows.Forms.ListView { public ListViewEx() { InitializeComponent(); } //C# listview进度条显示private Color mProgressColor = Color.Red; public Color ProgressColor { get { return this.mProgressColor; } set { this.mProgressColor = value; } } private Color mProgressTextColor = Color.Black; public Color ProgressTextColor { get { return mProgressTextColor; } set { mProgressTextColor = value; } } //C# listview进度条显示public int ProgressColumIndex { set { progressIndex = value; } get { return progressIndex; } } int progressIndex = -1; /// ﹤summary﹥ /// 检查是否可以转化为一个浮点数 /// ﹤/summary﹥ const string numberstring = "0123456789."; private bool CheckIsFloat(String s) { //C# listview进度条显示foreach (char c in s) { if (numberstring.IndexOf(c) ﹥ -1) { continue; } else return false; } return true; } protected override void Dispose(bool disposing) { base.Dispose(disposing); } //C# listview进度条显示private void InitializeComponent() { this.OwnerDraw = true; this.View = View.Details; } protected override void OnDrawColumnHeader( DrawListViewColumnHeaderEventArgs e) { e.DrawDefault = true; base.OnDrawColumnHeader(e); } protected override void OnDrawSubItem( DrawListViewSubItemEventArgs e) { if (e.ColumnIndex != this.progressIndex) { //C# listview进度条显示e.DrawDefault = true; base.OnDrawSubItem(e); } else { if (CheckIsFloat(e.Item.SubItems[e.ColumnIndex].Text)) //判断当前subitem文本是否可以转为浮点数 { float per = float.Parse(e.Item. SubItems[e.ColumnIndex].Text); if (per ﹥= 1.0f) { per = per / 100.0f; } Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height); DrawProgress(rect, per, e.Graphics); } } } //C# listview进度条显示 ///绘制进度条列的subitem private void DrawProgress(Rectangle rect, float percent, Graphics g) { if (rect.Height ﹥ 2 && rect.Width ﹥ 2) { //if ((rect.Top ﹥ 0 && rect.Top ﹤ this.Height) &&(rect.Left ﹥ this.Left && rect.Left ﹤ this.Width)) { //绘制进度 int width = (int)(rect.Width * percent); Rectangle newRect = new Rectangle(rect.Left + 1, rect.Top + 1, width - 2, rect.Height - 2); using (Brush tmpb = new SolidBrush(this.mProgressColor)) { g.FillRectangle(tmpb, newRect); } newRect = new Rectangle(rect.Left + 1, rect.Top + 1, rect.Width - 2, rect.Height - 2); g.DrawRectangle(Pens.RoyalBlue, newRect); StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; sf.Trimming = StringTrimming.EllipsisCharacter; newRect = new Rectangle(rect.Left + 1, rect.Top + 1, rect.Width - 2, rect.Height - 2); using (Brush b = new SolidBrush(mProgressTextColor)) { g.DrawString( percent.ToString("p1"), this.Font, b, newRect, sf); } } } //C# listview进度条显示else { return; } } } }
感谢各位的阅读,以上就是“C# listview进度条如何显示”的内容了,经过本文的学习后,相信大家对C# listview进度条如何显示这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!
新闻名称:C#listview进度条如何显示
标题来源:http://scyanting.com/article/pecpep.html