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

Shape Control for .NET

, 28 Apr 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Implementing shape control that supports transparency and custom design-time editors.


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.


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.


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.


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();


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{

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:
        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)
        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)

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

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.

ShapeImage Property

I have added a ShapeImage property that allows you to specify an image that you would like to use for generating the outline for the Shape control. A suitable image would be one with clear outline, like those included in the project's resources. The above picture shows the setting of the ShapeImage property of a Shape control to take the shape of a butterfly. Note that the ShapeImage property will override the Shape property. If you want to use the Shape property, first set ShapeImage property to (none).

The outline extraction implementation for the ShapeImage property is based on the my article

Fun with Outlines .

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.


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.


20 April 2014 : Updated Shape Type (Property) Editor to work correctly in Visual Studio 2008 and later

29 April 2014 : Add in ShapeImage property.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Yang Kok Wah
Software Developer (Senior)
Singapore Singapore
Yang Kok Wah is a Software Specialist in a leading System/Solution Integration Company in Singapore. He has more than 20 years experience in software development, specializing in areas of Biometrics, Smartcards and Image Processing. He has worked with VB, C#, Java and C/C++. He graduated from University of London with BSc(Hons) in Computing and also holds a Business Administration degree from National University of Singapore.
He likes Graphics, Games, AI and Image Processing.

Comments and Discussions

Generalgreat article. thank You! Pinmembertigerwood200623-Oct-14 16:10 
GeneralMy vote of 5 PinprofessionalVolynsky Alex20-Apr-14 22:32 
QuestionThanks for this nice control Pinmembermat7618-Feb-14 18:06 
QuestionHow to show shapes above other controls when they are overlapped Pinmemberkwekey18-Apr-12 6:35 
AnswerRe: How to show shapes above other controls when they are overlapped PinmemberYang Kok Wah20-Apr-12 1:23 
QuestionAbout License ? Pinmemberjayyang27-Dec-10 18:20 
AnswerRe: About License ? PinmemberYang Kok Wah28-Dec-10 16:28 
QuestionShape Control integration in a GPL project? Pinmemberseeseekey8-Dec-10 4:42 
GeneralMemory [modified] Pinmembertcholzer24-Sep-09 8:24 
GeneralCorrection in my previous post Yogesh18-Aug-09 8:05 
GeneralBadge Designer Yogesh18-Aug-09 7:59 
GeneralRe: Badge Designer PinmemberYang Kok Wah18-Aug-09 18:34 
GeneralHi Dear Pinmembermdrizwan_119-Jul-09 20:10 
GeneralRe: Hi Dear Pinmembersonu kumar29-Apr-14 4:05 
GeneralKeyDown event with this example. PLZ HELP, YANG!!! Yogesh15-May-09 1:57 
GeneralRe: KeyDown event with this example. PLZ HELP, YANG!!! PinmemberYang Kok Wah26-May-09 4:49 
GeneralRe: KeyDown event with this example. PLZ HELP, YANG!!! Yogesh28-May-09 5:57 
GeneralKeyDown event with this example. Yogesh14-May-09 7:46 
GeneralMy modification for the border Pinmembergaksoftware9-Jun-08 16:22 
QuestionAdded new shapes can not be accessable Pinmembermuharrem1-Apr-08 4:00 
AnswerRe: Added new shapes can not be accessable PinmemberYang Kok Wah26-May-09 4:54 
QuestionSimply Line shape Pinmembercarlornd14-Mar-06 22:00 
AnswerRe: Simply Line shape PinmemberYang Kok Wah19-Mar-06 22:41 
QuestionHow to resize shape control by mouse? PinmemberJian123456710-Jan-06 5:26 
AnswerRe: How to resize shape control by mouse? PinmemberYang Kok Wah10-Jan-06 16:09 
At design time, there would be a box surrounding the shape control when the control has the focus. When the box is resized by draging any of its handles ( using the mouse) , the shape control will be resized to fit into the box.
At runtime, you can change the size of the control by changing the width and height of the control.
Yang Kok Wah
Generalconnector PinmemberJian12345679-Jan-06 5:06 
GeneralRe: connector PinmemberYang Kok Wah10-Jan-06 16:25 
Generalshapecontrol.cs Pinmembermissions4Him22-Nov-05 12:23 
GeneralRe: shapecontrol.cs PinmemberYang Kok Wah4-Dec-05 16:05 
GeneralC Sharp Builder compatible version... Pinmemberlucapan19-Jun-05 14:06 
GeneralRe: C Sharp Builder compatible version... PinmemberYang Kok Wah21-Jun-05 8:24 
GeneralRe: C Sharp Builder compatible version... PinsussJaswinder Singh Kohli2-Jul-05 11:24 
GeneralRe: C Sharp Builder compatible version... PinsussAnonymous2-Aug-05 22:00 
GeneralExcellent PinmemberDoubin1-Jun-05 21:35 
GeneralGreat Example Pinmemberrudy.net1-Jun-05 9:49 
GeneralRe: Great Example PinmemberYang Kok Wah1-Jun-05 15:39 
GeneralRe: Great Example PinmemberSuper Lloyd19-Dec-05 15:12 
GeneralThank You Pinmemberrudy.net1-Jun-05 9:48 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150326.1 | Last Updated 29 Apr 2014
Article Copyright 2005 by Yang Kok Wah
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid