当OnPaint()方法不拉双缓冲:为什么没有工作? [英] Double Buffering when not drawing in OnPaint(): why doesn't it work?

查看:260
本文介绍了当OnPaint()方法不拉双缓冲:为什么没有工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个简单的矢量绘图应用程序,在C#/。NET。该图是在一个小组做的,但我不使用的OnPaint()事件,这一切 - 其实OnPaint()方法甚至只是调用另一个方法,实际上绘制的一切文件中

我试图添加双缓冲,但是当我设置DoubleBuffered为true,闪烁的问题更是雪上加霜。为什么是这样?如果我要加倍缓冲控制,我绝对要尽我的画中的OnPaint()事件,与所提供的图形对象,而不是使用Panel.CreateGraphics(),然后画上了吗?

编辑:这是基本的code我使用

 私人无效doc_Paint(对象发件人,PaintEventArgs的E)
{
    G = doc.CreateGraphics();
    渲染(比例因子,偏移量);
}

私人无效渲染(浮动比例因子,的PointF偏移)
{
    的foreach(线X在Document.Lines){的DrawLine(X.PointA,X.PointB,X.Color,X.LineWidth); }
}
私人无效的DrawLine(的PointF A,的PointF B,色色,漂浮宽)
{
    笔P =新钢笔(颜色,宽度);
    的PointF PA =新的PointF(((AX + Offset.X)*比例因子),((AY + Offset.Y)*比例因子));
    的PointF PB =新的PointF(((BX + Offset.X)*比例因子),((BY + Offset.Y)*比例因子));
    g.Smoothi​​ngMode = System.Drawing.Drawing2D.Smoothi​​ngMode.AntiAlias​​;
    g.DrawLine(对,PA,PB);
}
 

总的想法是,两个变量,比例因子和偏移量,指的是缩放级别和平移水平在UI中。 g是一个Graphics对象。

解决方案

  G = doc.CreateGraphics();
 

那是错误的。如果你画到缓冲区双缓冲只能工作。该e.Graphics引用之一。修正:

  G = e.Graphics;
 


要注意的是面板没有双缓冲默认打开。你需要派生自己。粘贴到一个新的类:

 使用系统;
使用System.Windows.Forms的;

类BufferedPanel:面板{
    公共BufferedPanel(){
        this.DoubleBuffered = TRUE;
        this.ResizeRedraw = TRUE;
    }
}
 

编译。它从工具箱的顶部下降。

I'm working on a simple vector drawing app in C#/.Net. The drawing is done in a panel, but I'm not using the OnPaint() event for all of it - in fact the OnPaint() even just calls another method which actually draws everything in the document.

I tried to add double buffering, but when I set DoubleBuffered to true, the flicker issue is even worse. Why is this? If I want to double buffer the control, do I absolutely have to do all my drawing in the OnPaint() event, with the supplied Graphics object, instead of using Panel.CreateGraphics() and then drawing to that?

EDIT: This is the basic code I am using.

private void doc_Paint(object sender, PaintEventArgs e)
{
    g = doc.CreateGraphics();
    Render(ScaleFactor, Offset);
}    

private void Render(float ScaleFactor, PointF offset)
{
    foreach (Line X in Document.Lines) { DrawLine(X.PointA, X.PointB, X.Color, X.LineWidth); }
}
private void DrawLine(PointF A, PointF B, Color Color, float Width)
{
    Pen p = new Pen(Color, Width);
    PointF PA = new PointF(((A.X + Offset.X) * ScaleFactor), ((A.Y + Offset.Y) * ScaleFactor));
    PointF PB = new PointF(((B.X + Offset.X) * ScaleFactor), ((B.Y + Offset.Y) * ScaleFactor));
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    g.DrawLine(p, PA, PB);
}

The general idea is that the two variables, ScaleFactor and Offset, refer to the zoom level and pan level in the UI. g is a Graphics object.

解决方案

g = doc.CreateGraphics();

That's the mistake. Double-buffering can only work if you draw into the buffer. The one that e.Graphics references. Fix:

g = e.Graphics;


Beware that Panel doesn't have double-buffering turned on by default. You'll need to derive your own. Paste this into a new class:

using System;
using System.Windows.Forms;

class BufferedPanel : Panel {
    public BufferedPanel() {
        this.DoubleBuffered = true;
        this.ResizeRedraw = true;
    }
}

Compile. Drop it from the top of the toolbox.

这篇关于当OnPaint()方法不拉双缓冲:为什么没有工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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