Click here to Skip to main content
Licence 
First Posted 21 Aug 2004
Views 97,875
Bookmarked 38 times

Creating Custom Shaped Windows Forms in .NET

By | 21 Aug 2004 | Article
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.

    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.
    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:
    private void button2_Click(object sender, System.EventArgs e)
    {
      this.WindowState=FormWindowState.Minimized;
    }

    Fig. 2

Writing code to move the form

  1. Create a new variable:
    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.

    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.
    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.
    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.

    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

About the Author

Haroon Rehman



Pakistan Pakistan

Member



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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionMaking things so fuzzy Pinmemberelgaabeb23:22 16 Nov '11  
GeneralMy vote of 1 PinmemberSAKryukov8:55 12 Nov '11  
GeneralMy vote of 1 PinmemberSAKryukov16:48 20 Sep '11  
Generalthe form is not transparent !!!! Pinmembertwentysevendesigns21:38 18 Jun '08  
Generaldoes not work on 32 bit Pinmembertehila7:13 24 Jul '07  
GeneralRe: does not work on 32 bit PinmemberDevinmccloud16:52 2 Oct '07  
GeneralRe: does not work on 32 bit Pinmemberprojectfallback20:45 30 Nov '07  
QuestionHow to keep the system menu Pinmemberjoebarthib22:23 20 Jun '07  
QuestionAlmost working PinmembergoNz_BcN11:06 20 Sep '05  
QuestionRezise?? Pinmemberelranu13:25 4 Jul '05  
GeneralDoesnt work PinmemberSickPuP110310:58 11 Nov '04  
GeneralRe: Doesnt work PinsussAnonymous7:38 18 Dec '04  
QuestionWhat about regions ? Pinmembermcarbenay22:38 22 Aug '04  
AnswerRe: What about regions ? Pinmembernick main6:09 25 Aug '04  
GeneralRe: What about regions ? PinmemberSAKryukov8:56 12 Nov '11  
AnswerRe: What about regions ? Pinmemberbalazs_hideghety2:08 7 Nov '07  
AnswerRe: What about regions ? PinmemberVCKicks12:14 18 Feb '08  
GeneralRe: What about regions ? PinmemberSAKryukov8:58 12 Nov '11  
GeneralIf this doesn't work Pinmembermav.northwind20:30 22 Aug '04  
GeneralRe: If this doesn't work PinmemberHaroon Rehman11:10 23 Aug '04  
GeneralRe: If this doesn't work Pinmemberdeisama11:33 9 Oct '04  
GeneralRe: If this doesn't work Pinmemberblue_raptor812:44 23 Dec '04  
GeneralRe: If this doesn't work Pinmemberwalterman10:29 22 Jan '07  
GeneralDownload files missing PinmemberASerfes14:41 22 Aug '04  
GeneralRe: Download files missing PinmemberHaroon Rehman10:56 23 Aug '04  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 22 Aug 2004
Article Copyright 2004 by Haroon Rehman
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid