gpt4 book ai didi

c# - 如何在 Zedgraph 中绘制一个在鼠标不移动时持续存在的扩展十字线光标?

转载 作者:太空宇宙 更新时间:2023-11-03 21:48:09 26 4
gpt4 key购买 nike

我正在使用 Zedgraph 创建股票图表应用程序。该图表有多个指标 Pane 。我想显示一个十字光标,它延伸到包含所有图形 Pane 的 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 快

关于c# - 如何在 Zedgraph 中绘制一个在鼠标不移动时持续存在的扩展十字线光标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15837612/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com