Language/C#

[Chart 마우스 드래그 이벤트]

비비이잉 2022. 6. 13. 14:15
반응형

마우스 드래그 이벤트

private void heightChart_MouseDown(object sender, MouseEventArgs e)
        {
            Axis ax = heightChart.ChartAreas[0].AxisX;
            mDown = ax.PixelPositionToValue(e.Location.X);
        }
private void heightChart_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Axis ax = heightChart.ChartAreas[0].AxisX;
                double xScaleview = heightChart.ChartAreas[0].AxisX.ScaleView.Position;
                double xScalesize = heightChart.ChartAreas[0].AxisX.ScaleView.Size;
                
                double xv = ax.PixelPositionToValue(e.Location.X);

                RectangleF ipar = InnerPlotPositionClientRectangle(heightChart, heightChart.ChartAreas[0]);

                if(ipar.Contains(e.Location))
                {
                    double gap = xv - mDown;
                    double xStartval = xScaleview - gap;
                    double xEndVal = xStartval + xScalesize;
                    if (xStartval >= minH && xEndVal <= maxH)
                    {
                        int maxy = dictmax(xStartval, xEndVal);
                        heightChart.ChartAreas[0].AxisX.ScaleView.Zoom(xStartval, xEndVal);
                        setChartAxisX(xStartval, xEndVal);
                        setChartAxisY(maxy);
                    }
                }
                
            }
        }
RectangleF InnerPlotPositionClientRectangle(Chart chart, ChartArea CA)
        {
            RectangleF IPP = CA.InnerPlotPosition.ToRectangleF();
            RectangleF CArp = ChartAreaClientRectangle(chart, CA);

            float pw = CArp.Width / 100f;
            float ph = CArp.Height / 100f;

            return new RectangleF(CArp.X + pw * IPP.X, CArp.Y + ph * IPP.Y,
                                    pw * IPP.Width, ph * IPP.Height);
        }
        RectangleF ChartAreaClientRectangle(Chart chart, ChartArea CA)
        {
            RectangleF CAR = CA.Position.ToRectangleF();
            float pw = chart.ClientSize.Width / 100f;
            float ph = chart.ClientSize.Height / 100f;
            return new RectangleF(pw * CAR.X, ph * CAR.Y, pw * CAR.Width, ph * CAR.Height);
        }

MouseMove , MouseDown 이벤트를 추가했는데 스크롤바를 이동할 때도 두 개의 이벤트가 동시에 동작헀기 때문에 스크롤 바 영역이 아닐 경우라는 조건을 추가시켜줘야 잘 작동한다.

반응형

'Language > C#' 카테고리의 다른 글

[C#] control invalidate, refresh, update 차이  (0) 2022.06.30
[int / int] 캐스팅  (0) 2022.06.28
C# height Histogram  (0) 2022.05.17
struct list 수정 불가  (0) 2022.05.16
1  (0) 2022.05.09