Making Transparent Controls using GDI+ and C#


This article provides an approach to a Transparent Control that draws an ellipse with real transparent background and a brush that supports transparent colors.

Below sample project is included in the ZIP file. In addition, you will find a file that contains the control TranspControl.ddl. It was made and based on technique presented in this article.



To make a transparent control you may create a default control and proceed with the following steps:

1. Add the transparent style to the control window.

Protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}

2. Override the OnPaintBackground event. This is necessary to prevent the background to be painted.

Protected override void OnPaintBackground(PaintEventArgs e)
{
// do nothing
}

3. Override the OnMove event with the following code.

Protected override void OnMove(EventArgs e)
{
RecriateHandle();
}


Similar Articles