How to build a none rectangular form using C#

Introduction

Building none rectangular form is possible within the .Net environment, thus, I will show in this article how to build none rectangular form using C# and GDI+ libraries. First let us make a little experience:

1.gif

Figure1

Open a new windows application project. A form named Form1 is added by default as the figure bellow shows.

As we see in the figure 1, the form is visible and it is rectangular as in the ordinary case. Now, press F4 in order to show the properties grid related to the given form. And select the transparency key as the figure 2 shows.

2.gif

Figure 2

Now, select system in the multi tab and then control as the figure below:

3.gif

Figure 3

Run the project now, and observe:

4.gif

Figure 4

In the figure 4 we can remark that only the form layout is visible, at the contrast, the form container is hidden. Now, if we change the form border style to none, the entire form will be hidden.

Now, we can use the GDI+ libraries especially "System. Drawing" and "System. Drawing 2D" to draw our customized form.

To do that add a paint event handler according to the form and implement it as follow:

private void Form1_Paint(object sender, PaintEventArgs e)

{

    using (Graphics myGraphic = e.Graphics)

    {

        //Set the smoothing mode to enhance the quality of the image

        myGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

        //The gradien brush needs the System.Drawing.Drawing2D

        LinearGradientBrush myBruch = new LinearGradientBrush(new Point(0,0),new Point(600,400),Color.Red,Color.Bisque);

        Rectangle myRectangle = new Rectangle(new Point(0,0),new Size(600,400));

        LinearGradientBrush myStringBruch = new LinearGradientBrush(new Point(10,10),new Point(300,200), Color.Bisque,Color. Red);

        myGraphic.FillEllipse(myBruch, myRectangle);

        //Draw the string title

        //The string content

        string sTitle = "This is the connection page";

        //The font

        Font myFont = new Font("Arial", 29);

        //The bruch

        SolidBrush StringBruch = new SolidBrush(Color.Purple);

        // Use the draw string method to add the title into the page

        myGraphic.DrawString(sTitle,myFont,StringBruch,new PointF(20,20)); 

    }
}

Then implement the load event handler as follow:

private void Form1_Load(object sender, EventArgs e)

{

    //Set the form border style to none

    this.FormBorderStyle = FormBorderStyle.None;

    //TextBoxes configurations

    TextBox UserTextBox = new TextBox();

    TextBox PassTextBox = new TextBox();

    UserTextBox.Location = new Point(285, 201);

    UserTextBox.Size = new Size(159, 20);

    PassTextBox.Location = new Point(285, 240);

    PassTextBox.Size = new Size(159, 30);

    UserTextBox.Parent = this;

    PassTextBox.Parent = this;

    //Labels configurations

    Label UserLabel = new Label();

    Label PassLabel = new Label();

    UserLabel.Location = new Point(180, 201);

    PassLabel.Location = new Point(180, 240);

    UserLabel.Text = "User name";

    PassLabel.Text = "Password";

    UserLabel.Parent = this;

    PassLabel.Parent = this;

    //Buttons configurations

    Button CancelButton = new Button();

    Button ConnectionButton = new Button();

    CancelButton.Location = new Point(290, 280);

    ConnectionButton.Location = new Point(370, 280);

    CancelButton.Text = "Cancel";

    ConnectionButton.Text = "Connection";

    CancelButton.Parent = this;

    ConnectionButton.Parent = this;

    //Add event handlers to the click events according to the buttons

    CancelButton.Click +=new EventHandler(CancelButton_Click);

    ConnectionButton.Click +=new EventHandler(ConnectionButton_Click);

}

Now, we implement the Cancel button event handler to be responsible of disposing the customized form and we implement the connection button event handler to customize the form behavior in a connection context.

private void CancelButton_Click(object sender, EventArgs e)

{

    //To dispose the customized form

    this.Dispose();

}

private void ConnectionButton_Click(object sender, EventArgs e)

{

    //TO DO: Impelement this event handler by a customized code

    MessageBox.Show("This event handler is empty from code", "This event handler is empty from code",MessageBoxButtons.OK, MessageBoxIcon.Information);

}

Finally, run the application and observe:

5.gif

Figure 5

As you see the form is none rectangular, that's it without any complications, in deed; It is possible to customize a form as we like and according to our imagination.


Similar Articles