Related
I found a code that allows rounded corners to be placed on forms, however, this code happens within the form itself I would like to do this in a class to make it cleaner, but it turns out that when I call I can't make the form receive the values.
Here my class file:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace cornersroundedTest
{
public class ProgramGraphics : Form
{
protected override void OnPaintBackground(PaintEventArgs e)
{
Rectangle rc = new Rectangle(0, 0, this.ClientSize.Width + 1, this.ClientSize.Height + 1);
using (LinearGradientBrush brush = new LinearGradientBrush(rc, Color.LightGreen, Color.WhiteSmoke, 45F))
{
e.Graphics.FillRectangle(brush, rc);
}
}
public void SetWindowRegion()
{
System.Drawing.Drawing2D.GraphicsPath FormPath;
FormPath = new System.Drawing.Drawing2D.GraphicsPath();
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
FormPath = GetRoundedRectPath(rect, 30); // 30 represents the size of the fillet angle
this.Region = new Region(FormPath);
}
private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
{
int diameter = radius;
Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
GraphicsPath path = new GraphicsPath();
path.AddArc(arcRect, 180, 90); // top left
arcRect.X = rect.Right - diameter;//top right
path.AddArc(arcRect, 270, 90);
arcRect.Y = rect.Bottom - diameter; // buttom right
path.AddArc(arcRect, 0, 90);
arcRect.X = rect.Left; // button left
path.AddArc(arcRect, 90, 90);
path.CloseFigure();
return path;
}
private static GraphicsPath GetRoundRectangle(Rectangle rectangle, int r)
{
int l = 2 * r;
// Divide the rounded rectangle into a combination of straight lines and arcs, and add them to the path in turn
GraphicsPath gp = new GraphicsPath();
gp.AddLine(new Point(rectangle.X + r, rectangle.Y), new Point(rectangle.Right - r, rectangle.Y));
gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Y, l, l), 270F, 90F);
gp.AddLine(new Point(rectangle.Right, rectangle.Y + r), new Point(rectangle.Right, rectangle.Bottom - r));
gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Bottom - l, l, l), 0F, 90F);
gp.AddLine(new Point(rectangle.Right - r, rectangle.Bottom), new Point(rectangle.X + r, rectangle.Bottom));
gp.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - l, l, l), 90F, 90F);
gp.AddLine(new Point(rectangle.X, rectangle.Bottom - r), new Point(rectangle.X, rectangle.Y + r));
gp.AddArc(new Rectangle(rectangle.X, rectangle.Y, l, l), 180F, 90F);
return gp;
}
public void FillRoundRectangle(Graphics g, Rectangle rectangle, Pen pen, int r)
{
rectangle = new Rectangle(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
g.DrawPath(pen, GetRoundRectangle(rectangle, r));
}
private void OnpaintForm(object sender, PaintEventArgs e)
{
SetWindowRegion();
Pen pen = new Pen(Color.Blue, 3);
pen.DashStyle = DashStyle.Solid;
Rectangle rectangle = new Rectangle(1, 1, this.Width - 2, this.Height - 2);
FillRoundRectangle(e.Graphics, rectangle, pen, 14);
}
}
}
This code was separated To be called by the form file name: ProgramGraphics it was typed as type form.
Now the normal code inside the form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace cornersroundedTest
{
public partial class Form1 : Form
{
private Timer drawTimer = new Timer();
public Form1()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
this.SetStyle(ControlStyles.ResizeRedraw, true);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
Rectangle rc = new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height);
using (LinearGradientBrush brush = new LinearGradientBrush(rc, Color.LightGreen, Color.WhiteSmoke, 45F))
{
e.Graphics.FillRectangle(brush, rc);
}
}
public void SetWindowRegion()
{
System.Drawing.Drawing2D.GraphicsPath FormPath;
FormPath = new System.Drawing.Drawing2D.GraphicsPath();
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
FormPath = GetRoundedRectPath(rect, 30); // 30 represents the size of the fillet angle
this.Region = new Region(FormPath);
}
private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
{
int diameter = radius;
Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
GraphicsPath path = new GraphicsPath();
path.AddArc(arcRect, 180, 90); // top left
arcRect.X = rect.Right - diameter;//top right
path.AddArc(arcRect, 270, 90);
arcRect.Y = rect.Bottom - diameter; // buttom right
path.AddArc(arcRect, 0, 90);
arcRect.X = rect.Left; // button left
path.AddArc(arcRect, 90, 90);
path.CloseFigure();
return path;
}
private static GraphicsPath GetRoundRectangle(Rectangle rectangle, int r)
{
int l = 2 * r;
// Divide the rounded rectangle into a combination of straight lines and arcs, and add them to the path in turn
GraphicsPath gp = new GraphicsPath();
gp.AddLine(new Point(rectangle.X + r, rectangle.Y), new Point(rectangle.Right - r, rectangle.Y));
gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Y, l, l), 270F, 90F);
gp.AddLine(new Point(rectangle.Right, rectangle.Y + r), new Point(rectangle.Right, rectangle.Bottom - r));
gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Bottom - l, l, l), 0F, 90F);
gp.AddLine(new Point(rectangle.Right - r, rectangle.Bottom), new Point(rectangle.X + r, rectangle.Bottom));
gp.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - l, l, l), 90F, 90F);
gp.AddLine(new Point(rectangle.X, rectangle.Bottom - r), new Point(rectangle.X, rectangle.Y + r));
gp.AddArc(new Rectangle(rectangle.X, rectangle.Y, l, l), 180F, 90F);
return gp;
}
public void FillRoundRectangle(Graphics g, Rectangle rectangle, Pen pen, int r)
{
rectangle = new Rectangle(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
g.DrawPath(pen, GetRoundRectangle(rectangle, r));
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.Blue, 3);
pen.DashStyle = DashStyle.Solid;
Rectangle rectangle = new Rectangle(1, 1, this.Width - 2, this.Height - 2);
FillRoundRectangle(e.Graphics, rectangle, pen, 14);
}
private void Form1_Load(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Normal)
{
SetWindowRegion();
}
else
{
this.Region = null;
}
}
}
}
I would just like to separate the code above that works within a class and call the attributes of the class so that they can be applied to any form in the project. Any tips on how to do this? I've already tried to create and call methods and even use public methods and nothing, the only result is the creation within a form to a sender , and in Onpaint and even then it only generated results in the form not in its corner.
Another approach ("cheat to win" LOL) is to use the Form.TransparencyKey property where this:
plus this:
runs as this:
Yeah, "this is what I learned."
Both labels have AutoSize true & TextAlign MiddleCenter.
How can also label2 show smooth borders?
Here is the test code for handlers Form.Load(...) & Form.Paint(...):
int _cornerRadius = 10;
Point _locationLabel2;
// Form.Load(...)
private void Form3_Load(object sender, EventArgs e)
{
// Step 1: Cut the label regions (seems to be ok, result is the same for both labels)
GraphicsPath graphicsPath = _getRoundPath(label1.ClientRectangle, _cornerRadius);
label1.Region = new Region(graphicsPath);
graphicsPath = _getRoundPath(label2.ClientRectangle, _cornerRadius);
label2.Region = new Region(graphicsPath);
_locationLabel2 = this.PointToClient(label2.Parent.PointToScreen(label2.Location));
}
// Form.Paint(...)
private void Form3_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(label1.BackColor, 3.0f))
{
// Step 2: Smooth the label borders (ok only for label1)
_drawRoundedRectangle(e.Graphics, pen, label1.Location.X, label1.Location.Y,
label1.ClientRectangle.Width, label1.ClientRectangle.Height, _cornerRadius);
_drawRoundedRectangle(e.Graphics, pen, _locationLabel2.X, _locationLabel2.Y,
label2.ClientRectangle.Width, label2.ClientRectangle.Height, _cornerRadius);
}
}
// Helper 1/3
private static GraphicsPath _getRoundPath(Rectangle rectangle, int radius)
{
int x = rectangle.X;
int y = rectangle.Y;
int width = rectangle.Width;
int height = rectangle.Height;
radius = radius << 1;
GraphicsPath path = new GraphicsPath();
if (radius > 0)
{
if (radius > height) radius = height;
if (radius > width) radius = width;
path.AddArc(x, y, radius, radius, 180, 90);
path.AddArc(x + width - radius, y, radius, radius, 270, 90);
path.AddArc(x + width - radius, y + height - radius, radius, radius, 0, 90);
path.AddArc(x, y + height - radius, radius, radius, 90, 90);
path.CloseFigure();
}
else
{
path.AddRectangle(rectangle);
}
return path;
}
// Helper 2/3
private void _drawRoundedRectangle(Graphics graphics, Pen pen, int x, int y, int width, int height, int radius)
{
RectangleF rectangle = new RectangleF(x, y, width, height);
GraphicsPath path = _generateRoundedRectangle(graphics, rectangle, radius);
SmoothingMode old = graphics.SmoothingMode;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.DrawPath(pen, path);
graphics.SmoothingMode = old;
}
// Helper 3/3
private static GraphicsPath _generateRoundedRectangle(Graphics graphics, RectangleF rectangle, int radius)
{
GraphicsPath path = new GraphicsPath();
float 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;
}
Main code parts are from Arun Reginald Zaheeruddin
Solved it according to this answer by #Reza Aghaei.
Solution
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
public class RoundLabel : Label
{
[Browsable(true)]
public Color _BackColor { get; set; }
public RoundLabel()
{
this.DoubleBuffered = true;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (var graphicsPath = _getRoundRectangle(this.ClientRectangle))
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
using (var brush = new SolidBrush(_BackColor))
e.Graphics.FillPath(brush, graphicsPath);
using (var pen = new Pen(_BackColor, 1.0f))
e.Graphics.DrawPath(pen, graphicsPath);
TextRenderer.DrawText(e.Graphics, Text, this.Font, this.ClientRectangle, this.ForeColor);
}
}
private GraphicsPath _getRoundRectangle(Rectangle rectangle)
{
int cornerRadius = 15; // change this value according to your needs
int diminisher = 1;
GraphicsPath path = new GraphicsPath();
path.AddArc(rectangle.X, rectangle.Y, cornerRadius, cornerRadius, 180, 90);
path.AddArc(rectangle.X + rectangle.Width - cornerRadius - diminisher, rectangle.Y, cornerRadius, cornerRadius, 270, 90);
path.AddArc(rectangle.X + rectangle.Width - cornerRadius - diminisher, rectangle.Y + rectangle.Height - cornerRadius - diminisher, cornerRadius, cornerRadius, 0, 90);
path.AddArc(rectangle.X, rectangle.Y + rectangle.Height - cornerRadius - diminisher, cornerRadius, cornerRadius, 90, 90);
path.CloseAllFigures();
return path;
}
}
I have followed this tutorial in order to create a rounded panel. The code in the tutorial is in vb but I was able to convert it to C# so here is my code:
public class SPanel : Panel
{
Pen pen;
float penWidth = 2.0f;
int _edge = 20;
Color _borderColor = Color.White;
public int Edge
{
get
{
return _edge;
}
set
{
_edge = value;
Invalidate();
}
}
public Color BorderColor
{
get
{
return _borderColor;
}
set
{
_borderColor = value;
pen = new Pen(_borderColor, penWidth);
Invalidate();
}
}
public SPanel()
{
pen = new Pen(_borderColor, penWidth);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
ExtendedDraw(e);
//DrawBorder(e.Graphics);
}
private void ExtendedDraw(PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
GraphicsPath path = new GraphicsPath();
path.StartFigure();
path.StartFigure();
path.AddArc(GetLeftUpper(Edge), 180, 90);
path.AddLine(Edge, 0, Width - Edge, 0);
path.AddArc(GetRightUpper(Edge), 270, 90);
path.AddLine(Width, Edge, Width, Height - Edge);
path.AddArc(GetRightLower(Edge), 0, 90);
path.AddLine(Width - Edge, Height, Edge, Height);
path.AddArc(GetLeftLower(Edge), 90, 90);
path.AddLine(0, Height - Edge, 0, Edge);
path.CloseFigure();
Region = new Region(path);
}
Rectangle GetLeftUpper(int e)
{
return new Rectangle(0, 0, e, e);
}
Rectangle GetRightUpper(int e)
{
return new Rectangle(Width - e, 0, e, e);
}
Rectangle GetRightLower(int e)
{
return new Rectangle(Width - e, Height - e, e, e);
}
Rectangle GetLeftLower(int e)
{
return new Rectangle(0, Height - e, e, e);
}
void DrawSingleBorder(Graphics graphics)
{
graphics.DrawArc(pen, new Rectangle(0, 0, Edge, Edge), 180, 90);
graphics.DrawArc(pen, new Rectangle(Width - Edge -1, -1, Edge, Edge), 270, 90);
graphics.DrawArc(pen, new Rectangle(Width - Edge - 1, Height - Edge - 1, Edge, Edge), 0, 90);
graphics.DrawArc(pen, new Rectangle(0, Height - Edge - 1, Edge, Edge), 90, 90);
graphics.DrawRectangle(pen, 0.0F, 0.0F, Width - 1, Height - 1);
}
void DrawBorder(Graphics graphics)
{
DrawSingleBorder(graphics);
}
}
I did not use the border however the result is the same. Here is a ss:
I thought smoothing with anti alias would do the trick but I guess i was wrong. The question is how can I smooth the edges?
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;
}
private static GraphicsPath GenerateCapsule(
this Graphics graphics,
RectangleF baseRect)
{
float diameter;
RectangleF arc;
GraphicsPath path = new GraphicsPath();
try
{
if (baseRect.Width > baseRect.Height)
{
diameter = baseRect.Height;
SizeF sizeF = new SizeF(diameter, diameter);
arc = new RectangleF(baseRect.Location, sizeF);
path.AddArc(arc, 90, 180);
arc.X = baseRect.Right - diameter;
path.AddArc(arc, 270, 180);
}
else if (baseRect.Width < baseRect.Height)
{
diameter = baseRect.Width;
SizeF sizeF = new SizeF(diameter, diameter);
arc = new RectangleF(baseRect.Location, sizeF);
path.AddArc(arc, 180, 180);
arc.Y = baseRect.Bottom - diameter;
path.AddArc(arc, 0, 180);
}
else path.AddEllipse(baseRect);
}
catch { path.AddEllipse(baseRect); }
finally { path.CloseFigure(); }
return path;
}
/// <summary>
/// Draws a rounded rectangle specified by a pair of coordinates, a width, a height and the radius
/// for the arcs that make the rounded edges.
/// </summary>
/// <param name="brush">System.Drawing.Pen that determines the color, width and style of the rectangle.</param>
/// <param name="x">The x-coordinate of the upper-left corner of the rectangle to draw.</param>
/// <param name="y">The y-coordinate of the upper-left corner of the rectangle to draw.</param>
/// <param name="width">Width of the rectangle to draw.</param>
/// <param name="height">Height of the rectangle to draw.</param>
/// <param name="radius">The radius of the arc used for the rounded edges.</param>
public static void DrawRoundedRectangle(
this Graphics graphics,
Pen pen,
float x,
float y,
float width,
float height,
float radius)
{
RectangleF rectangle = new RectangleF(x, y, width, height);
GraphicsPath path = graphics.GenerateRoundedRectangle(rectangle, radius);
SmoothingMode old = graphics.SmoothingMode;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.DrawPath(pen, path);
graphics.SmoothingMode = old;
}
/// <summary>
/// Draws a rounded rectangle specified by a pair of coordinates, a width, a height and the radius
/// for the arcs that make the rounded edges.
/// </summary>
/// <param name="brush">System.Drawing.Pen that determines the color, width and style of the rectangle.</param>
/// <param name="x">The x-coordinate of the upper-left corner of the rectangle to draw.</param>
/// <param name="y">The y-coordinate of the upper-left corner of the rectangle to draw.</param>
/// <param name="width">Width of the rectangle to draw.</param>
/// <param name="height">Height of the rectangle to draw.</param>
/// <param name="radius">The radius of the arc used for the rounded edges.</param>
public static void DrawRoundedRectangle(
this Graphics graphics,
Pen pen,
int x,
int y,
int width,
int height,
int radius)
{
graphics.DrawRoundedRectangle(
pen,
Convert.ToSingle(x),
Convert.ToSingle(y),
Convert.ToSingle(width),
Convert.ToSingle(height),
Convert.ToSingle(radius));
}
/// <summary>
/// Fills the interior of a rounded rectangle specified by a pair of coordinates, a width, a height
/// and the radius for the arcs that make the rounded edges.
/// </summary>
/// <param name="brush">System.Drawing.Brush that determines the characteristics of the fill.</param>
/// <param name="x">The x-coordinate of the upper-left corner of the rectangle to fill.</param>
/// <param name="y">The y-coordinate of the upper-left corner of the rectangle to fill.</param>
/// <param name="width">Width of the rectangle to fill.</param>
/// <param name="height">Height of the rectangle to fill.</param>
/// <param name="radius">The radius of the arc used for the rounded edges.</param>
public static void FillRoundedRectangle(
this Graphics graphics,
Brush brush,
float x,
float y,
float width,
float height,
float radius)
{
RectangleF rectangle = new RectangleF(x, y, width, height);
GraphicsPath path = graphics.GenerateRoundedRectangle(rectangle, radius);
SmoothingMode old = graphics.SmoothingMode;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.FillPath(brush, path);
graphics.SmoothingMode = old;
}
/// <summary>
/// Fills the interior of a rounded rectangle specified by a pair of coordinates, a width, a height
/// and the radius for the arcs that make the rounded edges.
/// </summary>
/// <param name="brush">System.Drawing.Brush that determines the characteristics of the fill.</param>
/// <param name="x">The x-coordinate of the upper-left corner of the rectangle to fill.</param>
/// <param name="y">The y-coordinate of the upper-left corner of the rectangle to fill.</param>
/// <param name="width">Width of the rectangle to fill.</param>
/// <param name="height">Height of the rectangle to fill.</param>
/// <param name="radius">The radius of the arc used for the rounded edges.</param>
public static void FillRoundedRectangle(
this Graphics graphics,
Brush brush,
int x,
int y,
int width,
int height,
int radius)
{
graphics.FillRoundedRectangle(
brush,
Convert.ToSingle(x),
Convert.ToSingle(y),
Convert.ToSingle(width),
Convert.ToSingle(height),
Convert.ToSingle(radius));
}
}
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, // height of ellipse
int nHeightEllipse // width of ellipse
);
public Form1()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
}
}
}
I found solution internet and tried and success! Hope help you
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace windowsFormsApp
{
public partial class Form2 : Form
{
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn
(
int nLeftRect,
int nTopRect,
int nRightRect,
int nBottomRect,
int nWidthEllipse,
int nHeightEllipse
);
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
panel1.Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, panel1.Width,
panel1.Height, 30, 30));
}
}
}
I was also trying to do the same and finally I did this by creating Custom Control.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace your.namespace.here
{
public class CustomPanel : System.Windows.Forms.Panel
{
private System.Windows.Forms.Panel panel;
private Color borderColor = Color.MediumSlateBlue;
private Color borderFocusColor = Color.HotPink;
private int borderSize = 2;
private bool underlinedStyle = false;
private bool isFocused = false;
private int borderRadius = 0;
public Color BorderColor
{
get { return borderColor; }
set
{
borderColor = value;
this.Invalidate();
}
}
public Color BorderFocusColor
{
get { return borderFocusColor; }
set { borderFocusColor = value; }
}
public int BorderSize
{
get { return borderSize; }
set
{
if (value >= 1)
{
borderSize = value;
this.Invalidate();
}
}
}
public bool UnderlinedStyle
{
get { return underlinedStyle; }
set
{
underlinedStyle = value;
this.Invalidate();
}
}
public override Color ForeColor
{
get { return base.ForeColor; }
set
{
base.ForeColor = value;
panel.ForeColor = value;
}
}
public int BorderRadius
{
get { return borderRadius; }
set
{
if (value >= 0)
{
borderRadius = value;
this.Invalidate();//Redraw control
}
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics graph = e.Graphics;
if (borderRadius > 1)//Rounded TextBox
{
//-Fields
var rectBorderSmooth = this.ClientRectangle;
var rectBorder = Rectangle.Inflate(rectBorderSmooth, -borderSize, -borderSize);
int smoothSize = borderSize > 0 ? borderSize : 1;
using (GraphicsPath pathBorderSmooth = GetFigurePath(rectBorderSmooth, borderRadius))
using (GraphicsPath pathBorder = GetFigurePath(rectBorder, borderRadius - borderSize))
using (Pen penBorderSmooth = new Pen(this.Parent.BackColor, smoothSize))
using (Pen penBorder = new Pen(borderColor, borderSize))
{
//-Drawing
this.Region = new Region(pathBorderSmooth);//Set the rounded region of UserControl
if (borderRadius > 15) SetTextBoxRoundedRegion();//Set the rounded region of TextBox component
graph.SmoothingMode = SmoothingMode.AntiAlias;
penBorder.Alignment = System.Drawing.Drawing2D.PenAlignment.Center;
if (isFocused) penBorder.Color = borderFocusColor;
if (underlinedStyle) //Line Style
{
//Draw border smoothing
graph.DrawPath(penBorderSmooth, pathBorderSmooth);
//Draw border
graph.SmoothingMode = SmoothingMode.None;
graph.DrawLine(penBorder, 0, this.Height - 1, this.Width, this.Height - 1);
}
else //Normal Style
{
//Draw border smoothing
graph.DrawPath(penBorderSmooth, pathBorderSmooth);
//Draw border
graph.DrawPath(penBorder, pathBorder);
}
}
}
}
private void SetTextBoxRoundedRegion()
{
GraphicsPath pathTxt;
pathTxt = GetFigurePath(panel.ClientRectangle, borderSize * 2);
panel.Region = new Region(pathTxt);
pathTxt.Dispose();
}
private GraphicsPath GetFigurePath(Rectangle rect, int radius)
{
GraphicsPath path = new GraphicsPath();
float curveSize = radius * 2F;
path.StartFigure();
path.AddArc(rect.X, rect.Y, curveSize, curveSize, 180, 90);
path.AddArc(rect.Right - curveSize, rect.Y, curveSize, curveSize, 270, 90);
path.AddArc(rect.Right - curveSize, rect.Bottom - curveSize, curveSize, curveSize, 0, 90);
path.AddArc(rect.X, rect.Bottom - curveSize, curveSize, curveSize, 90, 90);
path.CloseFigure();
return path;
}
}
}
the output is like:
Note: this code is only for Custom Panel.
Mine is an alternative solution that requires no code or pre-paid modules.
Start by creating a round edged container on MS PowerPoint or MS Paint for example (Draw Rectangle with rounded corners, you can even add shadows if you want).
Take a screenshot of it or use snipping tool and save it as a .png; Then go to any online tool to make the background transparent.
Download the new image and use it on a pictureBox on your C# .NET solution and there, you have your own HD rounded background with shadow effects (Use the Anchor and Dock properties to give it the effects and positions that you want on your Form).
In your code:
private void ExtendedDraw(PaintEventArgs e)
{
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
LinearGradientBrush brush = new LinearGradientBrush(ClientRectangle, Color.White, Color.White, 90); //here you need your target rectangle
GraphicsPath path = new GraphicsPath();
path.StartFigure();
path.StartFigure();
path.AddArc(GetLeftUpper(Edge), 180, 90);
path.AddLine(Edge, 0, Width - Edge, 0);
path.AddArc(GetRightUpper(Edge), 270, 90);
path.AddLine(Width, Edge, Width, Height - Edge);
path.AddArc(GetRightLower(Edge), 0, 90);
path.AddLine(Width - Edge, Height, Edge, Height);
path.AddArc(GetLeftLower(Edge), 90, 90);
path.AddLine(0, Height - Edge, 0, Edge);
path.CloseFigure();
e.Graphics.FillPath(brush, path);
}
I created a simple cool ProgressBar control using a tutorial. However, I'm facing an issue. This is my code:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Revarz
{
class GraphicsHelper
{
public GraphicsPath Createround(int X, int Y, int Width, int Height, int CornerRadius)
{
GraphicsPath gfxPath = new GraphicsPath();
try
{
gfxPath.AddArc(X, Y, CornerRadius, CornerRadius, 180, 90);
gfxPath.AddArc(X + Width - CornerRadius, Y, CornerRadius, CornerRadius, 270, 90);
gfxPath.AddArc(X + Width - CornerRadius, Y + Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
gfxPath.AddArc(X, Y + Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
gfxPath.CloseAllFigures(); return gfxPath;
}
catch (Exception) { return null; }
}
}
public class CustomProgressbar : Control
{
public int Value { get; set; }
private int _Maximum = 100;
public int Maximum
{
get { return _Maximum; }
set { if (value > 0) { _Maximum = value; } else { throw new Exception("Maximum should be bigger than zero!"); }; }
}
public CustomProgressbar()
{
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.ResizeRedraw, true);
DoubleBuffered = true;
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(BackColor);
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
GraphicsPath barPath = new GraphicsHelper().Createround(0, 0, Width - 1, Height - 1, 3);
e.Graphics.DrawPath(new Pen(Color.FromArgb(50, Color.Black)), barPath);
e.Graphics.SetClip(barPath);
LinearGradientBrush LGB = new LinearGradientBrush(new Rectangle(0, 2, Width - 1, Height - 3), Color.FromArgb(241, 229, 201), Color.FromArgb(237, 218, 202), 90F);
e.Graphics.FillRectangle(LGB, LGB.Rectangle);
e.Graphics.ResetClip();
int DrawWidth = (int)(((double)Value / (double)_Maximum) * (double)(Width - 1));
if (DrawWidth > 1)
{
GraphicsPath FilledPart = new GraphicsHelper().Createround(0, 0, DrawWidth, Height - 1, 3);
LinearGradientBrush LGB2 = new LinearGradientBrush(new Rectangle(0, 1, DrawWidth, Height - 2), Color.FromArgb(232, 119, 9), Color.FromArgb(255, 171, 3), 90F);
e.Graphics.FillRectangle(LGB2, LGB2.Rectangle);
e.Graphics.DrawPath(new Pen(Color.FromArgb(146, 101, 11)), FilledPart);
}
base.OnPaint(e);
}
}
}
The issue is that, when I increase the value, the value doesn't acutally apply (the bar itself, doesn't increase). My firned told me that I have to invalidate the value when it's changed, but I have no idea how to do so!
I'd like some help, thanks!
You need to call the Invalidate() method to force a repaint:
int _value;
public int Value {
get { return _value;}
set {
if(_value != value) {
_value = value;
Invalidate();
}
}
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I change the border thickness of a Groupbox on a windows form in C#?
Guys
I am using groupbox' in Visual studio. But the Border in the groupbox is to small. I was wondering if there is a way to edit the border by giving it a color or maybe a thicker edge?
As u can see in the image it is very hard to see the border around it.
Can anyone help me with this ?
You can Use This:
using System.Drawing;
using System.Drawing.Drawing2D;
private void Form1_Load(object sender, EventArgs e)
{
myGroupBox myGroupBox = new myGroupBox();
myGroupBox.Text = "GroupBox1";
this.Controls.Add(myGroupBox);
}
public static GraphicsPath CreatePath(float x, float y, float width, float height,
float radius, bool RoundTopLeft, bool RoundTopRight, bool RoundBottomRight, bool RoundBottomLeft)
{
float xw = x + width;
float yh = y + height;
float xwr = xw - radius;
float yhr = yh - radius;
float xr = x + radius;
float yr = y + radius;
float r2 = radius * 2;
float xwr2 = xw - r2;
float yhr2 = yh - r2;
GraphicsPath p = new GraphicsPath();
p.StartFigure();
//Top Left Corner
if (RoundTopLeft)
{
p.AddArc(x, y, r2, r2, 180, 90);
}
else
{
p.AddLine(x, yr, x, y);
p.AddLine(x, y, xr, y);
}
//Top Edge
p.AddLine(xr, y, xwr, y);
//Top Right Corner
if (RoundTopRight)
{
p.AddArc(xwr2, y, r2, r2, 270, 90);
}
else
{
p.AddLine(xwr, y, xw, y);
p.AddLine(xw, y, xw, yr);
}
//Right Edge
p.AddLine(xw, yr, xw, yhr);
//Bottom Right Corner
if (RoundBottomRight)
{
p.AddArc(xwr2, yhr2, r2, r2, 0, 90);
}
else
{
p.AddLine(xw, yhr, xw, yh);
p.AddLine(xw, yh, xwr, yh);
}
//Bottom Edge
p.AddLine(xwr, yh, xr, yh);
//Bottom Left Corner
if (RoundBottomLeft)
{
p.AddArc(x, yhr2, r2, r2, 90, 90);
}
else
{
p.AddLine(xr, yh, x, yh);
p.AddLine(x, yh, x, yhr);
}
//Left Edge
p.AddLine(x, yhr, x, yr);
p.CloseFigure();
return p;
}
class myGroupBox : GroupBox
{
public myGroupBox()
{
base.BackColor = Color.Transparent;
}
[Browsable(false)]
public override Color BackColor
{
get
{
return base.BackColor;
}
set
{
base.BackColor = value;
}
}
private Color backColor = Color.Transparent;
public Color ActualBackColor
{
get { return this.backColor; }
set { this.backColor = value; }
}
protected override void OnPaint(PaintEventArgs e)
{
Size tSize = TextRenderer.MeasureText(this.Text, this.Font);
Rectangle borderRect = e.ClipRectangle;
borderRect.Y += tSize.Height / 2;
borderRect.Height -= tSize.Height / 2;
GraphicsPath gPath = CreatePath(0, borderRect.Y, (float)(this.Width - 1), borderRect.Height - 1, 5, true, true, true, true);
e.Graphics.FillPath(new SolidBrush(ActualBackColor), gPath);
e.Graphics.DrawPath(new Pen(Color.Red), gPath);
borderRect.X += 6;
borderRect.Y -= 7;
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), borderRect);
}
}