Click here to Skip to main content
15,884,472 members
Articles / Multimedia / GDI+
Article

Creating Custom Shaped Windows Forms in .NET

Rate me:
Please Sign up or sign in to vote.
1.65/5 (18 votes)
21 Aug 20042 min read 181.4K   4.4K   40   27
Creating custom shaped Windows Forms in .NET.

Introduction

One of the niftiest aspects of Windows Forms is that you can craft them into non-rectangular shapes. Microsoft Windows Media™ Player 7 exhibits this feature, and, without doubt, many developers who see this want to incorporate it into their own applications. After much searching, I finally found the answer in MSDN.

Using the code

Following are the steps to create a simple non rectangular form:

  1. Create a bitmap with a non-rectangular shape. This will serve as your form later on, as shown in Fig. 1.

    Image 1

    Fig. 1

    Note: Choose an easy-to-remember background color, such as black, as this will be important later on.

  2. In Microsoft Visual Studio .NET, create a Windows Application Project.
  3. Set the FormBorderStyle property to None.
  4. Set the BackgroundImage property of the form to the .bmp you created above.
  5. Set the TransparencyKey property of the form to the background color of the .bmp file. In the case of the example above, you would set it to black. This will make the black portion of the form disappear.

    Note: Setting the FormBorderStyle to None disables the standard functionality provided by the title bar. Thus, you must add custom code to the project to allow the form to be moved, closed, minimized, and maximized.

Writing code to close the form

  1. From the Toolbox, drag a Button control to the form as shown in Fig 2.
  2. In the Properties window, set the Text property to "X".
  3. Double-click the Button to add a Click event handler.
  4. Enter code similar to the following to Close the form when the button is clicked.
    C#
    private void button1_Click(object sender, System.EventArgs e)
    {
      this.Close();
    }
  5. Similarly, drop another button and enter the following Code for minimizing, and enter code similar to as follows:
    C#
    private void button2_Click(object sender, System.EventArgs e)
    {
      this.WindowState=FormWindowState.Minimized;
    }

    Image 2

    Fig. 2

Writing code to move the form

  1. Create a new variable:
    C#
    public Point mouse_offset;

    This will store the mouse position when the form is clicked.

  2. Create an event handler for the form's MouseDown event.

    Image 3

    Fig. 3

  3. Enter code similar to the following to assign coordinates to the mouse_offset variable based on the current position of the mouse.
    C#
    private void Form1_MouseDown(object sender, 
            System.Windows.Forms.MouseEventArgs e)
    {
        mouse_offset = new Point(-e.X, -e.Y);
    }
  4. Similarly, create an event handler for the form's MouseMove event.
  5. Enter code similar to the following. When the left mouse button is clicked and the mouse is dragged, the form's Location property is set to the new position.
    C#
    private void Form1_MouseMove(object sender, 
                       System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left) 
        {
            Point mousePos = Control.MousePosition;
            mousePos.Offset(mouse_offset.X, mouse_offset.Y);
            Location = mousePos;
        }
    }

    Save the application. Press F5 to run it. The form will be shaped like the image you drew at the beginning. Click anywhere on the form and drag it to see the move functionality. Click the Close Form button to close the form.

    Image 4

    NOTE: Make Sure your VGA Card is set at 16 bit resolution.

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
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalthanks but resize Pin
Member 967039913-Jul-15 22:44
Member 967039913-Jul-15 22:44 
Questionthanks but resize Pin
Member 967039913-Jul-15 22:43
Member 967039913-Jul-15 22:43 
QuestionMaking things so fuzzy Pin
elgaabeb16-Nov-11 23:22
elgaabeb16-Nov-11 23:22 
GeneralMy vote of 1 Pin
Sergey Alexandrovich Kryukov12-Nov-11 8:55
mvaSergey Alexandrovich Kryukov12-Nov-11 8:55 
Voted one because this is fake non-rectangular shape, not reliable, too. It makes no sense as the settings the property Region is a simple and comprehensive way to solve this problem.

—SA

GeneralMy vote of 1 Pin
Sergey Alexandrovich Kryukov20-Sep-11 16:48
mvaSergey Alexandrovich Kryukov20-Sep-11 16:48 
Generalthe form is not transparent !!!! Pin
twentysevendesigns18-Jun-08 21:38
twentysevendesigns18-Jun-08 21:38 
Generaldoes not work on 32 bit Pin
tehila24-Jul-07 7:13
tehila24-Jul-07 7:13 
GeneralRe: does not work on 32 bit Pin
Devinmccloud2-Oct-07 16:52
Devinmccloud2-Oct-07 16:52 
GeneralRe: does not work on 32 bit Pin
projectfallback30-Nov-07 20:45
projectfallback30-Nov-07 20:45 
QuestionHow to keep the system menu Pin
joebarthib20-Jun-07 22:23
joebarthib20-Jun-07 22:23 
QuestionAlmost working Pin
goNz_BcN20-Sep-05 11:06
goNz_BcN20-Sep-05 11:06 
QuestionRezise?? Pin
elranu4-Jul-05 13:25
elranu4-Jul-05 13:25 
GeneralDoesnt work Pin
SickPuP110311-Nov-04 10:58
SickPuP110311-Nov-04 10:58 
GeneralRe: Doesnt work Pin
Anonymous18-Dec-04 7:38
Anonymous18-Dec-04 7:38 
QuestionWhat about regions ? Pin
mcarbenay22-Aug-04 22:38
mcarbenay22-Aug-04 22:38 
AnswerRe: What about regions ? Pin
nick main25-Aug-04 6:09
nick main25-Aug-04 6:09 
GeneralRe: What about regions ? Pin
Sergey Alexandrovich Kryukov12-Nov-11 8:56
mvaSergey Alexandrovich Kryukov12-Nov-11 8:56 
AnswerRe: What about regions ? Pin
balazs_hideghety7-Nov-07 2:08
balazs_hideghety7-Nov-07 2:08 
AnswerRe: What about regions ? Pin
VCSKicks18-Feb-08 12:14
VCSKicks18-Feb-08 12:14 
GeneralRe: What about regions ? Pin
Sergey Alexandrovich Kryukov12-Nov-11 8:58
mvaSergey Alexandrovich Kryukov12-Nov-11 8:58 
GeneralIf this doesn't work Pin
mav.northwind22-Aug-04 20:30
mav.northwind22-Aug-04 20:30 
GeneralRe: If this doesn't work Pin
Haroon Rehman23-Aug-04 11:10
Haroon Rehman23-Aug-04 11:10 
GeneralRe: If this doesn't work Pin
deisama9-Oct-04 11:33
deisama9-Oct-04 11:33 
GeneralRe: If this doesn't work Pin
blue_raptor8123-Dec-04 2:44
blue_raptor8123-Dec-04 2:44 
GeneralRe: If this doesn't work Pin
walterman22-Jan-07 10:29
walterman22-Jan-07 10:29 

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.