[英]windows forms: create transparent graphic upon a chart in C#
我详尽地使用了搜索,但未能找到令人满意的解决方案。
我使用图表(datavisualization.charting.chart)对数据可视化进行了编程。 该图稳定地变化,因为它显示了某种模拟结果。 我想在图表上画一条线(取决于鼠标位置)以显示上下文相关信息。
因此,我尝试了两种实现,但两种方法都不完全符合我的意愿:
在MouseMove事件上调用plotLine
gGraph是使用以下图表从基础图创建的:chartCreateGraphics();
protected void plotLine(object sender, System.EventArgs e)
{
if (this.chart.Series.Count > 0) //ensure that the chart shows data
{
plotChart(); // this plots the underlying chart
penGraph = new Pen(Color.Black);
Point point1 = new Point(Form1.MousePosition.X - chart.Location.X, 0);
Point point2 = new Point(Form1.MousePosition.X - chart.Location.X,
chart.Location.Y + chart.Size.Height);
gGraph.DrawLine(penGraph, point1, point2);
penGraph.Dispose();
}
}
此处,该线在绘制后立即消失,但是只要不移动鼠标,该线就会保留。
protected void plotLine(object sender, System.EventArgs e)
{
penGraph = new Pen(Color.Black);
Point point1 = new Point(Form1.MousePosition.X - chart.Location.X, 0);
Point point2 = new Point(Form1.MousePosition.X - chart.Location.X,
chart.Location.Y + chart.Size.Height);
gGraph.DrawLine(penGraph, point1, point2);
penGraph.Dispose();
}
在这里,只要图表不是由new绘制的,所有绘制的线都保留在图形表面中。 (仅保留最后一行以指示鼠标位置)
有谁能够帮助我?
您应该在OnPaint事件中进行绘制。 您正在规避更新模型,并看到了这样做的效果。 当然,您可以在MouseMove中进行一些绘制,但是当Paint事件触发时,它将被简单地擦除。
首先将代码放在OnPaint中。 鼠标移动时,只需记录所需的任何数据(即鼠标位置),然后在图表上调用Invalidate()。 当您执行此操作时,将调用Paint事件,并且将触发您的绘图代码。
经验法则; 除了“绘画”事件外,切勿从任何地方绘画。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.