ComponentFactory.Krypton.Toolkit.ViewDrawTP.NearestValueFromPoint C# (CSharp) Method

NearestValueFromPoint() public method

Find nearest value given the mouse postion within track area.
public NearestValueFromPoint ( Point pt ) : int
pt Point Mouse position,
return int
        public int NearestValueFromPoint(Point pt)
        {
            // Grab range and current position from the bar
            int min = _drawTrackBar.Minimum;
            int max = _drawTrackBar.Maximum;
            int range = Math.Abs(max - min);

            // If min and max are the same, we are done!
            if (range == 0)
                return min;

            Rectangle trackRect = TrackArea;
            if (_drawTrackBar.Orientation == Orientation.Horizontal)
            {
                if (_drawTrackBar.RightToLeft == RightToLeft.Yes)
                {
                    // Limit check the position
                    if (pt.X <= trackRect.X)
                        return max;
                    else if (pt.X >= (trackRect.Right - 1))
                        return min;
                    else
                    {
                        float offset = trackRect.Right - pt.X;
                        float x = offset / trackRect.Width;
                        float y = min + x * range;
                        int ret = (int)Math.Round(y, 0, MidpointRounding.AwayFromZero);
                        return ret;
                    }
                }
                else
                {
                    // Limit check the position
                    if (pt.X <= trackRect.X)
                        return min;
                    else if (pt.X >= (trackRect.Right - 1))
                        return max;
                    else
                    {
                        float offset = pt.X - trackRect.X;
                        float x = offset / trackRect.Width;
                        float y = min + x * range;
                        int ret = (int)Math.Round(y, 0, MidpointRounding.AwayFromZero);
                        return ret;
                    }
                }
            }
            else
            {
                // Limit check the position
                if (pt.Y <= trackRect.Y)
                    return max;
                else if (pt.Y >= (trackRect.Bottom - 1))
                    return min;
                {
                    float offset = trackRect.Bottom - pt.Y;
                    float x = offset / trackRect.Height;
                    float y = min + x * range;
                    int ret = (int)Math.Round(y, 0, MidpointRounding.AwayFromZero);
                    return ret;
                }
            }
        }

Usage Example

Beispiel #1
0
        /// <summary>
        /// Mouse has moved inside the view.
        /// </summary>
        /// <param name="c">Reference to the source control instance.</param>
        /// <param name="pt">Mouse position relative to control.</param>
        public virtual void MouseMove(Control c, Point pt)
        {
            if (_captured)
            {
                // Only interested if the mouse is over the track area
                if (_drawTB.ClientRectangle.Contains(pt))
                {
                    // Ignore multiple calls with the same point
                    if (_lastMovePt != pt)
                    {
                        _lastMovePt = pt;

                        _targetValue = _drawTB.NearestValueFromPoint(pt);
                    }
                }
            }
        }
All Usage Examples Of ComponentFactory.Krypton.Toolkit.ViewDrawTP::NearestValueFromPoint