I have a working scrollbar in my UI, but I am not completely satisfied with it because it does not behave like the Windows' scrollbar in the following respect:
When I drag the scrollbar thumb in Windows, the scrolling stops when the thumb meets the end of the trough. But in my implementation, I need to continue dragging the mouse after the thumb is at the end because it is the mouse cursor that controls scrolling. Only when the mouse cursor is at the end of the trough does the scrolling stop.
I have worked on this problem for a while but cannot crack it. Everything else is working except this feature.
Here is some of my my code:
protected void OnThumbMove(MouseEventArgs args)
{
if (this.draggingThumb)
{
int yPosition; // the cursor position in the trough
int beforeY = this.thumb.Y; // Get the position from before move
yPosition = this.thumb.Y + dragPoint - topButton.Height; // dragpoint is the relative position that the cursor stays attached to the thumb while dragging
int yDiff = args.Position.Y - this.lastLocation.Y;
yPosition += yDiff;
// probably needs some scaling code here so the thumb only moves in a limited range...
ScrollTo(MouseToScrollPosition(yPosition));
// set the new location after clamping:
this.lastLocation.Y += this.thumb.Y - beforeY;
}
}