Creating Shaped Forms and Controls


The shape of a Windows Form doesn't necessarily have to be rectangular. Each form has a Region property that can be used to programmatically change the shape of a form using a GraphicsPath object. The GraphicsPath class is used to build and represent shapes. The full details of using this class to build highly complex shapes are beyond the scope of this section of the chapter, but you can find out more about this class using the online MSDN reference.

To create a form that has a customized region (shaped form), all you need to do is override the OnPaint method and change the Region property, as shown in the following code:

protected override void OnPaint(PaintEventArgs e) {     base.OnPaint(e);     GraphicsPath shape = new GraphicsPath();     shape.AddEllipse(0, 31, this.Width, this.Height-31);     this.Region = new Region(shape); } 


This creates an ellipse that starts 31 pixels below the top of the window, completely hiding the original window's title bar from the new shape. Figure 36.5 shows a shaped form on top of the Visual Studio IDE. As you can see, areas of the window's original rectangle that fall outside the shape defined in the Region property end up being transparent.

Figure 36.5. A shaped Windows form.


Although it is extremely compelling to create a Windows Form that appears as an ellipse, there is also potential for creating nice user interfaces by modifying control shapes. For example, you can create pill-shaped buttons by taking a regular rectangle and rounding the edges within the Region property of the control. Quite a few applications have made use of triangular buttons to improve their look and feel.



Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net