推荐阅读:ListView 百分比进度条(delphi版)
对于已经有的组件,可以直接添加进来,添加后要先运行一下,然后会在工具箱内找到相应控件。
1、首先编写组件,然后将组件添加到工具箱内
编写代码如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120public partial 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; } } public int ProgressColumIndex { set { progressIndex = value; } get { return progressIndex; } } int progressIndex = -1; 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) { 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; } } }
2、调用方法:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30private void Form1_Load(object sender, EventArgs e) { ListViewItem lviUserName = new ListViewItem(); ListViewItem.ListViewSubItem lvsinc = new ListViewItem.ListViewSubItem(); ListViewItem.ListViewSubItem lvsihostname = new ListViewItem.ListViewSubItem(); ListViewItem.ListViewSubItem lvsiip = new ListViewItem.ListViewSubItem(); lviUserName.Text = "5"; lvsinc.Text = "4"; lvsihostname.Text = "3"; lvsiip.Text = "100"; lviUserName.SubItems.Add(lvsinc); lviUserName.SubItems.Add(lvsihostname); lviUserName.SubItems.Add(lvsiip); this.listView1.Items.Add(lviUserName); this.listView1.ProgressTextColor = Color.Red; this.listView1.ProgressColor = Color.YellowGreen; } private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) { //设置进度条的ColunIndex this.listView1.ProgressColumIndex = 1; } private void timer1_Tick(object sender, EventArgs e) { if (Convert.ToInt32(listView1.Items[0].SubItems[1].Text.ToString()) <= 100) { //进度条数字更新 listView1.Items[0].SubItems[1].Text = (Convert.ToInt32(listView1.Items[0].SubItems[1].Text.ToString()) + 1).ToString(); } }
3、注意要添加Timer控件
相应属性设置如下:
4、运行结果如下所示
以上所述是基于C#实现带进度条的ListView ,希望对大家有所帮助。
最后
以上就是健壮冷风最近收集整理的关于C#实现带进度条的ListView的全部内容,更多相关C#实现带进度条内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复