zoukankan      html  css  js  c++  java
  • 带有进度条的listView

     public class ListViewProgress : System.Windows.Forms.ListView
        {
            
    private string progressText;

            
    private System.Drawing.Color progressBackColor;

            
    public System.Drawing.Color ProgressBackColor
            {
                
    get { return progressBackColor; }
                
    set { progressBackColor = value; }
            }
            
    private System.Drawing.Color progressTextColor;

            
    public System.Drawing.Color ProgressTextColor
            {
                
    get { return progressTextColor; }
                
    set { progressTextColor = value; }
            }
            
    private int progressColumnIndex = -1;

            
    public int ProgressColumnIndex
            {
                
    get { return progressColumnIndex; }
                
    set { progressColumnIndex = value; }
            }

            
    public  ListViewProgress()
            {
                
    this.Name = "ProgressListView";
                
    this.progressBackColor = System.Drawing.Color.YellowGreen;
                
    this.progressTextColor = base.ForeColor;
                
    this.OwnerDraw = true;
            }

            
    protected override void OnDrawColumnHeader(System.Windows.Forms.DrawListViewColumnHeaderEventArgs e)
            {
                e.DrawDefault 
    = true;
                
    base.OnDrawColumnHeader(e);
            }

            
    protected override void OnDrawSubItem(System.Windows.Forms.DrawListViewSubItemEventArgs e)
            {
                
    if (e.ColumnIndex == this.ProgressColumnIndex)
                {
                    
    //画进度条
                    System.Drawing.Graphics ProgressGraphics = e.Graphics;
                    System.Drawing.Rectangle ProgressRect 
    = new System.Drawing.Rectangle(
                        e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height);

                    
    if (ProgressRect.Height > 6 && ProgressRect.Width > 6)
                    {
                        
    //调整为新的区域,以便产生一定间距
                        ProgressRect = new System.Drawing.
                            Rectangle(e.Bounds.Left 
    + 2, e.Bounds.Top + 2, e.Bounds.Width - 5, e.Bounds.Height - 5);
                        Single Percent 
    = 0;
                        
    try
                        {
                            Percent 
    = Convert.ToSingle(e.SubItem.Text);
                        }
                        
    catch
                        {
                            Percent 
    = 0;
                        }
                        
    if (Percent > 1.0f)
                            Percent 
    = Percent / 100.0f;
                        
    //外框
                        ProgressGraphics.FillRectangle(System.Drawing.Brushes.White, ProgressRect);
                        ProgressGraphics.DrawRectangle(
    new System.Drawing.Pen(e.SubItem.ForeColor), ProgressRect);

                        
    //内容
                        System.Drawing.Rectangle ProgressContentRect = new Rectangle(
                            ProgressRect.Left 
    + 1, ProgressRect.Top + 1, (int)(ProgressRect.Width * Percent) - 1, ProgressRect.Height - 1 );
                        ProgressGraphics.FillRectangle(
    new System.Drawing.SolidBrush(progressBackColor), ProgressContentRect);
                        
    //输出文字
                        if (e.SubItem.Font.Height < ProgressRect.Height)
                        {
                            ProgressRect 
    = new System.Drawing.Rectangle(ProgressRect.Left, ProgressRect.Top
                                
    - ((ProgressRect.Height - e.SubItem.Font.Height) / 2), ProgressRect.Width, ProgressRect.Height);
                        }
                        
    else
                        {
                            ProgressRect 
    = new System.Drawing.Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height);
                        }
                        
    using (StringFormat sf = new StringFormat())
                        {
                            sf.Alignment 
    = System.Drawing.StringAlignment.Center;
                            sf.LineAlignment 
    = System.Drawing.StringAlignment.Center;
                            sf.Trimming 
    = System.Drawing.StringTrimming.EllipsisCharacter;

                            e.Graphics.DrawString(Percent.ToString(
    "p1"), e.SubItem.Font,
                                
    new System.Drawing.SolidBrush(this.progressTextColor), ProgressRect, sf);
                        }
                    }
                    
    base.OnDrawSubItem(e);
                }
                
    else
                {
                    e.DrawDefault 
    = true;
                    
    base.OnDrawSubItem(e);
                }
            }
        }

     使用方法,数据加载 后,设置progressColumnIndex 属性即可

  • 相关阅读:
    Coursera Algorithms Programming Assignment 2: Deque and Randomized Queue (100分)
    Coursera Algorithms week1 查并集 练习测验:3 Successor with delete
    Coursera Algorithms week1 查并集 练习测验:2 Union-find with specific canonical element
    项目选题报告答辩总结
    项目UML设计(团队)
    第七次作业--项目需求分析(团队)
    结对项目--第二次作业
    【软件工程实践 · 团队项目】 第二次作业
    第五次作业--原型设计(结对)
    团队项目系列博客 —— 在路上(之wampserver 修改根目录以及配置多站点以及修改端口号)
  • 原文地址:https://www.cnblogs.com/wenming205/p/1888509.html
Copyright © 2011-2022 走看看