WEB状态条控件

  近段时间为了工作的需要学习了一下写自定义控件,呵呵!以前没写过,近段时间才开始研究的,昨天写了一个WEB状态条控件,
可以设置进度条的百分比,也可以设置它的总数与当前的数量来自动计算百分比,可以设置颜色显示或图片显示(当没有设置图片
的时候就会自动用颜色来显示),状态条的宽度和高度可以自由设定,还是第一次写控件,希望大家多多指教.好了不多说了,还是把
代码贴出来供大家参考吧!有不同想法的希望大家能提出来交流一下!

先看看运行效果:
           

第一步:
          新建一个类文件Guage.cs
          代码如下:
      

  1 None.gif using  System;
  2 None.gif using  System.Drawing;
  3 None.gif using  System.Web;
  4 None.gif using  System.Web.UI;
  5 None.gif using  System.Web.UI.WebControls;
  6 None.gif using  System.ComponentModel;
  7 None.gif
  8 None.gif namespace  ZYT.Web.UI
  9 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 10ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 11ExpandedSubBlockEnd.gif    /// 进度条WEB控件

 12InBlock.gif
 13InBlock.gif    [DefaultProperty("Text"),
 14InBlock.gif    ToolboxData("<{0}:Guage runat=server></{0}:Guage>")]
 15InBlock.gif    public class Guage : System.Web.UI.WebControls.WebControl
 16ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 17ContractedSubBlock.gifExpandedSubBlockStart.gif        变量#region 变量
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 19InBlock.gif        /// 列数(单元格)
 20ExpandedSubBlockEnd.gif        /// </summary>

 21InBlock.gif        private int intCellCount = 20;
 22InBlock.gif
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 24InBlock.gif        /// 设置进度条百分比
 25ExpandedSubBlockEnd.gif        /// </summary>

 26InBlock.gif        private int intPercentage = 0;
 27InBlock.gif
 28ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 29InBlock.gif        /// 总数量
 30ExpandedSubBlockEnd.gif        /// </summary>

 31InBlock.gif        private int intMaxNum = 0;
 32ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 33InBlock.gif        /// 现在数量
 34ExpandedSubBlockEnd.gif        /// </summary>

 35InBlock.gif        private int intCurNum = 0;
 36InBlock.gif
 37ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 38InBlock.gif        /// 填充图片地址
 39ExpandedSubBlockEnd.gif        /// </summary>

 40InBlock.gif        private string strFillImageUrl = "";
 41ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 42InBlock.gif        /// 进度条图片地址
 43ExpandedSubBlockEnd.gif        /// </summary>

 44InBlock.gif        private string strBarImageUrl = "";
 45InBlock.gif
 46ExpandedSubBlockEnd.gif        #endregion

 47InBlock.gif
 48ContractedSubBlock.gifExpandedSubBlockStart.gif        属性#region 属性
 49InBlock.gif        [Description("进度条百分比步长(必须被100整除)")]
 50InBlock.gif        public int PercentageStep
 51ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 52ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn 100 / intCellCount; }
 53InBlock.gif            set
 54ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 55InBlock.gif                if ((100 % value) != 0)
 56ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 57InBlock.gif                    throw new ArgumentException("百分比步长必须被100整除");
 58ExpandedSubBlockEnd.gif                }

 59InBlock.gif                intCellCount = 100 / value;
 60ExpandedSubBlockEnd.gif            }

 61ExpandedSubBlockEnd.gif        }

 62InBlock.gif
 63InBlock.gif        [Description("设置进度条百分比"), DefaultValue(0)]
 64InBlock.gif        public int Percentage
 65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 66ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn intPercentage; }
 67InBlock.gif            set
 68ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 69InBlock.gif                // 确定百分比在指定的范围内
 70InBlock.gif                //
 71InBlock.gif                if (value > 100// 超过100则显示100
 72ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 73InBlock.gif                    intPercentage = 100;
 74ExpandedSubBlockEnd.gif                }

 75InBlock.gif                else if (value < 0// 小于0则显示0
 76ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 77InBlock.gif                    intPercentage = 0;
 78ExpandedSubBlockEnd.gif                }

 79InBlock.gif                else
 80ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 81InBlock.gif                    intPercentage = value;
 82ExpandedSubBlockEnd.gif                }

 83ExpandedSubBlockEnd.gif            }

 84ExpandedSubBlockEnd.gif        }

 85InBlock.gif
 86InBlock.gif        [Description("总数量")]
 87InBlock.gif        public int MaxNum
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 89ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn intMaxNum; }
 90InBlock.gif            set
 91ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 92InBlock.gif                intMaxNum = value;
 93ExpandedSubBlockEnd.gif            }

 94ExpandedSubBlockEnd.gif        }

 95InBlock.gif
 96InBlock.gif        [Description("当前数量")]
 97InBlock.gif        public int CurNum
 98ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 99ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn intCurNum; }
100InBlock.gif            set
101ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
102InBlock.gif                intCurNum = value;
103ExpandedSubBlockEnd.gif            }

104ExpandedSubBlockEnd.gif        }

105InBlock.gif
106InBlock.gif        [Description("填充图片地址")]
107InBlock.gif        public string FillImageUrl
108ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
109ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn strFillImageUrl; }
110ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ strFillImageUrl = value; }
111ExpandedSubBlockEnd.gif        }

112InBlock.gif
113InBlock.gif        [Description("进度条图片地址")]
114InBlock.gif        public string BarImageUrl
115ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
116ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn strBarImageUrl; }
117ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ strBarImageUrl = value; }
118ExpandedSubBlockEnd.gif        }

119InBlock.gif
120ExpandedSubBlockEnd.gif        #endregion

121InBlock.gif
122ContractedSubBlock.gifExpandedSubBlockStart.gif        构造函数#region 构造函数
123InBlock.gif        public Guage()
124ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
125InBlock.gif            // 初始化进度条的背景颜色、字体颜色和边框颜色
126InBlock.gif            BackColor = System.Drawing.Color.LightGray;
127InBlock.gif            ForeColor = System.Drawing.Color.Blue;
128InBlock.gif            BorderColor = Color.Empty;
129InBlock.gif
130InBlock.gif            //初始化进度条的宽度和高度
131InBlock.gif            base.Width = Unit.Pixel(100);
132InBlock.gif            base.Height = Unit.Pixel(16);
133InBlock.gif
134ExpandedSubBlockEnd.gif        }

135ExpandedSubBlockEnd.gif        #endregion

136InBlock.gif
137ContractedSubBlock.gifExpandedSubBlockStart.gif        取得进度条百分比#region 取得进度条百分比
138ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
139InBlock.gif        /// 取得进度条的百分比
140InBlock.gif        /// </summary>
141InBlock.gif        /// <param name="MaxNum">总数量</param>
142InBlock.gif        /// <param name="curValue">当前数量</param>
143ExpandedSubBlockEnd.gif        /// <returns></returns>

144InBlock.gif        private int GetPercentage(int maxNum, int curNum)
145ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
146InBlock.gif            int intLvalue = 0;
147InBlock.gif            int intFvalue = (curNum * 100/ maxNum;
148InBlock.gif            int intMvalue = (curNum * 100% maxNum;
149InBlock.gif            if (intMvalue > 0)
150ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
151InBlock.gif                string strLvalue = intMvalue.ToString().Substring(01);
152InBlock.gif                if (int.Parse(strLvalue) > 4)
153ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
154InBlock.gif                    intLvalue = int.Parse(strLvalue);
155ExpandedSubBlockEnd.gif                }

156ExpandedSubBlockEnd.gif            }

157InBlock.gif            return intFvalue + intLvalue;
158ExpandedSubBlockEnd.gif        }

159InBlock.gif
160ExpandedSubBlockEnd.gif        #endregion

161InBlock.gif
162ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
163InBlock.gif        /// 进度条输出参数
164InBlock.gif        /// </summary>
165ExpandedSubBlockEnd.gif        /// <param name="output"> 进度条 </param>

166InBlock.gif        protected override void Render(HtmlTextWriter output)
167ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
168InBlock.gif            if (Width.Type != UnitType.Pixel)
169ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
170InBlock.gif                throw new ArgumentException("宽度必须为象素");
171ExpandedSubBlockEnd.gif            }

172InBlock.gif
173InBlock.gif            int intWidth = (int)Width.Value;//控件宽度
174InBlock.gif
175InBlock.gif            if (intPercentage == 0)
176ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
177InBlock.gif                //进度条百分比
178InBlock.gif                intPercentage = GetPercentage(intMaxNum, intCurNum);
179ExpandedSubBlockEnd.gif            }

180InBlock.gif
181InBlock.gif            if (BorderColor != Color.Empty)//进度条加边框
182ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
183InBlock.gif                output.Write("<table border='0' cellspacing='0' cellpadding='1' bgColor='" +
184InBlock.gif                ColorTranslator.ToHtml(BorderColor) + "'><tr><td>");
185ExpandedSubBlockEnd.gif            }

