Click here to Skip to main content
Click here to Skip to main content

Shape Control for .NET

By , 31 May 2005
 

Sample Image - ShapeControldotNET.jpg

Introduction

When Microsoft upgraded Visual Studio 6 to Visual Studio 7 (.NET), many of the familiar graphical controls (Image control, Shape control) were dropped. Although similar functionalities previously achieved by these controls can be attained using appropriate classes in the .NET library, much more effort is required.

After getting familiar with graphical programming in .NET, I am able to port most of my VB6 graphical programs to C#. However, I find that time and again, I need to implement replacement functionalities for the VB6 Shape control in .NET. There are probably already many Shape controls for .NET by third parties, but I could not find one to my satisfaction.

In this article, I would like to share with the readers how I attempted to implement the Shape control for .NET.

Background

In VB6, the Shape control is a graphical light-weight control, it is not a real control and does not have a Hwnd property. In .NET, all controls are real controls with Hwnd property. Although there is no equivalent Shape control in Visual Studio .NET, there are various classes in the .NET library that can collectively be used to implement the Shape control.

Basically the Shape control is an image with a certain shape. To handle the image functionalities, there are the Image and Bitmap classes. For shapes, there is the GraphicsPath class.

These classes are the key components for implementing the Shape control.

Transparency

In VB6, we can set the DrawMode property to achieve the effect of seeing through the VB6 Shape control. The DrawMode property could still be implemented using GDI/GDI+ functions in .NET, but there is an easier and more elegant way. .NET supports 32 bit ARGB rendering. In VB6, although 32 bit ARGB values can be assigned to colors, the A (Alpha) component of the value is never used.

For any standard control derived from System.Windows.Form.Control, there are at least two properties that take Color values: BackColor and ForeColor. Each of these properties can be assigned ARGB values.

How would you test for transparency? The answer is a background. All controls must reside within a container. For most cases, the form is the container. However, there are also controls that serve as containers, for example the Panel control. To test for transparency, we can set the container's background to an image and the backcolor of the control within the container to a color where the Alpha is less than 255 (maybe 100). You will then be able to see the background image through the control. The lower the Alpha, the more of the background is seen.

Shape

In VB6, we can also create custom controls with different shapes. However this can only be done by calling Win32 API functions. In .NET, every control has a Region property which can be used to specify its shape. When the control is rendered, Windows will only paint on pixels within the region. A Region can be created by specifying its outline. The outline can be constructed using a GraphicsPath object. The code below creates a GraphicsPath object, adds an ellipse shape to the path and then uses the path to instantiate a new Region object to be assigned to a control's Region property. The end result is that the control will take the shape of the Region, which in this case is an ellipse.

GraphicsPath _outline=new GraphicsPath();

_outline.AddEllipse(0,0,100,100);

this.Region=new Region(_outline);

Custom Design-time Editors

When you use Visual Studio .NET IDE to assign Color properties, you can do so using the standard Color Editor where you can select from a series of colors, or you can directly type in the ARGB values. When you use the standard Color Editor, you cannot specify the Alpha values. And when you directly type in the ARGB values, you do not know how the color will appear. Either way, you do not have an ideal way to enter Color values.

However, Visual Studio .NET allows you to create your own editor to edit property values. For the Shape control, I have created two custom design-time editors. One for editing ARGB values and the other to pick the shape for the Shape control.

In this article, I would not be discussing on how to create a custom design-time editor. You can get more information from the .NET documentation for System.Drawing.Design namespace.

Sample screenshotSample screenshot

Extending Shape Control

The source code for the Shape control (ShapeControl.cs) allows for easy addition/deletion of shapes. All shapes are enumerated in the ShapeType enum block. You can edit this block to add/delete shapes.

public enum ShapeType{
                Rectangle,
                RoundedRectangle,
                Diamond,
                Ellipse,
                TriangleUp,
                TriangleDown,
                TriangleLeft,
                TriangleRight,
                BallonNE,
                BallonNW,
                BallonSW,
                BallonSE,
                CustomPolygon,
                CustomPie
            }

Correspondingly add/edit the block that creates the outline path for the shape.

