首页 > 开发 > 综合 > 正文

功能增强的进度条控件(源码)

2024-07-21 02:24:07
字体:
来源:转载
供稿:网友
因为比较简单,也没有多少技术含量,就把它帖出来,希望给别的朋友一点帮助 这个进度条控件,除了具有普通进度条的功能以外,还具有如下功能:设置进度条的背景色和前景色设置进度条的外观方式(3d,single,none)是否自动显示当前进度比例(比如在进度条的中间显示当前进度58%)当然,这个功能还可以增加很多,比如背景色和前景色也可以用图片代替,还有其它的功能打算在以后有时间时再添加,这次就写这些了。代码如下:

using system;

using system.collections;

using system.componentmodel;

using system.drawing;

using system.drawing.drawing2d ;

using system.data;

using system.windows.forms;

namespace xiaopang.windows

{

     /// <summary>

     /// gprogressbar 的摘要说明。

     /// </summary>

     public class gprogressbar : system.windows.forms.usercontrol

     {

         /// <summary>

         /// 必需的设计器变量。

         /// </summary>

         private system.componentmodel.container components = null;

 

         public gprogressbar()

         {

              // 该调用是 windows.forms 窗体设计器所必需的。

              initializecomponent();

              base.height = 23 ;

              this.resize+=new eventhandler(gprogressbar_resize);

              this.locationchanged+=new eventhandler(gprogressbar_resize);

         }

 

         private void gprogressbar_resize(object sender,system.eventargs e)

         {

              base.refresh() ;

         }

 

         private  int mmax =100;

         [browsable(true), description("最大值"), category("xiaopang")]

         public int max

         {

              get

              {

                   return mmax ;

              }

 

              set

              {

                   mmax = value > 0?value:1;

              }

         }

        

         [browsable(true), description("大小"), category("xiaopang")]

          public new size size

         {

 

              get

              {

                   return base.size ;

              }

 

              set

              {

                   base.size = value ;

              }

         }

         private  int mmin =0;

         [browsable(true), description("最小值"), category("xiaopang")]

         public int min

         {

              get

              {

                   return mmin ;

              }

 

              set

              {

                   mmin = value ;

              }

         }

 

         private int mstep = 1 ;

         [browsable(true), description("步长"), category("xiaopang")]

         public int step

         {

              get

              {

                   return mstep ;

              }

 

              set

              {

                   mstep = value ;

              }

        

         }

             

         [browsable(true), description("背景色"), category("xiaopang")]

         public override color backcolor

         {

 

              get

              {

                   return base.backcolor ;

              }

 

              set

              {

                   base.backcolor = value ;

              }

         }

         [browsable(true), description("前景色"), category("xiaopang")]

         public override color forecolor

         {

 

              get

              {

                   return base.forecolor ;

              }

 

              set

              {

                   base.forecolor = value ;

              }

         }

         private bool mrate = false;

         [browsable(true), description("是否显示比例数字"), category("xiaopang")]

         public bool rate

         {

              get

              {

                   return mrate ;

              }

 

              set

              {

                   mrate = value ;

              }

         }

         private int mvalue = 0 ;

         [browsable(true), description("当前值"), category("xiaopang")]

         public int value

         {

              get

              {

                   return mvalue ;

              }

              set

              {

                   if (value <= this.max)

                    mvalue = value ;

                   else

                       mvalue = this.max ;

                   this.refresh() ;

              }

         }

 

 

         private borderstyle mborderstyle = borderstyle.fixed3d ;

         [defaultvalue(0), category("xiaopang"),description("外观")]

         public borderstyle borderstyle

         {

              get

              {

                   return this.mborderstyle;

              }

              set

              {

                   if (this.mborderstyle != value)

                   {

                       this.mborderstyle = value;

                       this.refresh() ;

                   }

              }

         }

 

         public void performstep()

         {

              value++ ;

         }

 

         private void drawprogress(graphics g,rectangle rect)

         {

              int iwidth = (value*rect.width)/max ;

              rectangle drawrect = new rectangle(rect.x,rect.y,iwidth,rect.height) ;

              solidbrush front = new solidbrush(this.forecolor) ;

              g.fillrectangle(front,drawrect);    

              if (rate)

              {

                   int irate = value*100/max ;

                   string strtext = irate.tostring() + "%" ;

                   int itextwidth = (int)g.measurestring(strtext,this.font).width ;

                   int istart = rect.left + (rect.width - itextwidth)/2 ;          

                   point poss = new point(istart -10,rect.top) ;

                   point pose = new point(istart + itextwidth + 10,rect.bottom) ;

                   lineargradientbrush textbrush = new lineargradientbrush(poss,pose,this.backcolor,this.forecolor) ;

                   g.drawstring(strtext,this.font,textbrush,istart ,rect.top+5) ;

              }            

         }

 

         protected override void onpaintbackground(painteventargs pevent)

         {

              solidbrush back = new solidbrush(this.backcolor) ;

              pevent.graphics.fillrectangle(back,pevent.cliprectangle);   

         }

 

         protected override void onpaint(painteventargs e)

         {

              if (!this.designmode)

                drawprogress(e.graphics,e.cliprectangle) ;

             

              switch(borderstyle)

              {

                   case borderstyle.none:

                       break ;

                   case borderstyle.fixedsingle:

                       system.windows.forms.controlpaint.drawborder3d(e.graphics,e.cliprectangle,border3dstyle.flat) ;

                       break ;

                   case borderstyle.fixed3d:

                       system.windows.forms.controlpaint.drawborder3d(e.graphics,e.cliprectangle,border3dstyle.sunken) ;

                       break ;

              }

         }

 

         /// <summary>

         /// 清理所有正在使用的资源。

         /// </summary>

         protected override void dispose( bool disposing )

         {

              if( disposing )

              {

                   if(components != null)

                   {

                       components.dispose();

                   }

              }

              base.dispose( disposing );

         }

 

         #region 组件设计器生成的代码

         /// <summary>

         /// 设计器支持所需的方法 - 不要使用代码编辑器

         /// 修改此方法的内容。

         /// </summary>

         private void initializecomponent()

         {

              //

              // gprogressbar

              //

              this.name = "gprogressbar";

              this.size = new system.drawing.size(150, 24);

 

         }

         #endregion

     }

}

 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表