2013-04-05 76 views
1

我正在使用Zedgraph創建股票圖表應用程序。該圖表有多個指標面板。我想顯示一個十字光標,該光標延伸到包含所有圖形窗格的Zedgraph控件的範圍。我使用下面的代碼和MouseMove事件完成了一半的工作。但是,當鼠標停止移動時,十字準線消失。當鼠標靜止時,我如何讓它們顯示?我的Zedgraph控件在下面的代碼中被命名爲'chtOHLC'。如何在Zedgraph中繪製延長的十字光標,當鼠標未移動時該光標持續存在?

#region Extended Crosshairs 
private Point _mousePoint; 
private void chtOHLC_MouseMove(object sender, MouseEventArgs e) 
{ 
    _mousePoint = new Point(e.X, e.Y); 
    chtOHLC.Refresh(); 
} 

private void chtOHLC_Paint(object sender, PaintEventArgs e) 
{ 
    if (_mousePoint != null) 
    { 
    Graphics g = chtOHLC.CreateGraphics(); 
    g.DrawLine(Pens.Black, 0, _mousePoint.Y, chtOHLC.Width, _mousePoint.Y); 
    g.DrawLine(Pens.Black, _mousePoint.X, 0, _mousePoint.X, chtOHLC.Height); 
    g.Dispose(); 
    } 
} 
#endregion 

回答

4

試一下: 它添加到您的窗體類

ZedGraph這裏是我的圖書館

private double? CrossHairX = null; 
private double? CrossHairY = null; 
LineObj xHairOld = new LineObj(); 
LineObj yHairOld = new LineObj(); 

ZedGraph鼠標移動事件的instanse:

private void ZedGraph_MouseMove(object sender, MouseEventArgs e) 
     { 
      double x, y; 
      ZedGraph.GraphPane.ReverseTransform(e.Location, out x, out y); 

      #region crosshair 

      if (x < ZedGraph.GraphPane.XAxis.Scale.Min || 
       x > ZedGraph.GraphPane.XAxis.Scale.Max || 
       y < ZedGraph.GraphPane.YAxis.Scale.Min || 
       y > ZedGraph.GraphPane.YAxis.Scale.Max 
       )//out of the bounds 
      { 
       ZedGraph_MouseLeave(new object(), new EventArgs()); 
      } 
      else//ok draw 
      { 

       if (CrossHairX != null && CrossHairY != null) 
       { 
        ZedGraph.GraphPane.GraphObjList.Remove(xHairOld); 
        ZedGraph.GraphPane.GraphObjList.Remove(yHairOld); 
       } 

       LineObj xHair = new LineObj(ZedGraph.GraphPane.XAxis.Scale.Min, y, ZedGraph.GraphPane.XAxis.Scale.Max, y); 
       LineObj yHair = new LineObj(x, ZedGraph.GraphPane.YAxis.Scale.Min, x, ZedGraph.GraphPane.YAxis.Scale.Max); 

       ZedGraph.GraphPane.GraphObjList.Add(xHair); 
       xHairOld = xHair; 

       ZedGraph.GraphPane.GraphObjList.Add(yHair); 
       yHairOld = yHair; 

       CrossHairY = y; 
       CrossHairX = x; 


       ZedGraph.Refresh(); 
      } 

      #endregion 
    } 

和鼠標離開事件

private void ZedGraph_MouseLeave(object sender, EventArgs e) 
     { 
      if (CrossHairX != null && CrossHairY != null) 
      { 
       ZedGraph.GraphPane.GraphObjList.Remove(xHairOld); 
       ZedGraph.GraphPane.GraphObjList.Remove(yHairOld); 
       ZedGraph.Refresh(); 
      } 
     } 

我覺得你的問題在perfomance。 LineObj更快然後圖形