嘿嘿。好东西呀。。。让大家都来看看。我没有睡觉的结果。

80酷酷网    80kuku.com

  using System;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace Blood.Com.WebControl
{
    /// <summary>
    /// 进度条WEB控件
    /// </summary>
    [DefaultProperty("Text"),
        ToolboxData("<{0}:ProgressBar runat=server></{0}:ProgressBar>")]
    public class ProgressBar : System.Web.UI.WebControls.WebControl
    {
        //声明变量

        /// <summary>
        /// 进度条百分比
        /// </summary>
        private int intPercentage = 0;
        /// <summary>
        /// 列数
        /// </summary>
        private int intCellCount = 20;
        /// <summary>
        /// 填充图片网址
        /// </summary>
        private string strFillImageUrl = "";
        /// <summary>
        /// 进度条图片网址
        /// </summary>
        private string strBarImageUrl  = "";
        /// <summary>
        /// 图片发生器网址
        /// </summary>
        private string strImageGeneratorUrl = "";

        /// <summary>
        /// 构造函数
        /// </summary>
        public ProgressBar()
        {
            // 初始化进度条的背景颜色、字体颜色和边框颜色
            BackColor   = System.Drawing.Color.LightGray;
            ForeColor   = System.Drawing.Color.Blue;
            BorderColor = Color.Empty;
            
            //初始化进度条的宽度和高度
            base.Width  = Unit.Pixel(100);
            base.Height = Unit.Pixel(16);
        }

        /// <summary>
        /// 进度条百分比步幅
        /// </summary>
        public int PercentageStep
        {
            get{return 100 / intCellCount;}
            set
            {
                if((100 % value) != 0)
                {
                    throw new ArgumentException("百分比步副必须被100整除");
                }
                intCellCount = 100 / value;
            }
        }

        /// <summary>
        /// 填充图片网址
        /// </summary>
        public string FillImageUrl
        {
            get{return strFillImageUrl;}
            set{strFillImageUrl = value;}
        }

        public string BarImageUrl
        {
            get{return strBarImageUrl;}
            set{strBarImageUrl = value;}            
        }

        public string ImageGeneratorUrl
        {
            get{return strImageGeneratorUrl;}
            set{strImageGeneratorUrl = value;}
        }

        /// <summary>
        /// 设置进度条百分比
        /// </summary>
        public int Percentage
        {
            get {return intPercentage;}
            set
            {
                // 确定百分比在指定的范围内
                //
                if (value > 100)    // 超过100则显示100
                {
                    intPercentage = 100;
                }
                else if (value < 0)    // 小于0则显示0
                {
                    intPercentage = 0;
                }
                else
                {
                    intPercentage = value;
                }
            }
        }

        /// <summary>
        /// 进度条输出参数
        /// </summary>
        /// <param name="output"> 进度条 </param>
        protected override void Render(HtmlTextWriter output)
        {
            if (Width.Type != UnitType.Pixel)
            {
                throw new ArgumentException("宽度必须为象素");
            }
                
            int intWidth = (int)Width.Value;

            if (ImageGeneratorUrl != "")
            {
                string strBorderColor = "";
                if (BorderColor != Color.Empty)
                {
                    strBorderColor = "&BorderColor=" + ColorTranslator.ToHtml(BorderColor);
                }
                
                output.Write(string.Format("<img src='{0}?Width={1}&Height={2}&Percentage={3}&ForeColor={4}&BackColor={5}{6}' Border='0' Width='{1}' Height='{2}'>",
                    ImageGeneratorUrl,
                    intWidth,
                    Height.ToString(),
                    Percentage,
                    ColorTranslator.ToHtml(ForeColor),
                    ColorTranslator.ToHtml(BackColor),
                    strBorderColor));
            }
            else
            {
                if (BorderColor != Color.Empty)
                {
                    output.Write("<table border='0' cellspacing='0' cellpadding='1' bgColor='" +
                        ColorTranslator.ToHtml(BorderColor) + "'><tr><td>");
                }
                if (BarImageUrl == "")
                {
                    output.Write("<table border='0' cellspacing='0' cellpadding='0' height='" + Height + "' bgColor='" + ColorTranslator.ToHtml(BackColor) + "'><tr>");
                    int intCellWidth  = intWidth / intCellCount;
                    int intCurPercentage  = 0;
                    int intPercentageStep = PercentageStep;
                    string strCellColor;
                    string strCellValue = "";

                    if (Page.Request.Browser.Browser.ToUpper() == "NETSCAPE")
                    {
                        if (FillImageUrl != "")
                        {
                            strCellValue = "<img src='" + FillImageUrl + "' border='0' width='" + intCellWidth + "'>";
                        }
                        else
                        {
                            strCellValue = " ";
                        }
                    }
                    for (int i = 0; i < intCellCount; i++, intCurPercentage += intPercentageStep)
                    {
                        if (intCurPercentage < intPercentage)
                        {
                            strCellColor = " bgColor='" + ColorTranslator.ToHtml(ForeColor) + "'";
                        }
                        else
                        {
                            strCellColor = "";
                        }

                        if (i == 0)
                        {
                            output.Write("<td height='" + Height + "' width='" + intCellWidth + "'"  + strCellColor + ">" + strCellValue + "</td>");
                        }
                        else
                        {
                            output.Write("<td width='" + intCellWidth + "'"  + strCellColor + ">" + strCellValue + "</td>");
                        }
                    }
                    output.Write("</tr></table>");
                }
                else
                {
                    int intImageWidth = (int)((intPercentage / 100.0) * intWidth);

                    output.Write("<table border='0' cellpadding='0' cellSpacing='0' bgColor='" + ColorTranslator.ToHtml(BackColor) + "'><tr><td width='" + intWidth + "'>");
                    output.Write("<img src='" + BarImageUrl + "' width='" + intImageWidth + "' height='" + Height + "'>");
                    output.Write("</td></tr></table>");
                }
                if (BorderColor != Color.Empty)
                {
                    output.Write("</td></tr></table>");
                }        
            }
        }
    }
}

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: