David Hood
David Hood

Reputation: 21

How do I draw an extended cross hair cursor in Zedgraph that is persistent when the mouse is not moving?

I am creating a stock charting application using Zedgraph. The chart has multiple panes for indicators. I would like to display a crosshair cursor that extends to the extents of the Zedgraph control encompassing all graph panes. I have half the work done using the following code and the MouseMove event. However, when the mouse stops moving the crosshairs disappear. How do I keep them shown when the mouse is stationary? My Zedgraph control is named 'chtOHLC' in the code below.

#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

Upvotes: 2

Views: 2723

Answers (1)

Try that: add it to your form class

ZedGraph here is my instanse of library

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

ZedGraph mouse move event:

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 
    }

And mouse leave event

 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();
            }
        }

I think your problem in perfomance. LineObj faster then Graphics

Upvotes: 6

Related Questions