0

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.

  • You can do something like this: [How can I draw a rounded rectangle as the border for a rounded Form?](https://stackoverflow.com/a/56533229/7444103) -- As you can see, in the derived Form you only have one method: `RoundedCornerRectangle()`, which is called only in the `OnPaint` override of the Form. Hence, you can move this method to a class and also all the code in `OnPaint()`, just pass the `PaintEventArgs` object to the class that performs the drawing. -- BTW, it's built like that for this exact reason, so you can move the code somewhere else and the Form itself has nothing to do. – Jimi Jun 09 '22 at 14:59
  • You can do the same with any other derived Form, just passing the `PaintEventArgs` to the method of a class that handles the drawing. – Jimi Jun 09 '22 at 15:03

1 Answers1

1

Another approach ("cheat to win" LOL) is to use the Form.TransparencyKey property where this:

enter image description here

plus this:

Transparency key

runs as this:

Transparency key applied

Yeah, "this is what I learned."

enter image description here

IVSoftware
  • 5,732
  • 2
  • 12
  • 23
  • Maybe `Black` is a bad example though because it runs the risk of making your text transparent :) but yeah it's that whole green-screen thing. You want a Form that has a see-though hole in the middle? Just make a PNG background image that has a filled circle of the color of `Key`. Just probably not `Black`. Learned this trick at an MSDN conference in 2005 and, well, I just love it. – IVSoftware Jun 09 '22 at 15:47