C#listview进度条显示浅析

C# listview进度条显示是如何实现的呢?让我们来看看具体的实现过程。

程序比较简单,就是重载了listview的实现,不过很实用!

 
 
 
  1. using System;  
  2.  
  3. using System.Collections.Generic;  
  4.  
  5. using System.Text;  
  6.  
  7. using System.Windows.Forms;  
  8.  
  9. using System.Drawing;  
  10.  //C# listview进度条显示
  11. namespace WindowsApplication1  
  12.  
  13. {  
  14.  
  15. class ListViewEx:System.Windows.Forms.ListView  
  16.  
  17. {  
  18.  
  19. public ListViewEx()  
  20.  
  21. {  
  22.  
  23. InitializeComponent();  
  24.  
  25. }  
  26.  
  27.  
  28.  //C# listview进度条显示
  29. private Color mProgressColor = Color.Red;  
  30.  
  31. public Color ProgressColor  
  32.  
  33. {  
  34.  
  35. get 
  36.  
  37. {  
  38.  
  39. return this.mProgressColor;  
  40.  
  41. }  
  42.  
  43. set 
  44.  
  45. {  
  46.  
  47. this.mProgressColor = value;  
  48.  
  49. }  
  50.  
  51. }  
  52.  
  53. private Color mProgressTextColor = Color.Black;  
  54.  
  55. public Color ProgressTextColor  
  56.  
  57. {  
  58.  
  59. get 
  60.  
  61. {  
  62.  
  63. return mProgressTextColor;  
  64.  
  65. }  
  66.  
  67. set 
  68.  
  69. {  
  70.  
  71. mProgressTextColor = value;  
  72.  
  73. }  
  74.  
  75. }  
  76.  //C# listview进度条显示
  77. public int ProgressColumIndex  
  78.  
  79. {  
  80.  
  81. set 
  82.  
  83. {  
  84.  
  85. progressIndex = value;  
  86.  
  87. }  
  88.  
  89. get 
  90.  
  91. {  
  92.  
  93. return progressIndex;  
  94.  
  95. }  
  96.  
  97. }  
  98.  
  99. int progressIndex = -1;  
  100.  
  101.  
  102. /// ﹤summary﹥  
  103.  
  104. /// 检查是否可以转化为一个浮点数  
  105.  
  106. /// ﹤/summary﹥  
  107.  
  108. const string numberstring = "0123456789.";  
  109.  
  110. private bool CheckIsFloat(String s)  
  111.  
  112. {  
  113.  //C# listview进度条显示
  114. foreach (char c in s)  
  115.  
  116. {  
  117.  
  118. if (numberstring.IndexOf(c) ﹥ -1)  
  119.  
  120. {  
  121.  
  122. continue;  
  123.  
  124. }  
  125.  
  126. else 
  127.  
  128. return false;  
  129.  
  130. }  
  131.  
  132. return true;  
  133.  
  134. }  
  135.  
  136.  
  137. protected override void Dispose(bool disposing)  
  138.  
  139. {  
  140.  
  141. base.Dispose(disposing);  
  142.  
  143. }  
  144.  
  145.  //C# listview进度条显示
  146. private void InitializeComponent()  
  147.  
  148. {  
  149.  
  150. this.OwnerDraw = true;  
  151.  
  152. this.View = View.Details;  
  153.  
  154. }  
  155.  
  156.  
  157. protected override void OnDrawColumnHeader(  
  158. DrawListViewColumnHeaderEventArgs e)  
  159.  
  160. {  
  161.  
  162. e.DrawDefault = true;  
  163.  
  164. base.OnDrawColumnHeader(e);  
  165.  
  166. }  
  167.  
  168.  
  169. protected override void OnDrawSubItem(  
  170. DrawListViewSubItemEventArgs e)  
  171.  
  172. {  
  173.  
  174. if (e.ColumnIndex != this.progressIndex)  
  175.  
  176. {  
  177.  //C# listview进度条显示
  178. e.DrawDefault = true;  
  179.  
  180. base.OnDrawSubItem(e);  
  181.  
  182. }  
  183.  
  184. else 
  185.  
  186. {  
  187.  
  188. if (CheckIsFloat(e.Item.SubItems[e.ColumnIndex].Text))  
  189. //判断当前subitem文本是否可以转为浮点数  
  190.  
  191. {  
  192.  
  193. float per = float.Parse(e.Item.  
  194. SubItems[e.ColumnIndex].Text);  
  195.  
  196. if (per ﹥= 1.0f)  
  197.  
  198. {  
  199.  
  200. per = per / 100.0f;  
  201.  
  202. }  
  203.  
  204. Rectangle rect = new Rectangle(e.Bounds.X,  
  205.  e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);  
  206.  
  207. DrawProgress(rect, per, e.Graphics);  
  208.  
  209. }     
  210.  
  211. }  
  212.  
  213. }  
  214.  //C# listview进度条显示
  215.  
  216. ///绘制进度条列的subitem  
  217.  
  218. private void DrawProgress(Rectangle rect,   
  219. float percent, Graphics g)  
  220.  
  221. {  
  222.  
  223. if (rect.Height ﹥ 2 && rect.Width ﹥ 2)  
  224.  
  225. {  
  226.  
  227. //if ((rect.Top ﹥ 0 && rect.Top ﹤ this.Height)  
  228.  &&(rect.Left ﹥ this.Left && rect.Left ﹤ this.Width))  
  229.  
  230. {  
  231.  
  232. //绘制进度  
  233.  
  234. int width = (int)(rect.Width * percent);  
  235.  
  236. Rectangle newRect = new Rectangle(rect.Left + 1,   
  237. rect.Top + 1, width - 2, rect.Height - 2);  
  238.  
  239. using (Brush tmpb =   
  240. new SolidBrush(this.mProgressColor))  
  241.  
  242. {  
  243.  
  244. g.FillRectangle(tmpb, newRect);  
  245.  
  246. }  
  247.  
  248.  
  249. newRect = new Rectangle(rect.Left +  
  250.  1, rect.Top + 1, rect.Width - 2,  
  251.  rect.Height - 2);  
  252.  
  253. g.DrawRectangle(Pens.RoyalBlue, newRect);  
  254.  
  255. StringFormat sf = new StringFormat();  
  256.  
  257. sf.Alignment = StringAlignment.Center;  
  258.  
  259. sf.LineAlignment = StringAlignment.Center;  
  260.  
  261. sf.Trimming = StringTrimming.EllipsisCharacter;  
  262.  
  263. newRect = new Rectangle(rect.Left + 1,   
  264. rect.Top + 1, rect.Width - 2,   
  265. rect.Height - 2);  
  266.  
  267. using (Brush b =   
  268. new SolidBrush(mProgressTextColor))  
  269.  
  270. {  
  271.  
  272. g.DrawString(  
  273. percent.ToString("p1"), this.Font, b, newRect, sf);  
  274.  
  275. }  
  276.  
  277. }  
  278.  
  279. }  
  280.  //C# listview进度条显示
  281. else 
  282.  
  283. {  
  284.  
  285. return;  
  286.  
  287. }  
  288.  
  289. }  
  290.  
  291. }  
  292.  

C# listview进度条显示的基本情况就向你介绍到这里,希望对你了解和学习C# listview进度条显示有所帮助。

当前标题:C#listview进度条显示浅析
URL地址:http://www.mswzjz.cn/qtweb/news16/464266.html

攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能