问题描述
我有一个WinForms应用程序,我必须在控件之间绘制一些行。这些行必须是持久的,所以我重写表单 code>属性。
如果您有一个静态图像,在控件调整大小时必须调整大小:
- 覆盖为位图的大小。
- 填充背景颜色和在其上绘制线条和形状。
- 将这个位图对象赋给控件的 BackgroundImage $ c
如果您有动画图片:
- 在 Load 事件中设置。获取,并直接在控件上绘制线条和形状。 创建并启用新的,并在其回调方法中调用控件的()。设置计时器以触发,例如每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
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 new Bitmap object.
- Fill it with the background color and draw the lines and shapes on it.
- Assign this Bitmap object to the control's BackgroundImage property.
If you have a static image that must resize when the control resizes:
- Override the OnResize method and create the new Bitmap in there. Use the control's ClientSize property for the size of the Bitmap.
- Fill it with the background color and draw the lines and shapes on it.
- Assign this Bitmap object to the control's BackgroundImage property.
If you have an animated image:
- In the Load event set the DoubleBuffered property of the control to true. Setting this prevents the flicker you saw as it uses an invisible buffer to draw the control.
- Override the control's OnPaint method. Get the Graphics 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's Invalidate method followed by the Update method (as shown here). Set the timer to fire, for example, every 40 ms.
这篇关于持久图形WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!