Browse DevX
Sign up for e-mail newsletters from DevX


Shaped .NET Windows Forms

.NET Windows Forms make it very easy to create forms that are non-rectangular by assigning a vector based outline as the region of the form. This, paired with appropriate graphics art, makes for very compelling form designs.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

ince the earliest versions of the Windows operating system, all Windows have been rectangular. However, the number of applications that break out of this boring mold is rising. Even Microsoft, the king of rectangularity, is starting to create applications that use shaped windows, such as Media Player. Unfortunately, creating shaped forms has always been quite tricky. Until now that is! The .NET Framework and the Windows Forms package in particular make it easy to produce forms that have rather sophisticated shapes. Since I already brought it up, I am going to stick to Windows Media Player as an example. You are likely to have Media Player on your computer, so you can easily try out some of the things I will point out to you. Also, Media Player incorporates a number of different techniques that make for excellent examples.

This article will demonstrate how to build shaped forms with shaping-functionality very similar to Windows Media Player. Before I show you that kind of advanced functionality let me walk you through some of the basics. Our First Shaped Form
The general principle of a shaped form is simple. You need to define the outline of the form based on vectors and assign that definition to the form.

Form shapes are defined by Regions. Region is a .NET Framework class. Every Windows Form has a member object called Region, but by default, Windows Forms do not have custom regions assigned. The object reference is null (C#) or Nothing (VB.NET). In this case the form will appear rectangular (Windows XP themes may actually alter a form's appearance, but I will ignore this detail here). To change the shape of a form you can instantiate a Region class and fill it with information about the shape you want to assign to the form. The easiest way to do that is to use a GraphicsPath object. GraphicsPath is a GDI+ class that you'll find in the System.Drawing.Drawing2D namespace. The concept of a GraphicsPath is very simple. It is capable of describing a vector-based shape. Just what you need to define the outline of your form! The defined path (see the GDI+ sidebar) is handed to the constructor of the Region object, which will automatically convert that path information into region data. You'll assign the region data to the form, and voila, the form changes its shape.

Luckily, for simple shapes things are pretty straightforward. .NET's GraphicsPath class is rather powerful and some of its methods are very sophisticated. To shape the forms presented in this article, however, I'm going to stick to simple paths composed out of lines and arcs.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date