Click here to Skip to main content
15,881,882 members
Articles / Multimedia / GDI+
Article

Irregular Shaped Form

Rate me:
Please Sign up or sign in to vote.
4.07/5 (19 votes)
17 Jan 20062 min read 71.6K   3.8K   42   10
How to customize your form shape and control shapes according to your needs.

Introduction

I get sick of the standard form shapes. I needed to customize my form shape according to my needs. Not only the form shape, but also any control shape. What about having a circle button or a triangle button? The implementation of all this will be described in easy steps, in this article. First I will start with the required technology.

GraphicPath

Applications use paths to draw outlines of shapes, fill the interiors of shapes, and create clipping regions. The graphics engine maintains the coordinates of geometric shapes in a path in world coordinate space. A path may be composed of any number of figures (sub paths). Each figure is either composed of a sequence of connected lines and curves, or a geometric shape primitive. The starting point of a figure is the first point in the sequence of connected lines and curves. The ending point is the last point in the sequence.

Requirements

  • Namespace: System.Drawing.Drawing2D

Region

A region is scalable because its coordinates are specified in world coordinates. On a drawing surface, however, its interior is dependent on the size and shape of the pixels representing it. An application can use regions to clamp the output of drawing operations. The window manager uses regions to define the drawing area of windows. These regions are called clipping regions. An application can also use regions in hit-testing operations, such as checking whether a point or a rectangle intersects a region. An application can fill a region by using a Brush object.

Usage

Simply start the application, right click to choose the form background. After you choose the background right, click and select "DrawPath". Start clicking on the form drawing your path. Finally, select "Redraw form" and you will have your form shaped.

The Code

Drawing path points:

C#
private void menuItem2_Click(object sender, 
                            System.EventArgs e)
{
        Start=true;
    contextMenu1.MenuItems[1].Enabled=true;
}

private void Form1_MouseDown(object sender, 
             System.Windows.Forms.MouseEventArgs e)
{
    //===================================================
    //    Select the path point
    if(e.Button==MouseButtons.Left)
    {
        g= this.CreateGraphics();
        p=new Pen(Color.Black);
        Point pont=new Point(0,0);
        if(Start==true)
        {
            g.DrawArc(p,e.X,e.Y,2,2,0,360);
            pont.X=e.X;
            pont.Y=e.Y;
            pth[PontNum++]=pont;
            for(int j=PontNum;j<1000;j++)
            {
                pth[j]=pont;
            }
        }
    }
}

Redraw Form:

C#
private void menuItem3_Click(object sender, System.EventArgs e)
{
    GraphicsPath NewPth=new GraphicsPath();
    NewPth.AddClosedCurve(pth);
    this.Region = new Region(NewPth);
    this.Invalidate();        
    Start=false;            
    contextMenu1.MenuItems[1].Enabled=false;
}

Moving the Form:

C#
private void Form1_MouseDown(object sender, 
              System.Windows.Forms.MouseEventArgs e)
{
    if(e.Button==MouseButtons.Right)
    {
        mov=true;
        this.Cursor=Cursors.SizeAll;
        x0=e.X;
        y0=e.Y;
    }
}

private void Form1_MouseUp(object sender, 
             System.Windows.Forms.MouseEventArgs e)
{
    if(e.Button==MouseButtons.Right)
    {
        mov=false;
        this.Cursor=Cursors.Default;
    }
}
        
private void Form1_MouseMove(object sender, 
             System.Windows.Forms.MouseEventArgs e)
{
    if(e.Button==MouseButtons.Right)
    {
        this.Top+=e.Y-y0;
        this.Left+=e.X-x0;
    }
}

Conclusion

I think this is a good base to customize your form shape and shape of other controls in your application. A more practical way to use it is like what we did for the button. You may use this code to get your path points, then add it in the form load of your application. Hope it could be useful. All comments about this are welcome.

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


Written By
Software Developer (Junior)
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey4-Apr-12 23:49
professionalManoj Kumar Choubey4-Apr-12 23:49 
GeneralTransparent Form and windows media player Pin
divyesh143220-Jun-08 21:06
divyesh143220-Jun-08 21:06 
General[Message Deleted] Pin
hkjghkj128-Nov-07 9:01
hkjghkj128-Nov-07 9:01 
GeneralRe: Where can i find DrawPath ??? Pin
shabonaa28-Nov-07 11:02
shabonaa28-Nov-07 11:02 
QuestionJust a quick question ... Pin
b0ksah12-Feb-07 1:02
b0ksah12-Feb-07 1:02 
AnswerRe: Just a quick question ... Pin
shabonaa13-Feb-07 21:15
shabonaa13-Feb-07 21:15 
Questiontext shape form? Pin
Jaamsadams30-Jan-06 15:20
Jaamsadams30-Jan-06 15:20 
AnswerRe: text shape form? Pin
shabonaa31-Jan-06 8:55
shabonaa31-Jan-06 8:55 
GeneralNice, but.. Pin
FZelle17-Jan-06 22:27
FZelle17-Jan-06 22:27 
It is a nice easy to understand article, but you , like many others
are producing memmoryleaks.

Please keep in mind that you allways should Dispose() all objekts that
implement IDisposable.

In your example all Pens, and Graphics objects are not destroid
and in normal sw this leads to some memmoryleaks.

A lot of people forget to dispose pens and brushes in the OnPaint
and this leads to "OutOfMemory" exceptions.


GeneralRe: Nice, but.. Pin
shabonaa18-Jan-06 9:26
shabonaa18-Jan-06 9:26 

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

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