-1

I am currently working on a project where my requirement is to create a form which has rounded corners and also the rounded corners must be black . I have successfully created a form with rounded corners and another form with black border but When I mix the codes of both forms together , the code did not work as expected as the black border did not come properly on edges of the form . I know nothing about gdi+ in c# , So I request all respected community members to please help me correcting my code .

public partial class roundborderlessform : no_border_form
{
    [System.Runtime.InteropServices.DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
    private static extern System.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
);

    [System.Runtime.InteropServices.DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
    private static extern bool DeleteObject(System.IntPtr hObject);
   protected override void OnPaint(PaintEventArgs e)
    {
        System.IntPtr ptr = CreateRoundRectRgn(0, 0, this.Width, this.Height, 50, 50); // _BoarderRaduis can be adjusted to your needs, try 15 to start.
        this.Region = System.Drawing.Region.FromHrgn(ptr);
        
        DeleteObject(ptr);
        ControlPaint.DrawBorder(e.Graphics, ClientRectangle, Color.Black, ButtonBorderStyle.Solid);
    }

enter image description here

  • It would help if you posted the code. And I would like to make the suggestion to proceed using WPF if you like to use fancy graphics. – Fixation May 21 '22 at 11:59
  • i have posted the code – ankit goel May 21 '22 at 12:15
  • You don't need to override the `OnPaint` method to set the From's region. Set the region whenever the size changes. In the `OnSizeChanged` for example. Now, this approach won't give you smooth corners since regions don't support antialiasing. Try [this](https://stackoverflow.com/a/56533229/14171304) approach instead. – dr.null May 21 '22 at 14:44
  • `ControlPaint.DrawBorder` draws a rectangular border. You need to draw a rounded rectangle, [here](https://stackoverflow.com/a/33853557/5114784) is how you can do it by using `Graphics`. – György Kőszeg May 21 '22 at 14:48
  • [How can I draw a rounded rectangle as the border for a rounded Form?](https://stackoverflow.com/a/56533229/7444103) – Jimi May 21 '22 at 15:30

0 Answers0