15
我在我的项目中使用Microsoft图表控件,我想通过使用鼠标滚轮在图表控件中启用缩放功能,我该如何实现这一点?如何通过使用鼠标滚轮启用缩放Microsoft图表控件
但用户不必点击图表,它应该是这样的,如果鼠标位置是我的图不是从点开始通过鼠标滚轮滚动它可以放大/缩小
我在我的项目中使用Microsoft图表控件,我想通过使用鼠标滚轮在图表控件中启用缩放功能,我该如何实现这一点?如何通过使用鼠标滚轮启用缩放Microsoft图表控件
但用户不必点击图表,它应该是这样的,如果鼠标位置是我的图不是从点开始通过鼠标滚轮滚动它可以放大/缩小
你会想使用MouseWheel事件。确保你的图表可缩放。例如:
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
然后访问MouseWheel事件:
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 { }
}
有可能是这样做的更清洁的方式,但它是。 e.Delta
属性告诉你已经完成了多少个轮子的“滚动”,并且可能有用。如果您完全滚动出来,使用此代码就可以恢复到图表的原始大小。
希望这会有所帮助!
这对我来说真棒。谢谢! – crocboy
这不适合我。图表的Mousewheel事件不会触发。 –
显然,你必须这样做首先为它工作'无效friendChart_MouseLeave(对象发件人,EventArgs的){ 如果 (friendChart.Focused) friendChart.Parent.Focus(); } void friendChart_MouseEnter(object sender,EventArgs e) if(!friendChart.Focused) friendChart.Focus(); }'[Mousewheel event not firing](http://stackoverflow.com/questions/13782763/mousewheel-event-not-firing) –