How to Draw a Rounded Rectangle with Winforms (.Net)

How to Draw a Rounded Rectangle with WinForms (.NET)?

The graphics class in C# does not have a built-in method to draw rounded rectangles, however there are several ways that you can accomplish this affect. The links in the answer by Jay Riggs offer good suggestions on where to start, additionally I would suggest that you check out this article:

C# - Creating Rounded Rectangles Using A Graphics Path

So first, we create a GraphicsPath,
and then we call StartFigure so that
we can start adding edges to the path.
The rest of this code is for the top
left corner and the top line of the
rounded rectangle. If we are supposed
to make this corner rounded, we add an
arc - otherwise...

How to draw a rounded rectangle in c#

    public static GraphicsPath RoundedRect(Rectangle bounds, int radius)
{
int diameter = radius * 2;
Size size = new Size(diameter, diameter);
Rectangle arc = new Rectangle(bounds.Location, size);
GraphicsPath path = new GraphicsPath();

if (radius == 0)
{
path.AddRectangle(bounds);
return path;
}

// top left arc
path.AddArc(arc, 180, 90);

// top right arc
arc.X = bounds.Right - diameter;
path.AddArc(arc, 270, 90);

// bottom right arc
arc.Y = bounds.Bottom - diameter;
path.AddArc(arc, 0, 90);

// bottom left arc
arc.X = bounds.Left;
path.AddArc(arc, 90, 90);

path.CloseFigure();
return path;
}

And you can make two extension methods for the Graphics type so you can use them as the usual Draw... and Fill... shape-drawing methods.

    public static void DrawRoundedRectangle(this Graphics graphics, Pen pen, Rectangle bounds, int cornerRadius)
{
if (graphics == null)
throw new ArgumentNullException("graphics");
if (pen == null)
throw new ArgumentNullException("pen");

using (GraphicsPath path = RoundedRect(bounds, cornerRadius))
{
graphics.DrawPath(pen, path);
}
}

public static void FillRoundedRectangle(this Graphics graphics, Brush brush, Rectangle bounds, int cornerRadius)
{
if (graphics == null)
throw new ArgumentNullException("graphics");
if (brush == null)
throw new ArgumentNullException("brush");

using (GraphicsPath path = RoundedRect(bounds, cornerRadius))
{
graphics.FillPath(brush, path);
}
}

Update 2020:

Recently I made my drawing libraries publicly available (NuGet). Feel free to explore the GraphicsExtensions class for more overloads (custom corner radius for each corners), and for other goodies.

How can I draw a rounded rectangle as the border for a rounded Form?

A base implementation of what is described in the comments.

The Form frmRoundCorners provides some properties that allow to draw its rounded area with a custom BackColor, a custom BorderColor and a custom inner border color, acting as a shadow for the internal side of the Form's border.

The Form itself is implemented using a base class, baseForm, derived from Form, so the Form's properties can be set in the Form designer.

The transparency is activated setting the Form's original BackColor equal to its TrasparencyKey, making its ClientArea completely transparent, but drawable.

The Form's original border is set to FormBorderStyle.None in the base class constructor.

I didn't set a specific BackColor/TransparencyKey Color (it must be set in the Form's designer) because I think it's something one need to experiment with. I'd suggest a medium gray color. Avoid red components.

The Form can be moved, clicking on any point of its ClientArea and dragging it.

The minimum/maximum curvature of the Form and its custom Border is set to 15 and 180 degrees. It cannot be changed to a different range using the PropertyGrid.

The rounded area of the Form and its border are drawn using the GraphicsPath.AddArc() method, then applying a Matrix transformation to the Graphics object, both in the Scale and the Transform (position) components. The Size component is untouched.

This is what it looks like:

Rounded draggable borderless Form

using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

[ToolboxItem(false)]
public partial class frmRoundCorners : baseForm
{
private GraphicsPath pathRegion = new GraphicsPath(FillMode.Winding);
private GraphicsPath pathBorder;
Point pMousePosition = Point.Empty;

public frmRoundCorners()
{
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.ResizeRedraw, true);
InitializeComponent();
}

protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) {
pMousePosition = e.Location;
}
base.OnMouseDown(e);
}

protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) {
Point screenPos = PointToScreen(e.Location);
this.Location = new Point(screenPos.X - pMousePosition.X, screenPos.Y - pMousePosition.Y);
}
base.OnMouseMove(e);
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);

e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
RoundedCornerRectangle(ClientRectangle);
RectangleF rect = pathRegion.GetBounds();
float scaleX = 1 - (BorderSize / rect.Width);
float scaleY = 1 - (BorderSize / rect.Height);
using (Pen pen = new Pen(BorderColor, BorderSize))
using (Pen penBorder = new Pen(InternalBorderColor, 2))
using (var brush = new SolidBrush(FillColor))
using (var mx = new Matrix(scaleX, 0, 0, scaleY, (pen.Width / 2), (pen.Width / 2)))
{
e.Graphics.Transform = mx;
e.Graphics.FillPath(brush, pathRegion);
e.Graphics.DrawPath(penBorder, pathBorder);
e.Graphics.DrawPath(pen, pathRegion);
}
}

private void RoundedCornerRectangle(Rectangle r)
{
pathRegion = new GraphicsPath(FillMode.Alternate);
float innerCurve = CurveAngle - m_PenSizeOffset;

pathRegion.StartFigure();
pathRegion.AddArc(r.X, r.Y, CurveAngle, CurveAngle, 180, 90);
pathRegion.AddArc(r.Right - CurveAngle, r.Y, CurveAngle, CurveAngle, 270, 90);
pathRegion.AddArc(r.Right - CurveAngle, r.Bottom - CurveAngle, CurveAngle, CurveAngle, 0, 90);
pathRegion.AddArc(r.X, r.Bottom - CurveAngle, CurveAngle, CurveAngle, 90, 90);
pathRegion.CloseFigure();

pathBorder = new GraphicsPath();
pathBorder.StartFigure();
pathBorder.AddArc(r.X + m_PenSizeOffset, r.Y + m_PenSizeOffset, innerCurve, innerCurve, 180, 90);
pathBorder.AddArc(r.Right - innerCurve - m_PenSizeOffset, r.Y + m_PenSizeOffset, innerCurve, innerCurve, 270, 90);
pathBorder.AddArc(r.Right - innerCurve - m_PenSizeOffset, r.Bottom - innerCurve- m_PenSizeOffset, innerCurve, innerCurve, 0, 90);
pathBorder.AddArc(r.X + m_PenSizeOffset, r.Bottom - innerCurve - m_PenSizeOffset, innerCurve, innerCurve, 90, 90);
pathBorder.CloseFigure();
}
}

baseForm class:

