Wednesday, November 28, 2012

Rounder Corner C# Windows Form

The C# Windows forms are usually rectangular and take the default Grey color background, however there are instances where we need to make our forms more stylish, in this post we shall see on how to set a Rounder Corner appearance to a C# Windows Form.

1. Create a new / open an existing C# Windows application.
2. Create/Open the form which
needs the Rounder Corner appearance.
3. Open the Properties of the form (View -> Properties Window or Press F4 key)
4. Set the
following Properties.


          MaximizeBox – false
          ManimizeBox – false
          StartPosition – CenterScreen
          FormBorderStyle - None

6.
The necessary controls to the form
7. Add the following reference.
      using System.Drawing.Drawing2D;
8. In the forms _Paint event add the following code.

        private void frmRoundedCorner_Paint(object sender, PaintEventArgs e)
        {
            GraphicsPath gp = new GraphicsPath();
            int cornerArcRadius = 50;

            Rectangle topArc = new Rectangle(0, this.Height - cornerArcRadius, cornerArcRadius, cornerArcRadius);
            Rectangle bottomArc = new Rectangle(this.Width - cornerArcRadius + 1, this.Height - cornerArcRadius, cornerArcRadius, cornerArcRadius);

            gp.AddArc(0, 0, cornerArcRadius, cornerArcRadius, 180, 90);
            gp.AddArc(this.Width - cornerArcRadius + 1, 0, cornerArcRadius, cornerArcRadius, 270, 90);
            gp.AddRectangle(new Rectangle(0, cornerArcRadius / 2, this.Width, this.Height - cornerArcRadius));
            gp.AddArc(topArc, -270, 90);
            gp.AddArc(bottomArc, 360, 90);

            this.BackColor = Color.Silver;
            Region = new Region(gp);
        }

9. Save, Build and run the application.
10. The form should appear as follows.



Search Flipkart Products:
Flipkart.com

No comments: