eselk
eselk

Reputation: 6894

Remove black flicker on first show of Winform with TransparencyKey set

The following code produces a black flicker on the screen right before the form is displayed (transparently), I'm wondering what my options are for removing that flicker?

   Form f = new Form();
   f.BackColor = Color.Lime;
   f.TransparencyKey = f.BackColor;
   f.StartPosition = FormStartPosition.Manual;
   f.Bounds = Screen.PrimaryScreen.WorkingArea;
   f.Show();

I get the same results if I create a new project, set the background of the form to Lime and the TransparencyKey to Lime, then click Run.

Things I've tried:

  1. Set Opacity to 99% -- same flicker

  2. Force WS_EX_COMPOSITED in OnCreateParams or using SetWindowLong -- same flicker

  3. Show the window smaller, or 0 width, or off screen, then move to desired location -- causes bad display issues where the windows behind my form do not redraw correctly.

  4. Setting ControlStyles.Opaque, ControlStyles.UserPaint, and several other ControlStyles combos and overriding different paint/background-paint events -- various results, either same flicker, worse flicker, or form not transparent.

Moving to WPF might be an option, but not really looking for "use WPF" as an answer.

Upvotes: 1

Views: 1599

Answers (1)

John Arlen
John Arlen

Reputation: 6698

Set the Opacity to 0.01.

If you need the form (or parts of it) visible - then re-set the Opacity once the form creation is complete:

Form f = new Form { Opacity = 0.01 };
f.Show();
f.BeginInvoke( new Action(() => f.Opacity = 0.99 ));

EDIT: Updated cleaner as Tergiver suggested

Upvotes: 5

Related Questions