private ScrollBarElement UpdateHoveredItem(ref ControlMetrics Metrics, bool InvalidateIfChanged = true) { var CurrentPosition = this.LastMousePosition_ClientCoordinates == null ? ScrollBarElement.None : GetElementAtPosition(this.LastLayoutInfo.Bounds, this.LastLayoutInfo.Horizontal, ref Metrics, this.LastMousePosition_ClientCoordinates.Value); this.SetHoveredItem(CurrentPosition, InvalidateIfChanged); return(CurrentPosition); }
/// <summary> /// Calculates where the things in the scroll bar are (in pixels). /// </summary> private static ControlMetrics GetControlMetrics(int ScrollBarLength, int ScrollBarWidth, int ViewportSize, int TotalScrollableDistance, int ScrollValue) { var Metrics = new ControlMetrics(); int ButtonHeight = ScrollBarWidth; Metrics.TrackStart = ButtonHeight; Metrics.BottomButtonStart = ScrollBarLength - ButtonHeight; float ThumbRatio; if (ViewportSize == 0) { ThumbRatio = 0; } else { ThumbRatio = (float)ViewportSize / (TotalScrollableDistance + ViewportSize); } int MinimumThumbSize = ScrollBarWidth; int TrackSize = Math.Max(0, Metrics.BottomButtonStart - Metrics.TrackStart); Metrics.ThumbSize = Math.Min(Math.Max(MinimumThumbSize, (int)(TrackSize * ThumbRatio)), TrackSize); // Size of thumb in pixels, proportional to ViewportSize / TotalScrollableDistance Metrics.TrackSpaceAvailableForThumbMovement = TrackSize - Metrics.ThumbSize; int ThumbPosRelativeToTrackStart; if (TotalScrollableDistance == 0) { ThumbPosRelativeToTrackStart = 0; } else { ThumbPosRelativeToTrackStart = (int)((float)ScrollValue / TotalScrollableDistance * Metrics.TrackSpaceAvailableForThumbMovement); } Metrics.ThumbStart = ThumbPosRelativeToTrackStart + Metrics.TrackStart; return(Metrics); }
/// <summary> /// It doesn't matter whether it's in screen coordinates or control coordinates as long as both <paramref name="Bounds"/> and <paramref name="Position"/> /// are in the same reference frame. /// </summary> private static ScrollBarElement GetElementAtPosition(Rectangle Bounds, bool Horizontal, ref ControlMetrics Metrics, Point Position) { if (!Bounds.Contains(Position)) { return(ScrollBarElement.None); } int y = Horizontal ? Position.X - Bounds.Left : Position.Y - Bounds.Top; if (y < 0) { return(ScrollBarElement.None); } if (y < Metrics.TrackStart) { return(ScrollBarElement.TopButton); } if (y < Metrics.ThumbStart) { return(ScrollBarElement.TrackAboveThumb); } if (y < Metrics.ThumbStart + Metrics.ThumbSize) { return(ScrollBarElement.Thumb); } if (y < Metrics.BottomButtonStart) { return(ScrollBarElement.TrackBelowThumb); } if (y < (Horizontal ? Bounds.Width : Bounds.Height)) { return(ScrollBarElement.BottomButton); } return(ScrollBarElement.None); }