public class baseForm : Form
{
private Color m_InternalBorderColor = Color.FromArgb(128, 128, 128);
private Color m_BorderColor = Color.Red;
private Color m_FillColor = Color.WhiteSmoke;
private float m_PenSize = 6f;
private float m_CurveAngle = 60.0f;
internal float m_PenSizeOffset = 3f;

public baseForm() => InitializeComponent();
private void InitializeComponent() => FormBorderStyle = FormBorderStyle.None;

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[DefaultValue(60.0f)]
public virtual float CurveAngle
{
get => m_CurveAngle;
set {
m_CurveAngle = Math.Max(Math.Min(value, 180), 15);
Invalidate();
}
}
[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[DefaultValue(6.0f)]
public virtual float BorderSize
{
get => m_PenSize;
set {
m_PenSize = value;
m_PenSizeOffset = value / 2.0f;
Invalidate();
}
}

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public virtual Color BorderColor
{
get => m_BorderColor;
set {
m_BorderColor = value;
Invalidate();
}
}

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public virtual Color FillColor
{
get => m_FillColor;
set {
m_FillColor = value;
Invalidate();
}
}

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Description("Get or Set the Color of the internal border")]
public virtual Color InternalBorderColor
{
get => m_InternalBorderColor;
set {
m_InternalBorderColor = value;
Invalidate();
}
}

[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[DefaultValue(FormBorderStyle.None)]
public new FormBorderStyle FormBorderStyle
{
get => base.FormBorderStyle;
set => base.FormBorderStyle = FormBorderStyle.None;
}
}

How to create a rounded rectangle at runtime in Windows Forms with VB.NET/C#?

This method fills a rounded rectangle on a graphics object (VB code) :

Public Sub FillRoundedRectangle(ByVal g As Drawing.Graphics, ByVal r As Rectangle, ByVal d As Integer, ByVal b As Brush)
Dim mode As Drawing2D.SmoothingMode = g.SmoothingMode
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed
g.FillPie(b, r.X, r.Y, d, d, 180, 90)
g.FillPie(b, r.X + r.Width - d, r.Y, d, d, 270, 90)
g.FillPie(b, r.X, r.Y + r.Height - d, d, d, 90, 90)
g.FillPie(b, r.X + r.Width - d, r.Y + r.Height - d, d, d, 0, 90)
g.FillRectangle(b, CInt(r.X + d / 2), r.Y, r.Width - d, CInt(d / 2))
g.FillRectangle(b, r.X, CInt(r.Y + d / 2), r.Width, CInt(r.Height - d))
g.FillRectangle(b, CInt(r.X + d / 2), CInt(r.Y + r.Height - d / 2), CInt(r.Width - d), CInt(d / 2))
g.SmoothingMode = mode
End Sub

To call this function, handle the paint event of the picturebox and pass the e.Graphics object as the first argument, and the picturebox's bounds as the second argument if you want the rectangle to fill your picture box completely.

The d parameter changes the corners' angle, I call it with a value of 30, you can try different values...

Also, here's some code to draw (instead of fill) a rounded rectangle:

Public Sub DrawRoundedRectangle(ByVal g As Drawing.Graphics, ByVal r As Rectangle, ByVal d As Integer, ByVal p As Pen)
g.DrawArc(p, r.X, r.Y, d, d, 180, 90)
g.DrawLine(p, CInt(r.X + d / 2), r.Y, CInt(r.X + r.Width - d / 2), r.Y)
g.DrawArc(p, r.X + r.Width - d, r.Y, d, d, 270, 90)
g.DrawLine(p, r.X, CInt(r.Y + d / 2), r.X, CInt(r.Y + r.Height - d / 2))
g.DrawLine(p, CInt(r.X + r.Width), CInt(r.Y + d / 2), CInt(r.X + r.Width), CInt(r.Y + r.Height - d / 2))
g.DrawLine(p, CInt(r.X + d / 2), CInt(r.Y + r.Height), CInt(r.X + r.Width - d / 2), CInt(r.Y + r.Height))
g.DrawArc(p, r.X, r.Y + r.Height - d, d, d, 90, 90)
g.DrawArc(p, r.X + r.Width - d, r.Y + r.Height - d, d, d, 0, 90)
End Sub

C# - Drawing a Rounded Rectangle on a panel

You generally should never be using CreateGraphics(). Simply remove this line:

System.Drawing.Graphics formGraphics = this.CreateGraphics();

And use e.Graphics where you would previously use formGraphics. The Paint event is basically asking you to "paint something for me, here's the graphics object to paint on".

Since you're already providing a Graphics object instance to your rounded rectangle method, no changes are required there.

C# Form with custom border and rounded edges

The Region propery simply cuts off the corners. To have a true rounded corner you will have to draw the rounded rectangles.

Drawing rounded rectangles

It might be easier to draw an image of the shape you want and put that on the transparent form. Easier to draw but cannot be resized.

Creating Smooth Rounded Corners in WinForm Applications

Once the function is not implemented using the normal WinForm function.
Therefore we must implement it using win32Api.

The code is created referring to this and this.

First, you have to draw a round rectangle.

public static GraphicsPath RoundedRect(Rectangle bounds, int radius)
{
int diameter = radius * 2;
Size size = new Size(diameter, diameter);
Rectangle arc = new Rectangle(bounds.Location, size);
GraphicsPath path = new GraphicsPath();

if (radius == 0)
{
path.AddRectangle(bounds);
return path;
}

// top left arc
path.AddArc(arc, 180, 90);

// top right arc
arc.X = bounds.Right - diameter;
path.AddArc(arc, 270, 90);

// bottom right arc
arc.Y = bounds.Bottom - diameter;
path.AddArc(arc, 0, 90);

// bottom left arc
arc.X = bounds.Left;
path.AddArc(arc, 90, 90);

path.CloseFigure();
return path;
}

public static void FillRoundedRectangle(Graphics graphics, Brush brush, Rectangle bounds, int cornerRadius)
{
if (graphics == null)
throw new ArgumentNullException("graphics");
if (brush == null)
throw new ArgumentNullException("brush");

using (GraphicsPath path = RoundedRect(bounds, cornerRadius))
{
graphics.FillPath(brush, path);
}
}

And let's add it to the drawing call.

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics graphics = e.Graphics;

Rectangle gradientRectangle = new Rectangle(0, 0, this.Width - 1, this.Height - 1);

Brush b = new LinearGradientBrush(gradientRectangle, Color.DarkSlateBlue, Color.MediumPurple, 0.0f);

graphics.SmoothingMode = SmoothingMode.HighQuality;
FillRoundedRectangle(graphics, b, gradientRectangle, 35);
}

