本人对播放器列表右边的灰色滚动条极为不满意,也影响到整个软件UI的协调性,遂下决心要重绘一个符合自己UI风格的滚动条.
查了很多资料,都找不到直接重写ListBox滚动条的方法,只能曲线救国,先自己重绘一个带皮肤的滚动条,然后让它取代ListBox现有的滚动条.
老习惯,先传个效果图,你觉得感兴趣就继续看下去,不喜欢的话就此打住,懒得耽误你宝
贵的时间,嘿嘿
注意,此图中的滚动条宽度明显小于ListBox本身滚动条的宽度,我目前只顾着实现功能了,毕竟,宽度调整相当简单哈。
下面简单介绍下重绘系统滚动条的详细步骤:
1.在项目中添加新项--用户控件,我们命名为CustomScrollbar.cs
2.准备几张图片添加进项目资源作为滚动条重绘时要用的背景,我用的图片如下:
uparrow.png资源名称为uparrow ,滚动条的上箭头
ThumbBottom.png资源名称为ThumbBottom ,滚动条中间滑道的背景
ThumbMiddle.png资源名称为ThumbMiddle ,滚动条的中间的拖动块
downarrow.png资源名称为downarrow ,滚动条的下箭头
3.然后就是利用上面图片做背景重画滚动条背景了,直接给出CustomScrollbar.cs的代码吧

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