强制形式重绘? [英] Force Form To Redraw?

查看:126
本文介绍了强制形式重绘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中的WinForms - 我在绘制实时线图是基于通过串口接收每500毫秒数据

In C# WinForms - I am drawing a line chart in real-time that is based on data received via serial port every 500 ms.

e.Graphics。 DrawLine的逻辑是表单的OnPaint处理中。

The e.Graphics.DrawLine logic is within the form's OnPaint handler.

有一次,我收到来自串行端口的数据,我需要调用的东西,导致窗体重绘,这样的OnPaint处理被调用。我曾尝试this.Refresh和this.Invalidate,什么情况是,我失去了一切已经在表格上以前绘制的。

Once I receive the data from the serial port, I need to call something that causes the form to redraw so that the OnPaint handler is invoked. I have tried this.Refresh and this.Invalidate, and what happens is that I lose whatever had been drawn previously on the form.

有另一种方式没有实现这一目标失去了什么已经绘窗体上?

Is there another way to achieve this without losing what has been drawn on your form?

感谢。

克里斯

推荐答案

的一点是,你应该想想什么地方存储您的绘图数据。正如已经说过的,一个缓冲器的位图是一个解决方案。但是,如果你没有太多的画,有时更容易,更好地图形数据存储在一个变量或数组和重绘在OnPaint事件的一切。

The point is that you should think about storing your drawing data somewhere. As already said, a buffer bitmap is a solution. However, if you have not too much to draw, sometimes it is easier and better to store your drawing data in a variable or an array and redraw everything in the OnPaint event.

假设你收到应添加到图表的一些点数据。所有的杉杉您创建一个列表:

Suppose you receive some point data that should be added to the chart. Firs of all you create a point List:

List<Point> points = new List<Point>();



然后,每次你得到一个新的点时间,你把它添加到列表中,并刷新表单:

Then each time you get a new point you add it to the list and refresh the form:

points.Add(newPoint);
this.Refresh();

在OnPaint事件把下面的代码:

In the OnPaint event put the following code:

private void Form_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawLines(Pens.Red, points);
}

这工作相当快达莫名其妙100万点,并使用比内存要少得多缓冲位图的解决方案。但是,你应该决定根据图纸的复杂性要使用的方法。

This works quite fast up to somehow 100 000 points and uses much less memory than the buffer bitmap solution. But you should decide which way to use according to the drawing complexity.

这篇关于强制形式重绘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