C# WinForms Flicker+Minimize Restore Fix

One of the normal issues when developing a WinForms application is that the controls can flicker when the Window is activated or loaded. The correct fix for this is to use the WS_EX_COMPOSITED attribute in the Windows form style.. Example code given below, This code is called in the form constructor:

  int style = NativeWinAPI.GetWindowLong(this.Handle, NativeWinAPI.GWL_EXSTYLE);
  style |= NativeWinAPI.WS_EX_COMPOSITED;
  NativeWinAPI.SetWindowLong(this.Handle, NativeWinAPI.GWL_EXSTYLE, style);

Once this is in place, then flicker becomes almost non-existent. However a side-effect of this fix is that , when a window is minimized and restored, some parts of the window do not repaint and thus you end up with a partial window. In other words some parts of the window appear transparent and you can see the desktop through the transparency.

By default, this will happen for all the parts of the windows form which have a black color. This is caused by the Transparency Key attribute of the window, which is set to Black by default. To rectify this, you just need to change the color to something else. Preferably some color which is not used anywhere in the window.

Be the first to comment

Leave a Reply

Your email address will not be published.


*