简体   繁体   English

Windows窗体:在C#中的图表上创建透明图形

[英]windows forms: create transparent graphic upon a chart in C#

I exhaustively used the search but have not been able to find a satisfying solution to my problem. 我详尽地使用了搜索,但未能找到令人满意的解决方案。

I programmed a data visualization using a chart (datavisualization.charting.chart). 我使用图表(datavisualization.charting.chart)对数据可视化进行了编程。 This chart changes steadily since it shows some kind of simulation results. 该图稳定地变化,因为它显示了某种模拟结果。 I would like to draw for example a line upon the chart (depending on the mouse position) in order to show context sensitive information. 我想在图表上画一条线(取决于鼠标位置)以显示上下文相关信息。

Therefore, I tried two implementations, none of both works exactly as I would like it to: 因此,我尝试了两种实现,但两种方法都不完全符合我的意愿:

plotLine is called on MouseMove- Event 在MouseMove事件上调用plotLine

gGraph is created from the underlying graph using: chartCreateGraphics(); 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();     
  }
}

Here, the line disappears each time immediately after being plotted, but should remain as long as the mouse is not moved. 此处,该线在绘制后立即消失,但是只要不移动鼠标,该线就会保留。

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

Here, all plotted lines remain in the graphics surface as long as the chart is not plotted by new. 在这里,只要图表不是由new绘制的,所有绘制的线都保留在图形表面中。 (only the last line should remain in order to indicate the mouse position) (仅保留最后一行以指示鼠标位置)

Can anybody help me? 有谁能够帮助我?

You should be drawing in the OnPaint event. 您应该在OnPaint事件中进行绘制。 You are circumventing the update model and are seeing the effects of doing so. 您正在规避更新模型,并看到了这样做的效果。 Sure, you do some drawing in MouseMove, but when the Paint event fires it will simply be erased. 当然,您可以在MouseMove中进行一些绘制,但是当Paint事件触发时,它将被简单地擦除。

Put your code in OnPaint first. 首先将代码放在OnPaint中。 On mouse move simply record whatever data you need to (ie, mouse position) and then call Invalidate() on the chart. 鼠标移动时,只需记录所需的任何数据(即鼠标位置),然后在图表上调用Invalidate()。 When you do that the Paint event will be called and your drawing code will fire. 当您执行此操作时,将调用Paint事件,并且将触发您的绘图代码。

Rule of thumb; 经验法则; never draw from anywhere but the Paint event. 除了“绘画”事件外,切勿从任何地方绘画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM