Five steps for creating a transparent user control

Guest post from Eyal Ron:

After much testing this is the correct formula for a truly transparent user control:

  1. Derive from Panel rather then UserControl.

  2. Override the OnPaintBackground function:
    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
    //do nothing
    }
  3. Override the OnMove function:
    protected override void OnMove(EventArgs e)
    {
    RecreateHandle();
    }
  4. Override the CreateParams property:
    protected override CreateParams CreateParams
    {
    get
    {
    CreateParams cp = base.CreateParams;
    cp.ExStyle = 0×00000020; //WS_EX_TRANSPARENT
    return cp;
    }
    }
  5. Override the OnPaint function:
    protected override void OnPaint(PaintEventArgs e)
    {
    Graphics g = e.Graphics;

    //Do your drawing here
    .
    .
    .

    g.Dispose();
    }

Category: Winforms

4 Responses to “Five steps for creating a transparent user control”

  1. Thankful Developer

    Awesome! This actually worked, unlike the dozen other solutions I found. Thanks!

  2. Eyal Ron

    You are most welcome.

  3. Eyal Ron

    You are most welcome.

  4. Anonymous

    I want to thank you as well. Thanks!!!


Leave a Reply