I have the code below to round off the edges of my form.
I have several forms and this code is present on each of them
Is it possible to put this code in a class and call it in each of the forms to simplify?
private int borderRadius = 30;
private int borderSize = 4;
private Color borderColor = Color.FromArgb(240, 185, 11);
[DllImport("user32.DLL", EntryPoint = "ReleaseCapture")]
private extern static void ReleaseCapture();
[DllImport("user32.DLL", EntryPoint = "SendMessage")]
private extern static void SendMessage(System.IntPtr hWnd, int wMsg, int wParam, int lParam);
private void FMain_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, 0x112, 0xf012, 0);
}
private GraphicsPath GetRoundedPath(Rectangle rect, float 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;
}
private void FormRegionAndBorder(Form form, float radius, Graphics graph, Color borderColor, float borderSize)
{
if (this.WindowState != FormWindowState.Minimized)
{
using (GraphicsPath roundPath = GetRoundedPath(form.ClientRectangle, radius))
using (Pen penBorder = new Pen(borderColor, borderSize))
using (Matrix transform = new Matrix())
{
graph.SmoothingMode = SmoothingMode.AntiAlias;
form.Region = new Region(roundPath);
if (borderSize >= 1)
{
Rectangle rect = form.ClientRectangle;
float scaleX = 1.0F - ((borderSize + 1) / rect.Width);
float scaleY = 1.0F - ((borderSize + 1) / rect.Height);
transform.Scale(scaleX, scaleY);
transform.Translate(borderSize / 1.6F, borderSize / 1.6F);
graph.Transform = transform;
graph.DrawPath(penBorder, roundPath);
}
}
}
}
private void FMain_Paint(object sender, PaintEventArgs e)
{
FormRegionAndBorder(this, borderRadius, e.Graphics, borderColor, borderSize);
}
What I have tried:
I tried this but I get several errors.. Am I on the right track?
using Bybit.Net.Enums;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using static MetroFramework.Drawing.MetroPaint;
namespace BasicProject
{
public class Rounded
{
private int borderRadius = 30;
private int borderSize = 4;
private Color borderColor = Color.FromArgb(240, 185, 11);
}
[DllImport("user32.DLL", EntryPoint = "ReleaseCapture")]
private extern static void ReleaseCapture();
[DllImport("user32.DLL", EntryPoint = "SendMessage")]
private extern static void SendMessage(System.IntPtr hWnd, int wMsg, int wParam, int lParam);
private void FMain_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, 0x112, 0xf012, 0);
}
private GraphicsPath GetRoundedPath(Rectangle rect, float radius)
{
GraphicsPath path = new GraphicsPath();
float curveSize = radius * 3F;
path.AddArc(rect.X, rect.Y, curveSize, curveSize, 180, 80);
path.AddArc(rect.Right - curveSize, rect.Y, curveSize, curveSize, 270, 80);
path.AddArc(rect.Right - curveSize, rect.Bottom - curveSize, curveSize, curveSize, 0, 80);
path.AddArc(rect.X, rect.Bottom - curveSize, curveSize, curveSize, 80, 80);
return path;
}
private void FormRegionAndBorder(Form form, float radius, Graphics graph, Color borderColor, float borderSize)
{
if (this.WindowState != FormWindowState.Minimized)
{
using (GraphicsPath roundPath = GetRoundedPath(form.ClientRectangle, radius))
using (Pen penBorder = new Pen(borderColor, borderSize))
using (Matrix transform = new Matrix())
{
graph.SmoothingMode = SmoothingMode.AntiAlias;
form.Region = new Region(roundPath);
if (borderSize >= 2)
{
Rectangle rect = form.ClientRectangle;
float scaleX = 1.0F - ((borderSize + 2) / rect.Width);
float scaleY = 1.0F - ((borderSize + 2) / rect.Height);
transform.Translate(borderSize / 1.6F, borderSize / 1.6F);
graph.DrawPath(penBorder, roundPath);
}
}
}
}
private void FMain_Paint(object sender, PaintEventArgs e)
{
FormRegionAndBorder(this, borderRadius, e.Graphics, borderColor, borderSize);
}