WinForm中重绘滚动条以及用重绘的滚动条控制ListBox的滚动

本人对播放器列表右边的灰色滚动条极为不满意,也影响到整个软件UI的协调性,遂下决心要重绘一个符合自己UI风格的滚动条.

查了很多资料,都找不到直接重写ListBox滚动条的方法,只能曲线救国,先自己重绘一个带皮肤的滚动条,然后让它取代ListBox现有的滚动条.
老习惯,先传个效果图,你觉得感兴趣就继续看下去,不喜欢的话就此打住,懒得耽误你宝

贵的时间,嘿嘿

注意,此图中的滚动条宽度明显小于ListBox本身滚动条的宽度,我目前只顾着实现功能了,毕竟,宽度调整相当简单哈。

下面简单介绍下重绘系统滚动条的详细步骤:

1.在项目中添加新项--用户控件,我们命名为CustomScrollbar.cs

2.准备几张图片添加进项目资源作为滚动条重绘时要用的背景,我用的图片如下:

 

     uparrow.png资源名称为uparrow   ,滚动条的上箭头

     ThumbBottom.png资源名称为ThumbBottom  ,滚动条中间滑道的背景


    ThumbMiddle.png资源名称为ThumbMiddle  ,滚动条的中间的拖动块


    downarrow.png资源名称为downarrow   ,滚动条的下箭头

3.然后就是利用上面图片做背景重画滚动条背景了,直接给出CustomScrollbar.cs的代码吧

 

复制代码
  1  using  System;
  2  using  System.Collections.Generic;
  3  using  System.ComponentModel;
  4  using  System.Drawing;
  5  using  System.Data;
  6  using  System.Text;
  7  using  System.Windows.Forms;
  8  using  System.Windows.Forms.Design;
  9  using  System.Diagnostics;
 10  namespace  Winamp
 11  {
 12      [Designer( typeof (ScrollbarControlDesigner))]
 13       public   partial   class  CustomScrollbar : UserControl
 14      {
 15 
 16           protected  Color moChannelColor  =  Color.Empty;
 17           protected  Image moUpArrowImage  =   null ;
 18                  protected  Image moDownArrowImage  =   null ;
 19                   protected  Image moThumbArrowImage  =   null ;
 20 
 21           protected  Image moThumbTopImage  =   null ;
 22           protected  Image moThumbTopSpanImage  =   null ;
 23           protected  Image moThumbBottomImage  =   null ;
 24           protected  Image moThumbBottomSpanImage  =   null ;
 25           protected  Image moThumbMiddleImage  =   null ;
 26 
 27           protected   int  moLargeChange  =   10 ;
 28           protected   int  moSmallChange  =   1 ;
 29           protected   int  moMinimum  =   0 ;
 30           protected   int  moMaximum  =   100 ;
 31           protected   int  moValue  =   0 ;
 32           private   int  nClickPoint;
 33 
 34           protected   int  moThumbTop  =   0 ;
 35 
 36           protected   bool  moAutoSize  =   false ;
 37 
 38           private   bool  moThumbDown  =   false ;
 39           private   bool  moThumbDragging  =   false ;
 40 
 41           public   new   event  EventHandler Scroll  =   null ;
 42           public   event  EventHandler ValueChanged  =   null ;
 43 
 44           private   int  GetThumbHeight()
 45          {
 46               int  nTrackHeight  =  ( this .Height  -  (UpArrowImage.Height  +  DownArrowImage.Height));
 47               float  fThumbHeight  =  (( float )LargeChange  /  ( float )Maximum)  *  nTrackHeight;
 48               int  nThumbHeight  =  ( int )fThumbHeight;
 49 
 50               if  (nThumbHeight  >  nTrackHeight)
 51              {
 52                  nThumbHeight  =  nTrackHeight;
 53                  fThumbHeight  =  nTrackHeight;
 54              }
 55               if  (nThumbHeight  <   56 )
 56              {
 57                  nThumbHeight  =   56 ;
 58                  fThumbHeight  =   56 ;
 59              }
 60 
 61               return  nThumbHeight;
 62          }
 63 
 64           public  CustomScrollbar()
 65          {
 66 
 67              InitializeComponent();
 68              SetStyle(ControlStyles.ResizeRedraw,  true );
 69              SetStyle(ControlStyles.AllPaintingInWmPaint,  true );
 70              SetStyle(ControlStyles.DoubleBuffer,  true );
 71 
 72              moChannelColor  =  Color.FromArgb( 51 166 3 );
 73              UpArrowImage  =  BASSSkin.uparrow;
 74              DownArrowImage  =  BASSSkin.downarrow;
 75 
 76 
 77              ThumbBottomImage  =  BASSSkin.ThumbBottom;
 78 
 79              ThumbMiddleImage  =  BASSSkin.ThumbMiddle;
 80 
 81               this .Width  =  UpArrowImage.Width;
 82               base .MinimumSize  =   new  Size(UpArrowImage.Width, UpArrowImage.Height  +  DownArrowImage.Height  +  GetThumbHeight());
 83          }
 84 
 85          [EditorBrowsable(EditorBrowsableState.Always), Browsable( true ), DefaultValue( false ), Category( " Behavior " ), Description
 86 
 87  ( " LargeChange " )]
 88           public   int  LargeChange
 89          {
 90               get  {  return  moLargeChange; }
 91               set
 92              {
 93                  moLargeChange  =  value;
 94                  Invalidate();
 95              }
 96          }
 97 
 98          [EditorBrowsable(EditorBrowsableState.Always), Browsable( true ), DefaultValue( false ), Category( " Behavior " ), Description
 99 
100  ( " SmallChange " )]
101           public   int  SmallChange
102          {
103               get  {  return  moSmallChange; }
104               set
105              {
106                  moSmallChange  =  value;
107 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值