Code
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. drawing;
Using system. Data;
Using system. text;
Using system. Windows. forms;
Using system. Windows. Forms. design;
Using system. diagnostics;
Namespace customcontrols {
[Designer (typeof (scrollbarcontroldesigner)]
Public class customscrollbar: usercontrol {
Protected color mochannelcolor = color. empty;
Protected image mouparrowimage = NULL;
// Protected image mouparrowimage_over = NULL;
// Protected image mouparrowimage_down = NULL;
Protected image modownarrowimage = NULL;
// Protected image modownarrowimage_over = NULL;
// Protected image modownarrowimage_down = NULL;
Protected image mothumbarrowimage = NULL;
Protected image mothumbtopimage = NULL;
Protected image mothumbtopspanimage = NULL;
Protected image mothumbbottomimage = NULL;
Protected image mothumbbottomspanimage = NULL;
Protected image mothumbmiddleimage = NULL;
Protected int molargechange = 10;
Protected int mosmallchange = 1;
Protected int mominimum = 0;
Protected int momaximum = 100;
Protected int movalue = 0;
Private int nclickpoint;
Protected int mothumbtop = 0;
Protected bool moautosize = false;
Private bool mothumbdown = false;
Private bool mothumbdragging = false;
Public new event eventhandler scroll = NULL;
Public event eventhandler valuechanged = NULL;
Private int getthumbheight ()
{
Int ntrackheight = (this. Height-(uparrowimage. height + downarrowimage. Height ));
Float fthumbheight = (float) largechange/(float) Maximum) * ntrackheight;
Int nthumbheight = (INT) fthumbheight;
If (nthumbheight> ntrackheight)
{
Nthumbheight = ntrackheight;
Fthumbheight = ntrackheight;
}
If (nthumbheight <56)
{
Nthumbheight = 56;
Fthumbheight = 56;
}
Return nthumbheight;
}
Public customscrollbar (){
Initializecomponent ();
Setstyle (controlstyles. resizeredraw, true );
Setstyle (controlstyles. allpaintinginwmpaint, true );
Setstyle (controlstyles. doublebuffer, true );
Mochannelcolor = color. fromargb (51,166, 3 );
Uparrowimage = resource. uparrow;
Downarrowimage = resource. downarrow;
Thumbbottomimage = resource. thumbbottom;
Thumbbottomspanimage = resource. thumbspanbottom;
Thumbtopimage = resource. thumbtop;
Thumbtopspanimage = resource. thumbspantop;
Thumbmiddleimage = resource. thumbmiddle;
This. width = uparrowimage. width;
Base. minimumsize = new size (uparrowimage. Width, uparrowimage. height + downarrowimage. height + getthumbheight ());
}
[Editorbrowsable (editorbrowsablestate. Always), browsable (true), defaultvalue (false), category ("behavior"), description ("largechange")]
Public int largechange {
Get {return molargechange ;}
Set {molargechange = value;
Invalidate ();
}
}
[Editorbrowsable (editorbrowsablestate. Always), browsable (true), defaultvalue (false), category ("behavior"), description ("smallchange")]
Public int smallchange {
Get {return mosmallchange ;}
Set {mosmallchange = value;
Invalidate ();
}
}
[Editorbrowsable (editorbrowsablestate. Always), browsable (true), defaultvalue (false), category ("behavior"), description ("minimum")]
Public int minimum {
Get {return mominimum ;}
Set {mominimum = value;
Invalidate ();
}
}
[Editorbrowsable (editorbrowsablestate. Always), browsable (true), defaultvalue (false), category ("behavior"), description ("maximum")]
Public int maximum {
Get {return momaximum ;}
Set {momaximum = value;
Invalidate ();
}
}
[Editorbrowsable (editorbrowsablestate. Always), browsable (true), defaultvalue (false), category ("behavior"), description ("value")]
Public int value {
Get {return movalue ;}
Set {movalue = value;
Int ntrackheight = (this. Height-(uparrowimage. height + downarrowimage. Height ));
Float fthumbheight = (float) largechange/(float) Maximum) * ntrackheight;
Int nthumbheight = (INT) fthumbheight;
If (nthumbheight> ntrackheight)
{
Nthumbheight = ntrackheight;
Fthumbheight = ntrackheight;
}
If (nthumbheight <56)
{
Nthumbheight = 56;
Fthumbheight = 56;
}
// Figure out value
Int npixelrange = ntrackheight-nthumbheight;
Int nrealrange = (maximum-minimum)-largechange;
Float fperc = 0.0f;
If (nrealrange! = 0)
{
Fperc = (float) movalue/(float) nrealrange;
}
Float ftop = fperc * npixelrange;
Mothumbtop = (INT) ftop;
Invalidate ();
}
}
[Editorbrowsable (editorbrowsablestate. Always), browsable (true), defaultvalue (false), category ("skin"), description ("channel color")]
Public color channelcolor
{
Get {return mochannelcolor ;}
Set {mochannelcolor = value ;}
}
[Editorbrowsable (editorbrowsablestate. Always), browsable (true), defaultvalue (false), category ("skin"), description ("Up Arrow graphic")]
Public Image uparrowimage {
Get {return mouparrowimage ;}
Set {mouparrowimage = value ;}
}
[Editorbrowsable (editorbrowsablestate. Always), browsable (true), defaultvalue (false), category ("skin"), description ("Up Arrow graphic")]
Public Image downarrowimage {
Get {return modownarrowimage ;}
Set {modownarrowimage = value ;}
}
[Editorbrowsable (editorbrowsablestate. Always), browsable (true), defaultvalue (false), category ("skin"), description ("Up Arrow graphic")]
Public Image thumbtopimage {
Get {return mothumbtopimage ;}
Set {mothumbtopimage = value ;}
}
[Editorbrowsable (editorbrowsablestate. Always), browsable (true), defaultvalue (false), category ("skin"), description ("Up Arrow graphic")]
Public Image thumbtopspanimage {
Get {return mothumbtopspanimage ;}
Set {mothumbtopspanimage = value ;}
}
[Editorbrowsable (editorbrowsablestate. Always), browsable (true), defaultvalue (false), category ("skin"), description ("Up Arrow graphic")]
Public Image thumbbottomimage {
Get {return mothumbbottomimage ;}
Set {mothumbbottomimage = value ;}
}
[Editorbrowsable (editorbrowsablestate. Always), browsable (true), defaultvalue (false), category ("skin"), description ("Up Arrow graphic")]
Public Image thumbbottomspanimage {
Get {return mothumbbottomspanimage ;}
Set {mothumbbottomspanimage = value ;}
}
[Editorbrowsable (editorbrowsablestate. Always), browsable (true), defaultvalue (false), category ("skin"), description ("Up Arrow graphic")]
Public Image thumbmiddleimage {
Get {return mothumbmiddleimage ;}
Set {mothumbmiddleimage = value ;}
}
Protected override void onpaint (painteventargs e ){
E. Graphics. interpolationmode = system. Drawing. drawing2d. interpolationmode. nearestneighbor;
If (uparrowimage! = NULL ){
E. Graphics. drawimage (uparrowimage, new rectangle (new point (0, 0), new size (this. Width, uparrowimage. Height )));
}
Brush obrush = new solidbrush (mochannelcolor );
Brush owhitebrush = new solidbrush (color. fromargb (255,255,255 ));
// Draw channel left and right border colors
E. Graphics. fillrectangle (owhitebrush, new rectangle (0, uparrowimage. Height, 1, (this. Height-DownArrowImage.Height )));
E. Graphics. fillrectangle (owhitebrush, new rectangle (this. Width-1, uparrowimage. Height, 1, (this. Height-downarrowimage. Height )));
// Draw Channel
E. Graphics. fillrectangle (obrush, new rectangle (1, uparrowimage. Height, this. Width-2, (this. Height-DownArrowImage.Height )));
// Draw thumb
Int ntrackheight = (this. Height-(uparrowimage. height + downarrowimage. Height ));
Float fthumbheight = (float) largechange/(float) Maximum) * ntrackheight;
Int nthumbheight = (INT) fthumbheight;
If (nthumbheight> ntrackheight ){
Nthumbheight = ntrackheight;
Fthumbheight = ntrackheight;
}
If (nthumbheight <56 ){
Nthumbheight = 56;
Fthumbheight = 56;
}
// Debug. writeline (nthumbheight. tostring ());
Float fspanheight = (fthumbheight-(thumbmiddleimage. height + thumbtopimage. height + thumbbottomimage. Height)/2.0f;
Int nspanheight = (INT) fspanheight;
Int ntop = mothumbtop;
Ntop + = uparrowimage. height;
// Draw top
E. Graphics. drawimage (thumbtopimage, new rectangle (1, ntop, this. Width-2, thumbtopimage. Height ));
Ntop + = thumbtopimage. height;
// Draw top Span
Rectangle rect = new rectangle (1, ntop, this. Width-2, nspanheight );
E. Graphics. drawimage (thumbtopspanimage, 1.0f, (float) ntop, (float) This. Width-2.0f, (float) fspanheight * 2 );
Ntop + = nspanheight;
// Draw middle
E. Graphics. drawimage (thumbmiddleimage, new rectangle (1, ntop, this. Width-2, thumbmiddleimage. Height ));
Ntop + = thumbmiddleimage. height;
// Draw top Span
Rect = new rectangle (1, ntop, this. Width-2, nspanheight * 2 );
E. Graphics. drawimage (thumbbottomspanimage, rect );
Ntop + = nspanheight;
// Draw bottom
E. Graphics. drawimage (thumbbottomimage, new rectangle (1, ntop, this. Width-2, nspanheight ));
If (downarrowimage! = NULL ){
E. Graphics. drawimage (downarrowimage, new rectangle (new point (0, (this. Height-DownArrowImage.Height), new size (this. Width, downarrowimage. Height )));
}
}
Public override bool autosize {
Get {
Return base. autosize;
}
Set {
Base. autosize = value;
If (base. autosize ){
This. width = mouparrowimage. width;
}
}
}
Private void initializecomponent (){
This. suspendlayout ();
//
// Customscrollbar
//
This. Name = "customscrollbar ";
This. mousedown + = new system. Windows. Forms. mouseeventhandler (this. customscrollbar_mousedown );
This. mousemove + = new system. Windows. Forms. mouseeventhandler (this. customscrollbar_mousemove );
This. mouseup + = new system. Windows. Forms. mouseeventhandler (this. customscrollbar_mouseup );
This. resumelayout (false );
}
Private void customscrollbar_mousedown (Object sender, mouseeventargs e ){
Point ptpoint = This. pointtoclient (cursor. position );
Int ntrackheight = (this. Height-(uparrowimage. height + downarrowimage. Height ));
Float fthumbheight = (float) largechange/(float) Maximum) * ntrackheight;
Int nthumbheight = (INT) fthumbheight;
If (nthumbheight> ntrackheight ){
Nthumbheight = ntrackheight;
Fthumbheight = ntrackheight;
}
If (nthumbheight <56 ){
Nthumbheight = 56;
Fthumbheight = 56;
}
Int ntop = mothumbtop;
Ntop + = uparrowimage. height;
Rectangle thumbrect = new rectangle (new point (1, ntop), new size (thumbmiddleimage. Width, nthumbheight ));
If (thumbrect. Contains (ptpoint ))
{
// Hit the thumb
Nclickpoint = (ptpoint. Y-ntop );
// MessageBox. Show (convert. tostring (ptpoint. Y-ntop )));
This. mothumbdown = true;
}
Rectangle uparrowrect = new rectangle (new point (1, 0), new size (uparrowimage. Width, uparrowimage. Height ));
If (uparrowrect. Contains (ptpoint ))
{
Int nrealrange = (maximum-minimum)-largechange;
Int npixelrange = (ntrackheight-nthumbheight );
If (nrealrange> 0)
{
If (npixelrange> 0)
{
If (mothumbtop-smallchange) <0)
Mothumbtop = 0;
Else
Mothumbtop-= smallchange;
// Figure out value
Float fperc = (float) mothumbtop/(float) npixelrange;
Float fvalue = fperc * (maximum-largechange );
Movalue = (INT) fvalue;
Debug. writeline (movalue. tostring ());
If (valuechanged! = NULL)
Valuechanged (this, new eventargs ());
If (scroll! = NULL)
Scroll (this, new eventargs ());
Invalidate ();
}
}
}
Rectangle downarrowrect = new rectangle (new point (1, uparrowimage. height + ntrackheight), new size (uparrowimage. Width, uparrowimage. Height ));
If (downarrowrect. Contains (ptpoint ))
{
Int nrealrange = (maximum-minimum)-largechange;
Int npixelrange = (ntrackheight-nthumbheight );
If (nrealrange> 0)
{
If (npixelrange> 0)
{
If (mothumbtop + smallchange)> npixelrange)
Mothumbtop = npixelrange;
Else
Mothumbtop + = smallchange;
// Figure out value
Float fperc = (float) mothumbtop/(float) npixelrange;
Float fvalue = fperc * (maximum-largechange );
Movalue = (INT) fvalue;
Debug. writeline (movalue. tostring ());
If (valuechanged! = NULL)
Valuechanged (this, new eventargs ());
If (scroll! = NULL)
Scroll (this, new eventargs ());
Invalidate ();
}
}
}
}
Private void customscrollbar_mouseup (Object sender, mouseeventargs e ){
This. mothumbdown = false;
This. mothumbdragging = false;
}
Private void movethumb (INT y ){
Int nrealrange = maximum-minimum;
Int ntrackheight = (this. Height-(uparrowimage. height + downarrowimage. Height ));
Float fthumbheight = (float) largechange/(float) Maximum) * ntrackheight;
Int nthumbheight = (INT) fthumbheight;
If (nthumbheight> ntrackheight ){
Nthumbheight = ntrackheight;
Fthumbheight = ntrackheight;
}
If (nthumbheight <56 ){
Nthumbheight = 56;
Fthumbheight = 56;
}
Int nspot = nclickpoint;
Int npixelrange = (ntrackheight-nthumbheight );
If (mothumbdown & nrealrange> 0 ){
If (npixelrange> 0 ){
Int nnewthumbtop = Y-(uparrowimage. height + nspot );
If (nnewthumbtop <0)
{
Mothumbtop = nnewthumbtop = 0;
}
Else if (nnewthumbtop> npixelrange)
{
Mothumbtop = nnewthumbtop = npixelrange;
}
Else {
Mothumbtop = Y-(uparrowimage. height + nspot );
}
// Figure out value
Float fperc = (float) mothumbtop/(float) npixelrange;
Float fvalue = fperc * (maximum-largechange );
Movalue = (INT) fvalue;
Debug. writeline (movalue. tostring ());
Application. doevents ();
Invalidate ();
}
}
}
Private void customscrollbar_mousemove (Object sender, mouseeventargs e ){
If (mothumbdown = true)
{
This. mothumbdragging = true;
}
If (this. mothumbdragging ){
Movethumb (E. y );
}
If (valuechanged! = NULL)
Valuechanged (this, new eventargs ());
If (scroll! = NULL)
Scroll (this, new eventargs ());
}
}
Internal class scrollbarcontroldesigner: system. Windows. Forms. Design. controldesigner {
Public override selectionrules {
Get {
Selectionrules = base. selectionrules;
Propertydescriptor propdescriptor = typedescriptor. getproperties (this. Component) ["autosize"];
If (propdescriptor! = NULL ){
Bool autosize = (bool) propdescriptor. getvalue (this. component );
If (autosize ){
Selectionrules = selectionrules. Visible | selectionrules. moveable | selectionrules. bottomsizeable | selectionrules. topsizeable;
}
Else {
Selectionrules = selectionrules. Visible | selectionrules. allsizeable | selectionrules. moveable;
}
}
Return selectionrules;
}
}
}
}
Vertical scroll bar code