C#Windows窗体:拆分器闪烁
问题描述:
我遇到以下问题:我在窗体中放置了一个拆分器控件(而不是拆分容器),并添加了2个面板.分离器工作正常,但是当我移动分离器时,它开始闪烁-面板不工作.
I have the following problem: I placed a splitter-control (not split-container) in my form and added 2 panels. The splitter works properly but when I move the splitter, it starts to flicker - the panels dont.
使用分割容器可以获得相同的结果.
I get the same result with a Split-Container.
我尝试了这个,但是没有用
I tried this but nothing works
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.DoubleBuffered = true;
...
class XSplitter : Splitter
{
public XSplitter() : base()
{
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.DoubleBuffered = true;
}
}
class XPanel : Panel
{
public XPanel() : base()
{
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.DoubleBuffered = true;
}
}
我使用Windows 8.1和VS 2010
I use Windows 8.1 and VS 2010
寻求帮助!
答
以下是使用此控件的步骤:
Here are the steps to use this control:
- 将新类"NonFlickerSplitContainer"添加到C#应用程序中.
- 用下面显示的C#代码替换自动生成的类代码.
-
在应用程序中使用NonFlickerSplitContainer对象而不是SplitContainer对象.
- Add new class "NonFlickerSplitContainer" to your C# application.
- Replace auto generated class code with C# code shown below.
Use NonFlickerSplitContainer object instead of SplitContainer object in your application.
public partial class NonFlickerSplitContainer : SplitContainer
{
public NonFlickerSplitContainer()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer, true);
MethodInfo objMethodInfo = typeof(Control).GetMethod("SetStyle",BindingFlags.NonPublic|BindingFlags.Instance);
object[] objArgs = new object[] { ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer, true };
objMethodInfo.Invoke(this.Panel1, objArgs);
objMethodInfo.Invoke(this.Panel2, objArgs);
}
}