如何在Zedgraph中绘制一个扩展的交叉毛线光标,该光标在鼠标不移动时持续存在
本文关键字:光标 鼠标 毛线 移动 存在 绘制 Zedgraph 扩展 一个 | 更新日期: 2023-09-27 18:07:30
我正在使用Zedgraph创建一个股票图表应用程序。该图表有多个显示指标的窗格。我想显示一个扩展到Zedgraph控件范围的十字线光标,包括所有的图形窗格。使用下面的代码和MouseMove事件,我完成了一半的工作。然而,当鼠标停止移动时,准星就会消失。当鼠标静止时,我如何保持它们的显示?我的Zedgraph控件在下面的代码中被命名为'chtOHLC'。
#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
试试:将它添加到表单类
ZedGraph这是我的库实例
private double? CrossHairX = null;
private double? CrossHairY = null;
LineObj xHairOld = new LineObj();
LineObj yHairOld = new LineObj();
ZedGraph鼠标移动事件:
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();
}
}
我认为你的问题在性能上。LineObj比Graphics快