Rounded Rectangle Form
Then we can draw the same form as the picture above.

Second, draw a form using Per Pixel Alpha Blend.

public void SetBitmap(Bitmap bitmap)
{
SetBitmap(bitmap, 255);
}

public void SetBitmap(Bitmap bitmap, byte opacity)
{
if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");

IntPtr screenDc = Win32.GetDC(IntPtr.Zero);
IntPtr memDc = Win32.CreateCompatibleDC(screenDc);
IntPtr hBitmap = IntPtr.Zero;
IntPtr oldBitmap = IntPtr.Zero;

try
{
hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
oldBitmap = Win32.SelectObject(memDc, hBitmap);

Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height);
Win32.Point pointSource = new Win32.Point(0, 0);
Win32.Point topPos = new Win32.Point(Left, Top);
Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
blend.BlendOp = Win32.AC_SRC_OVER;
blend.BlendFlags = 0;
blend.SourceConstantAlpha = opacity;
blend.AlphaFormat = Win32.AC_SRC_ALPHA;

Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA);
}
finally
{
Win32.ReleaseDC(IntPtr.Zero, screenDc);
if (hBitmap != IntPtr.Zero)
{
Win32.SelectObject(memDc, oldBitmap);
Win32.DeleteObject(hBitmap);
}

Win32.DeleteDC(memDc);
}
}

protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00080000;
return cp;
}
}

Finally, call SetBitmap when loading a form.

private void Form1_Load(object sender, EventArgs e)
{
Bitmap myBitmap = new Bitmap(this.Width, this.Height);

Graphics graphics = Graphics.FromImage(myBitmap);

Rectangle gradientRectangle = new Rectangle(0, 0, this.Width - 1, this.Height - 1);

Brush b = new LinearGradientBrush(gradientRectangle, Color.DarkSlateBlue, Color.MediumPurple, 0.0f);

graphics.SmoothingMode = SmoothingMode.HighQuality;
FillRoundedRectangle(graphics, b, gradientRectangle, 35);

SetBitmap(myBitmap);
}

When you finish the above tasks, you can finally get Smooth Round Corners in WinForm Applications.

Smooth Round Corners Form

Full code of Form

