Avatar of Richard Payne
Richard Payne
Flag for Bulgaria

asked on 

C# VS2015, what the best way to zoom professionally with mousewheel

Below is the working code with mousewheel control with Shift and Control key for Y and X axis.

I'm seeking better algorithm to scale up and down more smoothly and cleanly, it too jerky and do not like reset zoom when mousewheel goes other way. Any suggestion or better solution?



        private void cScope_MouseWheel(object sender, MouseEventArgs e)
        {
            try
            {
                if ((Control.ModifierKeys & Keys.Shift) != Keys.None)
                {
                    if (e.Delta < 0)
                    {
                        cScope.ChartAreas[0].AxisX.ScaleView.ZoomReset();
                    }

                    if (e.Delta > 0)
                    {
                        double xMin = cScope.ChartAreas[0].AxisX.ScaleView.ViewMinimum;
                        double xMax = cScope.ChartAreas[0].AxisX.ScaleView.ViewMaximum;

                        double posXStart = cScope.ChartAreas[0].AxisX.PixelPositionToValue(e.Location.X) - (xMax - xMin) / 2;
                        double posXFinish = cScope.ChartAreas[0].AxisX.PixelPositionToValue(e.Location.X) + (xMax - xMin) / 2;

                        cScope.ChartAreas[0].AxisX.ScaleView.Zoom(Math.Round(posXStart,0), Math.Round(posXFinish,0));
                    }
                }
                if ((Control.ModifierKeys & Keys.Control) != Keys.None)
                {
                    if (e.Delta < 0)
                    {
                        cScope.ChartAreas[0].AxisY.ScaleView.ZoomReset();
                    }

                    if (e.Delta > 0)
                    {
                        double yMin = cScope.ChartAreas[0].AxisY.ScaleView.ViewMinimum;
                        double yMax = cScope.ChartAreas[0].AxisY.ScaleView.ViewMaximum;

                        double posYStart = cScope.ChartAreas[0].AxisY.PixelPositionToValue(e.Location.Y) - (yMax - yMin) / 2;
                        double posYFinish = cScope.ChartAreas[0].AxisY.PixelPositionToValue(e.Location.Y) + (yMax - yMin) / 2;

                        cScope.ChartAreas[0].AxisY.ScaleView.Zoom(Math.Round(posYStart,0), Math.Round(posYFinish,0));
                    }
                }
            }
            catch { }
        }

Open in new window

.NET ProgrammingC#

Avatar of undefined
Last Comment
Richard Payne

8/22/2022 - Mon