internal static void updateOutline(ref GraphicsPath outline, 
                     ShapeType shape, int width,int height)
{
    Switch (Shape)
    {
        Case ShapeType.CustomPie:
            outline.AddPie(0,0,width,height,180,270);
            break;
        Case ShapeType.CustomPolygon:
            outline.AddPolygon(new Point[]{
                          new Point(0,0),
                          new Point(width/2,height/4),
                          new Point(width,0),
                          new Point((width*3)/4,height/2),
                          new Point(width,height),
                          new Point(width/2,(height*3)/4),
                          new Point(0,height),
                          new Point(width/4,height/2)
                                          }
                );
            break;
        Case ShapeType.Diamond:
            outline.AddPolygon(new Point[]{
                        new Point(0,height/2),
                        new Point(width/2,0),
                        new Point(width,height/2),
                        new Point(width/2,height)
                                          });
            break;

        Case ShapeType.Rectangle:
            outline.AddRectangle(new Rectangle(0,0,width,height));
            break;
        .....

The shape selection design-time editor is coded in such a way that it queries the ShapeType enum and calls the updateOutline function while rendering the the design-time UI. It will automatically display the shapes correctly.

Demo Sample Application

This is created to show the features of the shape control. The left panel demonstrates transparency as you drag the diamond shape around the panel. The other shape controls show the various different shapes and settings.

Possible Enhancement

One problem with any shape control is that there are sure to be some shapes that we want but are not found in the shape selection. One way of solving this problem is to create a design-time Outline editor to edit an Outline property. The Outline editor would allow the user to edit/create the GraphicsPath object and assign it to the Outline property for the shape control.

Conclusion

I hope that the readers would benefit not only from using the shape control to create fanciful UI, but also from their exploration of the various .NET classes used to create the magic of the shape control.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Yang Kok Wah
Web Developer
Singapore Singapore
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to show shapes above other controls when they are overlappedmemberkwekey18 Apr '12 - 5:35 
AnswerRe: How to show shapes above other controls when they are overlappedmemberYang Kok Wah20 Apr '12 - 0:23 
QuestionAbout License ?memberjayyang27 Dec '10 - 17:20 
AnswerRe: About License ?memberYang Kok Wah28 Dec '10 - 15:28 
QuestionShape Control integration in a GPL project?memberseeseekey8 Dec '10 - 3:42 
GeneralMemory [modified]membertcholzer24 Sep '09 - 7:24 
GeneralCorrection in my previous postmemberyogeshcprajapati@hotmail.com Yogesh18 Aug '09 - 7:05 
GeneralBadge Designermemberyogeshcprajapati@hotmail.com Yogesh18 Aug '09 - 6:59 
GeneralRe: Badge DesignermemberYang Kok Wah18 Aug '09 - 17:34 
GeneralHi Dearmembermdrizwan_119 Jul '09 - 19:10 
GeneralKeyDown event with this example. PLZ HELP, YANG!!!memberyogeshcprajapati@hotmail.com Yogesh15 May '09 - 0:57 
GeneralRe: KeyDown event with this example. PLZ HELP, YANG!!!memberYang Kok Wah26 May '09 - 3:49 
GeneralRe: KeyDown event with this example. PLZ HELP, YANG!!!memberyogeshcprajapati@hotmail.com Yogesh28 May '09 - 4:57 
GeneralKeyDown event with this example.memberyogeshcprajapati@hotmail.com Yogesh14 May '09 - 6:46 
GeneralMy modification for the bordermembergaksoftware9 Jun '08 - 15:22 
QuestionAdded new shapes can not be accessablemembermuharrem1 Apr '08 - 3:00 
AnswerRe: Added new shapes can not be accessablememberYang Kok Wah26 May '09 - 3:54 
QuestionSimply Line shapemembercarlornd14 Mar '06 - 21:00 
AnswerRe: Simply Line shapememberYang Kok Wah19 Mar '06 - 21:41 
QuestionHow to resize shape control by mouse?memberJian123456710 Jan '06 - 4:26 
AnswerRe: How to resize shape control by mouse?memberYang Kok Wah10 Jan '06 - 15:09 
GeneralconnectormemberJian12345679 Jan '06 - 4:06 
GeneralRe: connectormemberYang Kok Wah10 Jan '06 - 15:25 
Generalshapecontrol.csmembermissions4Him22 Nov '05 - 11:23 
GeneralRe: shapecontrol.csmemberYang Kok Wah4 Dec '05 - 15:05 
GeneralC Sharp Builder compatible version...memberlucapan19 Jun '05 - 13:06 
GeneralRe: C Sharp Builder compatible version...memberYang Kok Wah21 Jun '05 - 7:24 
GeneralRe: C Sharp Builder compatible version...sussJaswinder Singh Kohli2 Jul '05 - 10:24 
GeneralRe: C Sharp Builder compatible version...sussAnonymous2 Aug '05 - 21:00 
GeneralExcellentmemberDoubin1 Jun '05 - 20:35 
GeneralGreat Examplememberrudy.net1 Jun '05 - 8:49 
GeneralRe: Great ExamplememberYang Kok Wah1 Jun '05 - 14:39 
GeneralRe: Great ExamplememberSuper Lloyd19 Dec '05 - 14:12 
GeneralThank Youmemberrudy.net1 Jun '05 - 8:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 1 Jun 2005
Article Copyright 2005 by Yang Kok Wah
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid