Search This Blog

Thursday, March 6, 2014

Creating a shaped form in .net (creating non-rectangular forms)


To create a shaped form
  1. Create a bitmap of a non-rectangular shape of one color with a distinct background of another color. Use any paint program you like. The shape you draw will ultimately be your form, so be sure to draw it large enough to be useful.
  2. In the Properties window:
    • Set the FormBorderStyle property to None.
      This property removes the title bar from the form. (It also removes the functionality that is provided by a title bar, including the ability to close the form and move the form. However, this shortcoming is addressed below in code.)
    • Set the BackgroundImage property of the form to the bitmap file you created earlier. There is no need to add the file to the project system; this will be done automatically when you specify it as the background image.
      This property sets the bitmap image to be the background of the form. (When used in tandem with the TransparencyKey property specified below, this property defines the shape of the form.)
    • Set the TransparencyKey property to the background color of the bitmap file.
      This property tells the application which parts of the form you want to see through.

To write code to close the form
  1. Add code that will allow the user to close the form by invoking its Close method.
    The following example shows how you can add a button that, when clicked, will close the form.
' VB
Private Sub Button1_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles Button1.Click
   Me.Close()
End Sub

// C#
private void button1_Click(object sender, System.EventArgs e)
{
   this.Close();
}

No comments:

Post a Comment