public class RoundedForm : Form
{
private Timer drawTimer = new Timer();

public NanoRoundedForm()
{
this.FormBorderStyle = FormBorderStyle.None;
}

protected override void OnLoad(EventArgs e)
{
if (!DesignMode)
{
drawTimer.Interval = 1000 / 60;
drawTimer.Tick += DrawForm;
drawTimer.Start();
}
base.OnLoad(e);
}

private void DrawForm(object pSender, EventArgs pE)
{
using (Bitmap backImage = new Bitmap(this.Width, this.Height))
{
using (Graphics graphics = Graphics.FromImage(backImage))
{
Rectangle gradientRectangle = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
using (Brush b = new LinearGradientBrush(gradientRectangle, Color.DarkSlateBlue, Color.MediumPurple, 0.0f))
{
graphics.SmoothingMode = SmoothingMode.HighQuality;

RoundedRectangle.FillRoundedRectangle(graphics, b, gradientRectangle, 35);

foreach (Control ctrl in this.Controls)
{
using (Bitmap bmp = new Bitmap(ctrl.Width, ctrl.Height))
{
Rectangle rect = new Rectangle(0, 0, ctrl.Width, ctrl.Height);
ctrl.DrawToBitmap(bmp, rect);
graphics.DrawImage(bmp, ctrl.Location);
}
}

PerPixelAlphaBlend.SetBitmap(backImage, Left, Top, Handle);
}
}
}
}

protected override void OnPaint(PaintEventArgs e)
{
if (DesignMode)
{
Graphics graphics = e.Graphics;

Rectangle gradientRectangle = new Rectangle(0, 0, this.Width - 1, this.Height - 1);

Brush b = new LinearGradientBrush(gradientRectangle, Color.DarkSlateBlue, Color.MediumPurple, 0.0f);

graphics.SmoothingMode = SmoothingMode.HighQuality;

RoundedRectangle.FillRoundedRectangle(graphics, b, gradientRectangle, 35);
}

base.OnPaint(e);
}

protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
if (!DesignMode)
{
cp.ExStyle |= 0x00080000;
}
return cp;
}
}
}

public static class RoundedRectangle
{
public static GraphicsPath RoundedRect(Rectangle bounds, int radius)
{
int diameter = radius * 2;
Size size = new Size(diameter, diameter);
Rectangle arc = new Rectangle(bounds.Location, size);
GraphicsPath path = new GraphicsPath();

if (radius == 0)
{
path.AddRectangle(bounds);
return path;
}

// top left arc
path.AddArc(arc, 180, 90);

// top right arc
arc.X = bounds.Right - diameter;
path.AddArc(arc, 270, 90);

// bottom right arc
arc.Y = bounds.Bottom - diameter;
path.AddArc(arc, 0, 90);

// bottom left arc
arc.X = bounds.Left;
path.AddArc(arc, 90, 90);

path.CloseFigure();
return path;
}

public static void FillRoundedRectangle(Graphics graphics, Brush brush, Rectangle bounds, int cornerRadius)
{
if (graphics == null)
throw new ArgumentNullException("graphics");
if (brush == null)
throw new ArgumentNullException("brush");

using (GraphicsPath path = RoundedRect(bounds, cornerRadius))
{
graphics.FillPath(brush, path);
}
}
}

internal static class PerPixelAlphaBlend
{
public static void SetBitmap(Bitmap bitmap, int left, int top, IntPtr handle)
{
SetBitmap(bitmap, 255, left, top, handle);
}

public static void SetBitmap(Bitmap bitmap, byte opacity, int left, int top, IntPtr handle)
{
if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");

IntPtr screenDc = Win32.GetDC(IntPtr.Zero);
IntPtr memDc = Win32.CreateCompatibleDC(screenDc);
IntPtr hBitmap = IntPtr.Zero;
IntPtr oldBitmap = IntPtr.Zero;

try
{
hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
oldBitmap = Win32.SelectObject(memDc, hBitmap);

Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height);
Win32.Point pointSource = new Win32.Point(0, 0);
Win32.Point topPos = new Win32.Point(left, top);
Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
blend.BlendOp = Win32.AC_SRC_OVER;
blend.BlendFlags = 0;
blend.SourceConstantAlpha = opacity;
blend.AlphaFormat = Win32.AC_SRC_ALPHA;

Win32.UpdateLayeredWindow(handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA);
}
finally
{
Win32.ReleaseDC(IntPtr.Zero, screenDc);
if (hBitmap != IntPtr.Zero)
{
Win32.SelectObject(memDc, oldBitmap);
Win32.DeleteObject(hBitmap);
}

Win32.DeleteDC(memDc);
}
}
}

