C# 如何双缓冲面板?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/818415/
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
How do I double buffer a Panel?
提问by
I have a panel that has a roulette wheel on it, and I need to double buffer the panel, so that it stops flickering. Can anyone help me out?
我有一个面板,上面有一个轮盘,我需要对面板进行双重缓冲,以使其停止闪烁。谁能帮我吗?
EDIT:
编辑:
Yes, I have tried that.
是的,我试过了。
panel1.doublebuffered does not exist, only this.doublebuffered. And I don't need to buffer the Form, just the Panel.
panel1.doublebuffered 不存在,只有 this.doublebuffered。而且我不需要缓冲表单,只需要缓冲面板。
回答by JP Alioto
Winform panels have a DoubleBuffered property.
Winform面板有一个DoubleBuffered 属性。
Edit: I should have noticed that it was protected. Others have described how to sub-class it. :)
编辑:我应该注意到它受到保护。其他人已经描述了如何对其进行子类化。:)
回答by user79755
You need to derive from Panel or PictureBox.
您需要从 Panel 或 PictureBox 派生。
There are ramifications to this depending on how you choose to enable the buffering.
这取决于您选择启用缓冲的方式。
If you set the this.DoubleBuffer flag then you should be ok.
如果您设置了 this.DoubleBuffer 标志,那么您应该没问题。
If you manually update the styles then you have to paint the form yourself in WM_PAINT.
如果您手动更新样式,那么您必须自己在 WM_PAINT 中绘制表单。
If you really feel ambitious you can maintain and draw your own back buffer as a Bitmap.
如果你真的有雄心壮志,你可以维护和绘制你自己的后台缓冲区作为位图。
using System.Windows.Forms;
public class MyDisplay : Panel
{
public MyDisplay()
{
this.DoubleBuffered = true;
// or
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
UpdateStyles();
}
}
回答by Koopakiller
You can make the DoubleBuffered
-Property public in a derivided class of Panel
:
您可以DoubleBuffered
在以下派生类中公开 -Property Panel
:
public class DoubleBufferedPanel : Panel
{
[DefaultValue(true)]
public new bool DoubleBuffered
{
get
{
return base.DoubleBuffered;
}
set
{
base.DoubleBuffered = value;
}
}
}
回答by Jakob Danielsson
Another way of doing this is to invoke the member doublebuffered, using the InvokeMember method:
另一种方法是使用 InvokeMember 方法调用双缓冲成员:
typeof(Panel).InvokeMember("DoubleBuffered", BindingFlags.SetProperty
| BindingFlags.Instance | BindingFlags.NonPublic, null,
panel2, new object[] { true });
By doing it this way, you don't have to create a subclass
通过这样做,您不必创建子类
回答by Jyclop
Just expanding on User79775's answer, if you're trying to achieve this in VB.net, do so like this:
只是扩展 User79775 的答案,如果您想在 VB.net 中实现这一点,请这样做:
Imports System.Windows.Forms
Public Class MyDisplay
Inherits Panel
Public Sub New()
Me.DoubleBuffered = True
' or
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
UpdateStyles()
End Sub
End Class