简体   繁体   English

在Microsoft图表控件中启用鼠标滚轮缩放

[英]Enabling mouse wheel zooming in a Microsoft Chart Control

how to enable zooming in Microsoft chart control by using Mouse wheel 如何使用鼠标滚轮启用Microsoft图表控件放大

I have the below code, i need to know how to make this event? 我有以下代码,我需要知道如何制作此活动? in which class it is.. 在哪个班级..

private void chData_MouseWheel(object sender, MouseEventArgs e)
{
    try
    {
        if (e.Delta < 0)
        {
            chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset();
            chart1.ChartAreas[0].AxisY.ScaleView.ZoomReset();
        }

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

            double posXStart = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.Location.X) - (xMax - xMin) / 4;
            double posXFinish = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.Location.X) + (xMax - xMin) / 4;
            double posYStart = chart1.ChartAreas[0].AxisY.PixelPositionToValue(e.Location.Y) - (yMax - yMin) / 4;
            double posYFinish = chart1.ChartAreas[0].AxisY.PixelPositionToValue(e.Location.Y) + (yMax - yMin) / 4;

            chart1.ChartAreas[0].AxisX.ScaleView.Zoom(posXStart, posXFinish);
            chart1.ChartAreas[0].AxisY.ScaleView.Zoom(posYStart, posYFinish);
        }
    }
    catch { }            
}

I think the above answer should be, 我认为上面的答案应该是,

chData.MouseWheel += new MouseEventHandler(chData_MouseWheel); chData.MouseWheel + = new MouseEventHandler(chData_MouseWheel);

But according to what I found out, chart's mouse-wheel doesn't work as long as you don't set focus on the chart control in your code. 但根据我发现的结果,只要您没有将焦点设置在代码中的图表控件上,图表的鼠标滚轮就不起作用。 So I used mouse-enter of the chart control to set focus to the chart and mouse leave event of the chart control to set the control back to its parent. 因此,我使用图表控件的鼠标输入将焦点设置为图表控件的图表和鼠标离开事件,以将控件设置回其父控件。

So you need to add below lines to your code, bind the mouse leave and mouse enter events of the chart control correspondingly plus add the above line too. 因此,您需要在代码中添加以下行,相应地绑定鼠标左键和鼠标输入图表控件的事件,并添加上面的行。

    private void chartTracking_MouseEnter(object sender, EventArgs e)
    {
        this.chartTracking.Focus();
    }

    private void chartTracking_MouseLeave(object sender, EventArgs e)
    {
        this.chartTracking.Parent.Focus();
    }

What you have is an handler method for the MouseWheel event. 你拥有的是MouseWheel事件的处理程序方法。 You need to attach your handler method to the MouseWheel event for the chart control. 您需要将处理程序方法附加到图表控件的MouseWheel事件。 From the method signature, I assume that your chart control is named chData , so you could use the following code in your form's constructor: 从方法签名,我假设您的图表控件名为chData ,因此您可以在窗体的构造函数中使用以下代码:

chData.MouseWheel += new EventHandler(chData_MouseWheel);

Of course, you could also associate the handler with the event at design time. 当然,您也可以在设计时将处理程序与事件相关联。 To do that, use the Properties Window and click the lightning bolt in the toolbar to switch to the "Events" view. 为此,请使用“属性”窗口并单击工具栏中的闪电以切换到“事件”视图。 Then find the MouseWheel event, click the drop-down arrow, and select your handler method's signature. 然后找到MouseWheel事件,单击下拉箭头,然后选择处理程序方法的签名。 This will cause the designer to write the above code into the code-behind file for your form. 这将使设计人员将上述代码编写到表单的代码隐藏文件中。

Aside from that, there's a giant red flag in your code: an empty catch block. 除此之外,你的代码中还有一个巨大的红旗:一个空的catch块。 If you aren't handling an exception or doing anything with it, then you should not be catching it. 如果你没有处理异常或对它做任何事情,那么你就不应该抓住它。 This isn't Pokemon, there's no reward for catching them all. 这不是口袋妖怪,没有奖励抓住他们所有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM