Tuesday, May 18, 2004

Shaped forms in .NET

Shaped forms are often used nowadays to increase the visual appeal of an application, when done correctly and combined with some good graphics they can look extremely good. A common example is Windows Media Player.
Using some very simple code, it is possible to implement form shaping in .NET, the following examples are in C#.

The basic idea behind form shaping in .NET is as follows:
1. Create a new "GraphicsPath" object.
2. Add a shape to the new GraphicsPath object.
3. Set your forms "Region" property to be a new "Region" object using your new GraphicsPath object as the constructor argument.
Its as simple as that.

The GraphicsPath object has a number of methods to aid in adding shapes, for example:
AddArc
AddBezier
AddClosedCurve
AddCurve
AddEllipse
AddLine
AddPie
AddPolygon
AddRectangle

The "AddPolygon" method is especially useful as this allows you to create any shape you like, by adding an array of points to the GraphicsPath.

I have created an example application that will allow you to create any shaped form you like, by clicking or "drawing" out the regions of your desired form. To download the example click here (coming soon, I promise!).

Here is a simple example that will provide a circular shaped form, place it in your forms Load event:

GraphicsPath p = new GraphicsPath();
p.AddEllipse(0,0,200,200);
newForm.Region= new Region(p);


No comments: