现在的位置: 首页 > 综合 > 正文

c# 使用listview来显示进度条

2013年08月02日 ⁄ 综合 ⁄ 共 4464字 ⁄ 字号 评论关闭

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

 (这个版本是我自己参考了国外的作品后我自己写出来的而且版权归我自己所有,很讨厌很多网站和很多人没经过本人的同意甚至连转载都不写明就直接转载我的代码

尤其讨厌百度文库我的毕业设计和我的代码都直接转载)

 

 /*

作者:(tianhongmu@126.com)

代码作用:重载ListView控件,实现用listview显示进度条的功能

声明:我是抱着大家共同交流学习的目的将这段代码直接写了出来,请大家尊重别人的劳动成果,谢谢。

*/

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows.Forms;

using System.Drawing;

namespace WindowsApplication1

{

    class ListViewEx:System.Windows.Forms.ListView

    {

        public ListViewEx()

        {

            InitializeComponent();

        }

        

        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;

        /// <summary>

        /// 检查是否可以转化为一个浮点数

        /// </summary>

        const string numberstring = "0123456789.";

        private bool CheckIsFloat(String s)

        {

            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);

        }

        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);

                }               

            }

        }

        ///绘制进度条列的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);

                    }

                }

            }

            else

            {

                return;

            }

        }

    }

}

抱歉!评论已关闭.