持久图形WinForms [英] Persistent graphics WinForms
问题描述
我有一个WinForms应用程序,我必须在控件之间绘制一些行。这些行必须是持久的,所以我重写表单 OnPaint()
事件。
问题是,
我创建的图形如下所示:
pre> Graphics g;
g = this.CreateGraphics();
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.FillRectangle(Brushes.White,this.ClientRectangle);
并画出如下行:
<$使用(笔笔=新笔(Color.Black,4)){
pen.StartCap = System($)使用公共无效线图(Control L1,Control L2){
.Drawing.Drawing2D.LineCap.Flat;
pen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
int x1,x2,y1,y2;
//选择x / y坐标
g.DrawLine(pen,x1,y1,x2,y2);
$ b有没有我可以设置的任何属性来改善平滑度的绘制图形?
目标
失效
任何时候控件(或表单)被调整大小,最小化/最大化,局部模糊或移动时,都必须(部分)重绘。当发生这种情况时,必须重新绘制的控件部分被认为是无效的。
当失效时,控件执行如下操作:
- 调用
OnPaintBackground
:使用背景颜色填充无效区域。 >
- 调用
OnPaint
:这将在背景上绘制文本和图形。
为什么 OnPaint
导致闪烁
您已覆盖 OnPaint
方法的控件。每当控件重新绘制时,您都会看到控件的一个闪光灯,其中只绘制其背景色。这是在 OnPaintBackground
被调用之后,并且在调用 OnPaint
之前。
解决方案
-
如果你有一个静态图像(即它永远不会改变):
- 在
Load
事件中:创建一个新的位图
对象。 - 将这个
位图
对象分配给该背景颜色并在其上绘制线条和形状。控件的BackgroundImage code>属性
。
- 在
-
如果您有一个静态图像,在控件调整大小时必须调整大小:
- 覆盖 Bitmap 。使用控件的
ClientSize 属性
为位图
的大小。 - 填充背景颜色和在其上绘制线条和形状。
- 将这个
位图
对象赋给控件的BackgroundImage $ c
- 覆盖 Bitmap 。使用控件的
-
如果您有动画图片:
- 在
Load
事件中设置控件的DoubleBuffered
属性为true
。设置此选项可以防止您看到的闪烁,因为它使用不可见的缓冲区来绘制控件。 重写控件的OnPaint
方法。获取Graphics
上下文,并直接在控件上绘制线条和形状。 创建并启用新的Timer
object ,并在其回调方法中调用控件的Invalidate
method 后跟更新
方法(如下所示)。设置计时器以触发,例如每40毫秒。
- 在
I've a WinForms application on wich i have to draw some lines between controls. These lines need to be persistent, so i override the form OnPaint()
event.
The problem is that, the re-draw of the lines aren't very smooth.
I'm creating the graphics as follows:
Graphics g;
g = this.CreateGraphics();
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.FillRectangle(Brushes.White, this.ClientRectangle);
And drawing the lines as follows:
public void lineDraw(Control L1, Control L2) {
using (Pen pen = new Pen(Color.Black, 4)) {
pen.StartCap = System.Drawing.Drawing2D.LineCap.Flat;
pen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
int x1, x2, y1, y2;
//choose x/y coordinates
g.DrawLine(pen, x1, y1, x2, y2);
}
}
Is there any property i can set to improve the smoothness of the drawn graphics?
Goal
An image is shown on a control (or form).
Invalidation
Any time the control (or form) is resized, minimalized/maximalized, partically obscured or moved around, it must be (partially) redrawn. When this happens the part of the control that must be redrawn is said to be invalidated.
When invalidated the control does something like this:
- Call
OnPaintBackground
: this fills the invalidated region with the background color. - Call
OnPaint
: this draws the text and graphics on top of the background.
Why OnPaint
causes flickering
You have overridden the OnPaint
method of the control. Every time the control is redrawn you see a flash of the control with only its background color drawn in it. That is after OnPaintBackground
has been called and before OnPaint
has been called.
The solutions
If you have a static image (i.e. it never changes):
- In the
Load
event: create a newBitmap
object. - Fill it with the background color and draw the lines and shapes on it.
- Assign this
Bitmap
object to the control'sBackgroundImage
property.
- In the
If you have a static image that must resize when the control resizes:
- Override the
OnResize
method and create the newBitmap
in there. Use the control'sClientSize
property for the size of theBitmap
. - Fill it with the background color and draw the lines and shapes on it.
- Assign this
Bitmap
object to the control'sBackgroundImage
property.
- Override the
If you have an animated image:
- In the
Load
event set theDoubleBuffered
property of the control totrue
. Setting this prevents the flicker you saw as it uses an invisible buffer to draw the control. - Override the control's
OnPaint
method. Get theGraphics
context of the control and draw the lines and shapes directly on the control. - Create and enable a new
Timer
object and in its callback method call the control'sInvalidate
method followed by theUpdate
method (as shown here). Set the timer to fire, for example, every 40 ms.
- In the
这篇关于持久图形WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!