Reputation: 11
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.
Upvotes: 0
Views: 545
Reputation: 9158
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."
Upvotes: 1