C# 滚动时如何消除 Windows.Forms 自定义控件中的闪烁?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/64272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 10:23:18  来源:igfitidea点击:

How to eliminate flicker in Windows.Forms custom control when scrolling?

提问by user7305

I want to create a custom control in C#. But every time I have to fully redraw my control, it flickers, even if I use double buffering (drawing to an Image first, and blitting that).

我想在 C# 中创建一个自定义控件。但是每次我必须完全重绘我的控件时,它都会闪烁,即使我使用双缓冲(先绘制到图像,然后再进行 blitting)。

How do I eliminate flicker when I have to fully redraw?

必须完全重绘时如何消除闪烁?

采纳答案by Shaun Austin

You could try putting the following in your constructor after the InitiliseComponent call.

您可以尝试在 InitiliseComponent 调用之后将以下内容放入您的构造函数中。

SetStyle(ControlStyles.OptimizedDoubleBuffer | 
         ControlStyles.UserPaint |
         ControlStyles.AllPaintingInWmPaint, true);

EDIT:

编辑:

If you're giving this a go, if you can, remove your own double buffering code and just have the control draw itself in response to the appropriate virtual methods being called.

如果你想试一试,如果可以的话,删除你自己的双缓冲代码,让控件自己绘制以响应被调用的适当的虚拟方法。

回答by Grokys

You say you've tried double buffering, but then you say drawing to an Image first and blitting that. Have you tried setting DoubleBuffered = true in the constructor rather than doing it yourself with an Image?

你说你已经尝试过双缓冲,但是你说首先绘制到一个图像然后 blitting 。您是否尝试过在构造函数中设置 DoubleBuffered = true 而不是自己使用 Image 来设置?

回答by Eric W

It may be good enough to just call

打电话可能就够了

SetStyle(ControlStyles::UserPaint | ControlStyles::AllDrawingInWmPaint, true);

The flickering you are seeing most likely because Windows draws the background of the control first (via WM_ERASEBKGND), then asks your control to do whatever drawing you need to do (via WM_PAINT). By disabling the background paint and doing all painting in your OnPaint override can eliminate the problem in 99% of the cases without the need to use all the memory needed for double buffering.

您看到的闪烁很可能是因为 Windows 首先绘制控件的背景(通过 WM_ERASEBKGND),然后要求您的控件执行您需要执行的任何绘制(通过 WM_PAINT)。通过禁用背景绘制并在 OnPaint 覆盖中进行所有绘制,可以在 99% 的情况下消除该问题,而无需使用双缓冲所需的所有内存。

回答by Brad Bruce

I pulled this from a working C# program. Other posters have syntax errors and clearly copied from C++ instead of C#

我从一个工作的 C# 程序中提取了这个。其他海报有语法错误,显然是从 C++ 而不是 C# 复制的

SetStyle(ControlStyles.OptimizedDoubleBuffer | 
                        ControlStyles.UserPaint |
                        ControlStyles.AllPaintingInWmPaint, true);