title: Mousemove Custom Scroll Bar maths
body: <p><a href="https://i.stack.imgur.com/JSRpO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JSRpO.png" alt="Scroll Bar"></a></p>
  
  <p>I am currently building a Custom Scroll Bar (pictured above) I am able to set the scrollbar value to any number within the allowed range (between Min and Max), but when it comes to mouse scrolling I have a maths problem.</p>
  
  <p>I want the mouse to remain at the same position where the user clicked in the thumb bar, and then drag the thumb up or down, so that the mid position of the scroll bar reflects the value.   </p>
  
  <p>I have the following code to rest the mid position of the thumb:</p>
  
  <pre><code>MoveThumb() 
  {
      m_iMidPoint = Mouse.Cursor.Y;
      int iHalfThumb = ThumbLength / 2;
      m_iMidPoint += iHalfThumb;
  
      // Clamp to allowable range
      m_iMidPoint = m_iMidPoint &lt; (TrenchStartPixel + iHalfThumb) 
          ? TrenchStartPixel + iHalfThumb 
          : m_iMidPoint;
  
      m_iMidPoint = m_iMidPoint &gt; (TrenchEndPixel - iHalfThumb) 
          ? TrenchEndPixel - iHalfThumb 
          : m_iMidPoint;
  
      Value = MidThumbToValue(m_iMidPoint);           // This property gets clamped
  }
  
  public int ConvertRange( int originalStart, int originalEnd, int newStart, 
      int newEnd, int value) 
  {
      double scale = (double)(newEnd - newStart) / (originalEnd - originalStart);
      return (int)(newStart + ((value - originalStart) * scale));
  }
  
  public int ValueToMidThumb(int iValue) 
  {
      return ConvertRange(Minimum, Maximum, TrenchStartPixel + (ThumbLength/2), 
          TrenchEndPixel - (ThumbLength/2), iValue);
  }
  
  public int MidThumbToValue(int iValue) 
  {
      return ConvertRange(TrenchStartPixel + (ThumbLength /2), 
          TrenchEndPixel - (ThumbLength /2), Minimum, Maximum, iValue);
  }
  </code></pre>
  
  <p>This code works ok, APART from the fact that Thumb always jumps so that the middle of the thumb tracks the mouse. </p>
  
  <p>How can I get the thumb to track the mouse position without the jumping?</p>