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:
- Derive from Panel rather then UserControl.
- Override the OnPaintBackground function:
protected override void OnPaintBackground(PaintEventArgs pevent)
{
//do nothing
}
- Override the OnMove function:
protected override void OnMove(EventArgs e)
{
RecreateHandle();
}
- Override the CreateParams property:
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
return cp;
}
}
- Override the OnPaint function:
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
//Do your drawing here
.
.
.
g.Dispose();
}