[英]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.