C# .NET - Rounding of Windows Panel - Asked By Nitesh on 03-May-11 10:37 AM

Hi,

I have a query,i am using windows.forms.panel in one of my forms in which i have other controls.
i need to make the borders of the panel round in shape.
How can i achieve this?

Any help would be appreciated.

Regards!
Nitesh
dipa ahuja replied to Nitesh on 03-May-11 10:44 AM
Step 1 : Set BorderStyle property to None.

Step 2 : Draw a rectangle and set the pen color in paint event of the panel

private void panelpanelbordercolor1_Paint(object sender, PaintEventArgs e)
{
  e.Graphics.DrawRectangle(Pens.Red,
  e.ClipRectangle.Left,
  e.ClipRectangle.Top,
  e.ClipRectangle.Width - 1,
  e.ClipRectangle.Height -1);
  base.OnPaint(e);
}
Ravi S replied to Nitesh on 03-May-11 10:47 AM
HI

try using following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace WinForms4OpacityControl
{
    public class CircularPanel : Panel
    {
      public CircularPanel()
      {
        this.BackColor = Color.AliceBlue;  // set an image.
        this.Size = new Size(50,50);
      }
      protected override void OnPaint(PaintEventArgs e)
      {
        GraphicsPath buttonPath = new GraphicsPath();

        // Set a new rectangle to the same size as the button's
        // ClientRectangle property.
        Rectangle newRectangle = this.ClientRectangle;

        // Decrease the size of the rectangle.
        newRectangle.Inflate(-10, -10);

        // Draw the button's border.
        e.Graphics.DrawEllipse(System.Drawing.Pens.Black, newRectangle);

        // Increase the size of the rectangle to include the border.
        newRectangle.Inflate(2, 2);

        // Create a circle within the new rectangle.
        buttonPath.AddEllipse(newRectangle);

        // Set the button's Region property to the newly created
        // circle region.
        this.Region = new System.Drawing.Region(buttonPath);
        base.OnPaint(e);
      }
    }
}

==========================================================

    public partial class Form2 : Form
    {
      CircularPanel circularPanel1;
      public Form2()
      {
        InitializeComponent();
      }

      private void AddCircularPanel()
      {
        this.circularPanel1 = new WinForms4OpacityControl.CircularPanel();
        this.SuspendLayout();
        //
        // circularPanel1
        //
        this.circularPanel1.BackColor = System.Drawing.Color.AliceBlue;
        this.circularPanel1.Location = new System.Drawing.Point(129, 103);
        this.circularPanel1.Name = "circularPanel1";
        this.circularPanel1.Size = new System.Drawing.Size(50,50);
        this.circularPanel1.TabIndex = 0;
        //
        this.Controls.Add(this.circularPanel1);
        this.ResumeLayout(false);

      }      /// <summary>
      /// Required designer variable.
      /// </summary>
      private System.ComponentModel.IContainer components = null;

      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
      protected override void Dispose(bool disposing)
      {
        if (disposing && (components != null))
        {
          components.Dispose();
        }
        base.Dispose(disposing);
      }

      #region Windows Form Designer generated code

      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
        this.SuspendLayout();
        //
        // Form2
        //
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 273);
        this.Name = "Form2";
        this.Text = "Form2";
        this.Load += new System.EventHandler(this.Form2_Load);
        this.ResumeLayout(false);

      }

      #endregion

      private void Form2_Load(object sender, EventArgs e)
      {
        AddCircularPanel();
        return;
      }

    }

Sahil Kumar replied to Nitesh on 03-May-11 10:54 AM

Hi,

You need to create a custom control.

1) Add a custom control Item in your project
2) Change the inheritance to "ContainerControl"
3) Override the OnPaint() method
4) Use the Graphics object (exposed by the function parameter) to draw your own control.


by default any windows control has a rectangle region so if you want to give it another shape create shape region for what you asked for look at

http://www.codeproject.com/vb/net/AlphaGradientPanel.asp

http://www.codeproject.com/vb/net/custompanel.asp

it have what you want in vb.net but as you know you can use assembly writin in another .net language in c# or convert its code to c#

Some examples:

http://www.codeproject.com/cs/miscctrl/AnalogClockControl.asp

http://msdn.microsoft.com/en-us/library/Aa289491

Hope this helps!

Nitesh replied to Nitesh on 04-May-11 01:46 AM
Hi Dipa,

Thanks for your suggestion,i tried using your code snippet which is as follows:

private void guipnlPatientDetails_Paint(object sender, PaintEventArgs e)

{

e.Graphics.DrawRectangle(Pens.Black ,

e.ClipRectangle.Left,

e.ClipRectangle.Top,

e.ClipRectangle.Width - 1,

e.ClipRectangle.Height - 1);

base.OnPaint(e);

}



Although the corners don't get rounded.
I made the BorderStyle property  to "None"

Am i missing anything?

Regards!
Nitesh