避免在Windows窗体中闪烁?

双缓冲无法使用combobox。 还有其他方法可以避免在Windows窗体中闪烁吗?

我有一个窗口forms,其中有多个面板。 我根据菜单选择一次只显示一个面板。

我有一个图标面板,一个标题面板和combobox。 基于该combobox的选定项目,gridview1和2正在填充。 当我使用键盘向下箭头快速选择combobox项目时,图标面板和标题面板总是重新绘制。 我需要保持这两点而不做任何改变。 当我改变combobox选择的索引时,这两个面板产生一些闪烁效果(即,它们闪烁或闪烁)。 有没有办法避免这种闪烁。 我尝试在表单构造函数和表单加载事件中启用双缓冲。 请帮忙…………..

InitializeComponent(); this.SetStyle(ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, false); this.SetStyle(ControlStyles.Opaque, false); this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); this.SetStyle(ControlStyles.ResizeRedraw, true); 

我在表单构造器和表单加载事件中尝试了此代码

又一个解决方案:

 //TODO: Don't forget to include using System.Runtime.InteropServices. internal static class NativeWinAPI { internal static readonly int GWL_EXSTYLE = -20; internal static readonly int WS_EX_COMPOSITED = 0x02000000; [DllImport("user32")] internal static extern int GetWindowLong(IntPtr hWnd, int nIndex); [DllImport("user32")] internal static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); } 

您的表单构造函数应如下所示:

 public MyForm() { InitializeComponent(); int style = NativeWinAPI.GetWindowLong(this.Handle, NativeWinAPI.GWL_EXSTYLE); style |= NativeWinAPI.WS_EX_COMPOSITED; NativeWinAPI.SetWindowLong(this.Handle, NativeWinAPI.GWL_EXSTYLE, style); } 

在上面的代码中,您可以更改此。处理this.Handle类的MyFlickeringPanel.Handle

您可以在此处阅读更多相关内容: 扩展窗口样式和此处: CreateWindowEx 。

使用WS_EX_COMPOSITED设置,窗口的所有后代都使用双缓冲从底部到顶部绘制顺序。 从下到上的绘制顺序允许后代窗口具有半透明(alpha)和透明度(颜色 – 键)效果,但前提是后代窗口也设置了WS_EX_TRANSPARENT位。 双缓冲允许窗口及其后代被绘制而不会闪烁。

解决方案#1:
在添加项目之前使用ComboxBox.BeginUpdate() 。 这将阻止Control在每次将项添加到列表时重新绘制ComboBox 。 添加项目后,可以使用ComboBox.EndUpdate()进行重绘。

解决方案#2

 private void EnableDoubleBuffering() { this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); } 
  protected override CreateParams CreateParams { get { CreateParams handleParam = base.CreateParams; handleParam.ExStyle |= 0x02000000; // WS_EX_COMPOSITED return handleParam; } }