[原创]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的代码吧
代码
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Text;
7using System.Windows.Forms;
8using System.Windows.Forms.Design;
9using System.Diagnostics;
js控制滚动条
10namespace Winamp
11 {
12    [Designer(typeof(ScrollbarControlDesigner))]
13public partial class CustomScrollbar : UserControl
14    {
15
16protected Color moChannelColor = Color.Empty;
17protected Image moUpArrowImage = null;
18protected Image moDownArrowImage = null;
19protected Image moThumbArrowImage = null;
20
21protected Image moThumbTopImage = null;
22protected Image moThumbTopSpanImage = null;
23protected Image moThumbBottomImage = null;
24protected Image moThumbBottomSpanImage = null;
25protected Image moThumbMiddleImage = null;
27protected int moLargeChange = 10;
28protected int moSmallChange = 1;
29protected int moMinimum = 0;
30protected int moMaximum = 100;
31protected int moValue = 0;
32private int nClickPoint;
33
34protected int moThumbTop = 0;
35
36protected bool moAutoSize = false;
37
38private bool moThumbDown = false;
39private bool moThumbDragging = false;
40
41public new event EventHandler Scroll = null;
42public event EventHandler ValueChanged = null;
43
44private int GetThumbHeight()
45        {
46int nTrackHeight = (this.Height - (UpArrowImage.Height + DownArrowImage.Height));
47float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
48int nThumbHeight = (int)fThumbHeight;
49
50if (nThumbHeight > nTrackHeight)
51            {
52                nThumbHeight = nTrackHeight;
53                fThumbHeight = nTrackHeight;
54            }
55if (nThumbHeight < 56)
56            {
57                nThumbHeight = 56;
58                fThumbHeight = 56;
59            }
60
61return nThumbHeight;
62        }
63
64public 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
81this.Width = UpArrowImage.Width;
82base.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")]
88public int LargeChange
89        {
90get { return moLargeChange; }
91set
92            {
93                moLargeChange = value;
94                Invalidate();
95            }
96        }
97
98        [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Behavior"), Description
100 ("SmallChange")]
101public int SmallChange
102        {
103get { return moSmallChange; }
104set
105            {
106                moSmallChange = value;
107                Invalidate();
108            }
109        }
110
111        [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Behavior"), Description("Minimum")] 112public int Minimum
113        {
114get { return moMinimum; }
115set
116            {
117                moMinimum = value;
118                Invalidate();
119            }
120        }
121
122        [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Behavior"), Description("Maximum")] 123public int Maximum
124        {
125get { return moMaximum; }
126set
127            {
128                moMaximum = value;
129                Invalidate();
130            }
131        }
132
133        [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Behavior"), Description("Value")] 134public int Value
135        {
136get { return moValue; }
137set
138            {
139                moValue = value;
140
141int nTrackHeight = (this.Height - (UpArrowImage.Height + DownArrowImage.Height));
142float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
143int nThumbHeight = (int)fThumbHeight;
144
145if (nThumbHeight > nTrackHeight)
146                {
147                    nThumbHeight = nTrackHeight;
148                    fThumbHeight = nTrackHeight;
149                }
150if (nThumbHeight < 56)
151                {
152                    nThumbHeight = 56;
153                    fThumbHeight = 56;
154                }
155
156
157int nPixelRange = nTrackHeight - nThumbHeight;
158int nRealRange = (Maximum - Minimum) - LargeChange;
159float fPerc = 0.0f;
160if (nRealRange != 0)
161                {
162                    fPerc = (float)moValue / (float)nRealRange;
163
164                }
165
166float fTop = fPerc * nPixelRange;
167                moThumbTop = (int)fTop;
168
169
170                Invalidate();
171            }
172        }
173
174        [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Skin"), Description("Channel Color")]
175public Color ChannelColor
176        {
177get { return moChannelColor; }
178set { moChannelColor = value; }
179        }
180
181        [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Skin"), Description("Up Arrow
182
183 Graphic")]
184public Image UpArrowImage
185        {
186get { return moUpArrowImage; }
187set { moUpArrowImage = value; }
188        }
189
190        [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Skin"), Description("Up Arrow
191
192 Graphic")]
193public Image DownArrowImage
194        {
195get { return moDownArrowImage; }
196set { moDownArrowImage = value; }
197        }
198
199
200
201
202        [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Skin"), Description("Up Arrow
203
204 Graphic")]
205public Image ThumbBottomImage
206        {
207get { return moThumbBottomImage; }
208set { moThumbBottomImage = value; }
209        }
210
211
212
213        [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Skin"), Description("Up Arrow
214
215 Graphic")]
216public Image ThumbMiddleImage
217        {
218get { return moThumbMiddleImage; }
219set { moThumbMiddleImage = value; }
220        }
221
222protected override void OnPaint(PaintEventArgs e)
223        {
224
225            e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
226
227if (UpArrowImage != null)
228            {
229                e.Graphics.DrawImage(UpArrowImage, new Rectangle(new Point(0, 0), new Size(this.Width, UpArrowImage.Height)));
230            }
231
232            Brush oBrush = new SolidBrush(moChannelColor);
233            Brush oWhiteBrush = new SolidBrush(Color.FromArgb(255, 255, 255));
234
235            e.Graphics.FillRectangle(oWhiteBrush, new Rectangle(0, UpArrowImage.Height, 1, (this.Height - DownArrowImage.Height)));
236            e.Graphics.FillRectangle(oWhiteBrush, new Rectangle(this.Width - 1, UpArrowImage.Height, 1, (this.Height -
237
238 DownArrowImage.Height)));
239
240
241            e.Graphics.DrawImage(ThumbBottomImage, new Rectangle(0, UpArrowImage.Height, this.Width, (this.Height - DownArrowImage.Height))); 242
243int nTrackHeight = (this.Height - (UpArrowImage.Height + DownArrowImage.Height));
244float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
245int nThumbHeight = (int)fThumbHeight;
246
247if (nThumbHeight > nTrackHeight)
248            {
249                nThumbHeight = nTrackHeight;
250                fThumbHeight = nTrackHeight;
251            }
252
253if (nThumbHeight < 56)
254            {
255                nThumbHeight = 56;
256                fThumbHeight = 56;
257            }
258
259
260int nTop = moThumbTop;//0
261            nTop += UpArrowImage.Height;//9px
262
263
264            e.Graphics.DrawImage(ThumbMiddleImage, new Rectangle(0, nTop, this.Width, ThumbMiddleImage.Height));
265
266
267
268if (DownArrowImage != null)
269            {
270                e.Graphics.DrawImage(DownArrowImage, new Rectangle(new Point(0, (this.Height - DownArrowImage.Height)), new Size(this.Width, 271
272 DownArrowImage.Height)));
273            }
274
275        }
276
277
278public override bool AutoSize
279        {
280get
281            {
282return base.AutoSize;
283            }
284set
285            {
286base.AutoSize = value;
287if (base.AutoSize)
288                {
289this.Width = moUpArrowImage.Width;
290                }
291            }
292        }
293
294private void InitializeComponent()
295        {
296this.SuspendLayout();
297
298this.Name = "CustomScrollbar";
299this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.CustomScrollbar_MouseDown);
300this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.CustomScrollbar_MouseMove);
301this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.CustomScrollbar_MouseUp);
302this.ResumeLayout(false);
303
304        }
305
306private void CustomScrollbar_MouseDown(object sender, MouseEventArgs e)
307        {
308            Point ptPoint = this.PointToClient(Cursor.Position);
309int nTrackHeight = (this.Height - (UpArrowImage.Height + DownArrowImage.Height));
310float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
311int nThumbHeight = (int)fThumbHeight;
312
313if (nThumbHeight > nTrackHeight)
314            {
315                nThumbHeight = nTrackHeight;
316                fThumbHeight = nTrackHeight;
317            }

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。