internal class Win32
{
public enum Bool
{
False = 0,
True
};

[StructLayout(LayoutKind.Sequential)]
public struct Point
{
public Int32 x;
public Int32 y;

public Point(Int32 x, Int32 y) { this.x = x; this.y = y; }
}

[StructLayout(LayoutKind.Sequential)]
public struct Size
{
public Int32 cx;
public Int32 cy;

public Size(Int32 cx, Int32 cy) { this.cx = cx; this.cy = cy; }
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct ARGB
{
public byte Blue;
public byte Green;
public byte Red;
public byte Alpha;
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BLENDFUNCTION
{
public byte BlendOp;
public byte BlendFlags;
public byte SourceConstantAlpha;
public byte AlphaFormat;
}

public const Int32 ULW_COLORKEY = 0x00000001;
public const Int32 ULW_ALPHA = 0x00000002;
public const Int32 ULW_OPAQUE = 0x00000004;

public const byte AC_SRC_OVER = 0x00;
public const byte AC_SRC_ALPHA = 0x01;

[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
public static extern Bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);

[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
public static extern IntPtr GetDC(IntPtr hWnd);

[DllImport("user32.dll", ExactSpelling = true)]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);

[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern Bool DeleteDC(IntPtr hdc);

[DllImport("gdi32.dll", ExactSpelling = true)]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);

[DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
public static extern Bool DeleteObject(IntPtr hObject);
}

Rounded Corners in C# windows forms

try this:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn
(
int nLeftRect, // x-coordinate of upper-left corner
int nTopRect, // y-coordinate of upper-left corner
int nRightRect, // x-coordinate of lower-right corner
int nBottomRect, // y-coordinate of lower-right corner
int nWidthEllipse, // width of ellipse
int nHeightEllipse // height of ellipse
);

public Form1()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
}
}
}

from here: Form with Rounded Borders in C#?

Winforms: Smooth the rounded edges for panel

I was able to solve this by following this link. I just downloaded the sample project and created a new panel. Copied what he had on Form's onpaint to new panel's onpaint and now I have smooth edges.

public class SPanel : Panel
{
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.FillRoundedRectangle(new SolidBrush(Color.White), 10, 10, this.Width - 40, this.Height - 60, 10);
SolidBrush brush = new SolidBrush(
Color.White
);
g.FillRoundedRectangle(brush, 12, 12, this.Width - 44, this.Height - 64, 10);
g.DrawRoundedRectangle(new Pen(ControlPaint.Light(Color.White, 0.00f)), 12, 12, this.Width - 44, this.Height - 64, 10);
g.FillRoundedRectangle(new SolidBrush(Color.White), 12, 12 + ((this.Height - 64) / 2), this.Width - 44, (this.Height - 64)/2, 10);
}
}

Here is his GraphicsExtension class if link ever get broken.

static class GraphicsExtension
{
private static GraphicsPath GenerateRoundedRectangle(
this Graphics graphics,
RectangleF rectangle,
float radius)
{
float diameter;
GraphicsPath path = new GraphicsPath();
if (radius <= 0.0F)
{
path.AddRectangle(rectangle);
path.CloseFigure();
return path;
}
else
{
if (radius >= (Math.Min(rectangle.Width, rectangle.Height)) / 2.0)
return graphics.GenerateCapsule(rectangle);
diameter = radius * 2.0F;
SizeF sizeF = new SizeF(diameter, diameter);
RectangleF arc = new RectangleF(rectangle.Location, sizeF);
path.AddArc(arc, 180, 90);
arc.X = rectangle.Right - diameter;
path.AddArc(arc, 270, 90);
arc.Y = rectangle.Bottom - diameter;
path.AddArc(arc, 0, 90);
arc.X = rectangle.Left;
path.AddArc(arc, 90, 90);
path.CloseFigure();
}
return path;
}


Related Topics



Leave a reply



Submit