簡體   English   中英

在Microsoft圖表控件中啟用鼠標滾輪縮放

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

如何使用鼠標滾輪啟用Microsoft圖表控件放大

我有以下代碼,我需要知道如何制作此活動? 在哪個班級..

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 { }            
}

我認為上面的答案應該是,

chData.MouseWheel + = new MouseEventHandler(chData_MouseWheel);

但根據我發現的結果,只要您沒有將焦點設置在代碼中的圖表控件上,圖表的鼠標滾輪就不起作用。 因此,我使用圖表控件的鼠標輸入將焦點設置為圖表控件的圖表和鼠標離開事件,以將控件設置回其父控件。

因此,您需要在代碼中添加以下行,相應地綁定鼠標左鍵和鼠標輸入圖表控件的事件,並添加上面的行。

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

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

你擁有的是MouseWheel事件的處理程序方法。 您需要將處理程序方法附加到圖表控件的MouseWheel事件。 從方法簽名,我假設您的圖表控件名為chData ,因此您可以在窗體的構造函數中使用以下代碼:

chData.MouseWheel += new EventHandler(chData_MouseWheel);

當然,您也可以在設計時將處理程序與事件相關聯。 為此,請使用“屬性”窗口並單擊工具欄中的閃電以切換到“事件”視圖。 然后找到MouseWheel事件,單擊下拉箭頭,然后選擇處理程序方法的簽名。 這將使設計人員將上述代碼編寫到表單的代碼隱藏文件中。

除此之外,你的代碼中還有一個巨大的紅旗:一個空的catch塊。 如果你沒有處理異常或對它做任何事情,那么你就不應該抓住它。 這不是口袋妖怪,沒有獎勵抓住他們所有。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM