我的 C# 表单闪烁,我无法修复它。我已经尝试了一切

Dra*_*x64 0 c# forms gdi+ flicker winforms

我已经在这个程序上工作了大约一个月了,尝试了一切。无论我做什么,我的程序都会闪烁,即使在其他机器上也是如此。它只是一个背景、一个图像和一个矩形。没有什么大的或昂贵的。然而它仍然闪烁。我试过将 DoubleBuffered 设置为 true,我试过覆盖 CreateParams(它使我的窗口变黑),我试过使用反射的方法,我试过 SetStyle 来设置 DoubleBuffer、OptimizedDoubleBuffer、UserPaint、AllPaintingInWmPain 和 Opaque当设置为 true 时,他们都没有做任何事情。我已经在 google 和 stackoverflow 上搜索了答案,但没有一个起作用。我无法理解我做错了什么。我添加了代码、exe、编译脚本和图像的 zip。我和其他人的计算机上发生的事情就是 gif 上的内容。该程序每秒更新 4 次,足以绘制背景、图像和正方形的时间,但它仍然闪烁。请我需要帮助。

主文件:

using System;
using System.Threading;
using System.Windows.Forms;

public class main{

    public static void Main(string[] args){
    
        Console.WriteLine("START");
    
        Engine.Init();
    
        while(true){
        
            Engine.MainDraw();
            Application.DoEvents();
        
            Thread.Sleep(250);
        }
    
        Console.WriteLine("END");
    
    }

}
Run Code Online (Sandbox Code Playgroud)

引擎.cs:

using System;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.Drawing;

public class Engine : Form{
    
    public static Engine EngineForm = new Engine();
    public static Graphics g;
    
    /*
    protected override CreateParams CreateParams{
        get{
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x02000000;
            return cp;
        }
    }
    //*/
    
    /*
    public static void enableDoubleBuff(System.Windows.Forms.Control cont){
        System.Reflection.PropertyInfo DemoProp = typeof(System.Windows.Forms.Control).GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        DemoProp.SetValue(cont, true, null);
    }
    //*/
    
    public Engine(){
        
        this.DoubleBuffered = true;
        
        //enableDoubleBuff(this);
        
        this.SetStyle(
            ControlStyles.DoubleBuffer |
            ControlStyles.OptimizedDoubleBuffer |
            ControlStyles.UserPaint |
            ControlStyles.AllPaintingInWmPaint |
            ControlStyles.Opaque
            ,
        true);
        this.UpdateStyles();
    }
    
    public static void Init(){
        EngineForm.Enabled = true;
        EngineForm.Visible = true;
        
        EngineForm.Activate();
        
        g = EngineForm.CreateGraphics();
    }
    
    public static Pen pen = new Pen(Color.FromArgb(255, 0, 0, 0), 5);
    
    public static Image image = Image.FromFile("bulbasaur.png");
    
    public static Color color = Color.FromArgb(200, 200, 255, 255);
    
    public static void MainDraw(){
        g.Clear(color);
        
        g.DrawImage(image, new PointF(0, 0));
        
        g.DrawRectangle(pen, 100, 100, 100, 100);
    }
    
}
Run Code Online (Sandbox Code Playgroud)

程序的样子:

程序输出。

Cor*_*rey 5

您看到的问题是因为CreateGraphics创建了一个Graphics绕过双缓冲的实例,让您可以直接访问窗口的画布。无论您对该Graphics对象做什么,都会立即在屏幕上发生。

要解决此问题,您需要覆盖在正常更新过程中调用的表单方法之一:OnPaintOnPaintBackground。这些接收一个Graphics对象,它将按照您希望的方式工作,包括处理双缓冲:

protected override void OnPaintBackground(PaintEventArgs e)
{
    base.OnPaintBackground(e);
    var g = e.Graphics;
    g.Clear(color);
    g.DrawImage(image, 0, 0);
    g.DrawRectangle(pen, 100, 100, 100, 100);
}
Run Code Online (Sandbox Code Playgroud)

每当重绘窗体时、在绘制控件之前等时都会调用此方法。您可以通过调用Invalidate()以下方法来重绘窗体:

public static void MainDraw()
{
    EngineForm.Invalidate();
}
Run Code Online (Sandbox Code Playgroud)

这会向窗口发送一条消息,告诉它重绘。下一次通过您的事件循环,该消息将被处理并被OnPaintBackground()调用。

结果是平滑、无闪烁的更新。您可以在不Clear引起背景闪烁等的情况下制作动画和其他内容。