186InBlock.gif            if (BarImageUrl == "")
187ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
188InBlock.gif                output.Write("<table border='0' cellspacing='0' cellpadding='0' height='" + Height + "' bgColor='" + ColorTranslator.ToHtml(BackColor) + "'><tr>");
189InBlock.gif                int intCellWidth = intWidth / intCellCount;
190InBlock.gif                int intCurPercentage = 0;
191InBlock.gif                int intPercentageStep = PercentageStep;
192InBlock.gif                string strCellColor;
193InBlock.gif                string strCellValue = "";
194InBlock.gif
195InBlock.gif                if (Page.Request.Browser.Browser.ToUpper() == "NETSCAPE")
196ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
197InBlock.gif                    if (FillImageUrl != "")
198ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
199InBlock.gif                        strCellValue = "<img src='" + FillImageUrl + "' border='0' width='" + intCellWidth + "'>";
200ExpandedSubBlockEnd.gif                    }

201InBlock.gif                    else
202ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
203InBlock.gif                        strCellValue = " ";
204ExpandedSubBlockEnd.gif                    }

205ExpandedSubBlockEnd.gif                }

206InBlock.gif                for (int i = 0; i < intCellCount; i++, intCurPercentage += intPercentageStep)
207ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
208InBlock.gif                    if (intCurPercentage < intPercentage)
209ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
210InBlock.gif                        strCellColor = " bgColor='" + ColorTranslator.ToHtml(ForeColor) + "'";
211ExpandedSubBlockEnd.gif                    }

212InBlock.gif                    else
213ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
214InBlock.gif                        strCellColor = "";
215ExpandedSubBlockEnd.gif                    }

216InBlock.gif
217InBlock.gif                    if (i == 0)
218ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
219InBlock.gif                        output.Write("<td height='" + Height + "' width='" + intCellWidth + "'" + strCellColor + ">" + strCellValue + "</td>");
220ExpandedSubBlockEnd.gif                    }

221InBlock.gif                    else
222ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
223InBlock.gif                        output.Write("<td width='" + intCellWidth + "'" + strCellColor + ">" + strCellValue + "</td>");
224ExpandedSubBlockEnd.gif                    }

225ExpandedSubBlockEnd.gif                }

226InBlock.gif                output.Write("</tr></table>");
227ExpandedSubBlockEnd.gif            }

228InBlock.gif            else
229ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
230InBlock.gif                int intImageWidth = (int)((intPercentage / 100.0* intWidth);
231InBlock.gif
232InBlock.gif                output.Write("<table border='0' cellpadding='0' cellSpacing='0' bgColor='" + ColorTranslator.ToHtml(BackColor) + "'><tr><td width='" + intWidth + "'>");
233InBlock.gif                output.Write("<img src='" + BarImageUrl + "' width='" + intImageWidth + "' height='" + Height + "'>");
234InBlock.gif                output.Write("</td></tr></table>");
235ExpandedSubBlockEnd.gif            }

236InBlock.gif
237InBlock.gif            if (BorderColor != Color.Empty)
238ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
239InBlock.gif                output.Write("</td></tr></table>");
240ExpandedSubBlockEnd.gif            }

241ExpandedSubBlockEnd.gif        }

242InBlock.gif
243ExpandedSubBlockEnd.gif    }

244ExpandedBlockEnd.gif}

第二步:在WEB项目下添加一个WEB文件:GuageDemo.aspx
代码如下:
     
 1 None.gif <% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " guageDemo.aspx.cs "  Inherits = " guageDemo "   %>
 2 None.gif <% @ Register Assembly = " ZYT.Web.UI "  Namespace = " ZYT.Web.UI "  TagPrefix = " ZYTControl "   %>
 3 None.gif <! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
 4 None.gif
 5 None.gif < html xmlns = " http://www.w3.org/1999/xhtml "   >
 6 None.gif < head runat = " server " >
 7 None.gif     < title > 无标题页 </ title >
 8 None.gif </ head >
 9 None.gif < body >
10 None.gif     < form id = " form1 "  runat = " server " >
11 None.gif     < div >
12 None.gif     < ZYTControl:Guage ID = " guage1 "  runat = " server "  CurNum = " 34 "  MaxNum = " 1000 "  Height = " 25px "  Width = " 300px "  PercentageStep = " 2 "  BarImageUrl = " Guage/Images/3.jpg "  FillImageUrl = " Guage/Images/1.jpg "  ImageGeneratorUrl = ""   />
13 None.gif     </ div >
14 None.gif     </ form >
15 None.gif </ body >
16 None.gif </ html >
17 None.gif

OK!就这样行了

转载于:https://www.cnblogs.com/sunming0905/archive/2006/10/31/545976.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值