持久图形WinForms [英] Persistent graphics WinForms

查看:89
本文介绍了持久图形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有没有我可以设置的任何属性来改善平滑度的绘制图形?

解决方案

目标







失效



任何时候控件(或表单)被调整大小,最小化/最大化,局部模糊或移动时,都必须(部分)重绘。当发生这种情况时,必须重新绘制的控件部分被认为是无效的。



当失效时,控件执行如下操作:


  1. 调用 OnPaintBackground :使用背景颜色填充无效区域。 >
  2. 调用 OnPaint :这将在背景上绘制文本和图形。



为什么 OnPaint 导致闪烁



您已覆盖 OnPaint 方法的控件。每当控件重新绘制时,您都会看到控件的一个闪光灯,其中只绘制其背景色。这是在 OnPaintBackground 被调用之后,并且在调用 OnPaint 之前。



解决方案




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:

  1. Call OnPaintBackground: this fills the invalidated region with the background color.
  2. 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):

    1. In the Load event: create a new Bitmap object.
    2. Fill it with the background color and draw the lines and shapes on it.
    3. Assign this Bitmap object to the control's BackgroundImage property.

  • If you have a static image that must resize when the control resizes:

    1. Override the OnResize method and create the new Bitmap in there. Use the control's ClientSize property for the size of the Bitmap.
    2. Fill it with the background color and draw the lines and shapes on it.
    3. Assign this Bitmap object to the control's BackgroundImage property.

  • If you have an animated image:

    1. 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.
    2. Override the control's OnPaint method. Get the Graphics context of the control and draw the lines and shapes directly on the control.
    3. 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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