C# Programming > Windows Forms

Irregular-Shaped Windows Form

irregular windows form

Non-Rectangular C# Windows Form

Creating custom shaped Forms in C#.NET can be of infinite uses. Most people use them to simply recreate a non-rectangular Windows Forms that stand out from the competion and wows users.

Before the .NET Framework 2.0, creating a custom shape Windows Form in C# required extensive use of API calls. Luckily starting with C# Net 2.0 those API calls are handled automatically for us, making non-rectangular Windows Forms easy to program. Only one Windows Form property needs to be modified: TransperancyKey

Let's see how to use the TransperancyKey property to create a stunning custom shape Form with pure C# code.

Creating the Image

All C# irregular-shaped Windows Forms, however created, need a reference shape. TransperancyKey will also require the referenced shape to becomes the background image of the Form. With that said we need to create a fancy background image.

For this example I created an abstract shape using Photoshop (included in the project files). Feel free to create one with any image creating software.

The important concept here is, the background image needs to have a contrasting background color. Also another important tip is, avoid anti-aliasing. The TransperancyKey property will only accept a single color when creating a custom shape Form.

Applying the Transparency

So what does the TransperancyKey do? It represents the color that the Windows Form will not draw. That means that if you have your C# code set it to equal the contrast color, the result will be a custom shaped Windows Form.

So the process is extremely simple:

  • Set the Form's background image to the custom shaped image
  • Set the TransperancyKey to the contrast color
  • Format the Form for better results: remove the border and make its Size the same as the background image
this.BackgroundImage = //Image
this.FormBorderStyle = FormBorderStyle.None;
this.Width = this.BackgroundImage.Width;
this.Height = this.BackgroundImage.Height;
this.TransparencyKey = Color.FromArgb(0, 255, 0); //Contrast Color

Behind the scenes the TransparencyKey is using API calls to get the window to ignore the given color. C# now handles all that for you, thus programming irregular-shaped Windows easy to do.

Alternate Method - Regions

TransparencyKey does have some downsides, one being that we can only specify one color of transparency, making anti-aliased images difficult to display. Another common problem is that under certain monitor color depths the effect will fail.

If you would like a more advanced and flexible way to create an irregular shaped Windows Form in C#, check out the Non-Rectangular Windows Form - Region article...

Back to C# Article List