6 Replies - 2283 Views - Last Post: 11 April 2019 - 01:48 PM Rate Topic: -----

#1 AndrewHNZ   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 06-July 16

VB.NET Rounded upper corners form

Posted 27 April 2017 - 12:14 AM

I got this code to round the corners of a form, but it makes all of them round, I want just the upper ones rounded. How can I change the angle of the curve?
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.DoubleBuffered = True
        Me.FormBorderStyle = FormBorderStyle.None
        Dim p As New Drawing2D.GraphicsPath()
        p.StartFigure()
        p.AddArc(New Rectangle(0, 0, 40, 40), 180, 90)
        p.AddLine(40, 0, Me.Width - 40, 0)
        p.AddArc(New Rectangle(Me.Width - 40, 0, 40, 40), -90, 90)
        p.AddLine(Me.Width, 40, Me.Width, Me.Height - 40)
        p.AddArc(New Rectangle(Me.Width - 40, Me.Height - 40, 40, 40), 0, 90)
        p.AddLine(Me.Width - 40, Me.Height, 40, Me.Height)
        p.AddArc(New Rectangle(0, Me.Height - 40, 40, 40), 90, 90)
        p.CloseFigure()
        Me.Region = New Region(p)
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: VB.NET Rounded upper corners form

#2 andrewsw   User is offline

  • quantum multiprover
  • member icon

Reputation: 6792
  • View blog
  • Posts: 28,049
  • Joined: 12-December 12

Re: VB.NET Rounded upper corners form

Posted 27 April 2017 - 12:40 AM

Do you understand the code? Have you experimented?
At a glance, I guess it is going around the form from corner to corner. Is it going clockwise? And which corner does it start from?
Was This Post Helpful? 0
  • +
  • -

#3 AndrewHNZ   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 06-July 16

Re: VB.NET Rounded upper corners form

Posted 27 April 2017 - 12:56 AM

I tried commenting them one by one but simply can't understand how it works, which corners starts from. Try it on your own computer, just paste the code please.
Was This Post Helpful? 0
  • +
  • -

#4 dday9   User is offline

  • D.I.C Regular

Reputation: 95
  • View blog
  • Posts: 495
  • Joined: 17-April 13

Re: VB.NET Rounded upper corners form

Posted 27 April 2017 - 06:23 AM

Here is an extension method module that I made to draw round-cornered rectangles:
Option Strict On
Option Explicit On

Imports System.Runtime.CompilerServices
Module ExtendedGraphics

    ''' <summary>
    ''' Draws a rectangle with it's corners rounded using GDI+
    ''' </summary>
    ''' <param name="g">The graphics that does the drawing</param>
    ''' <param name="pen">System.Drawing.Pen that determines the color, width, and style of the rectangle.</param>
    ''' <param name="area">System.Drawing.Rectangle that represents the rectangle to draw.</param>
    ''' <param name="radius">System.Int32 that determines the roundness of the corners.</param>
    <Extension()>
    Public Sub DrawRectangle(ByVal g As Graphics, ByVal pen As Pen, ByVal area As Rectangle, ByVal radius As Int32)
        'Clip out the corners
        g.SetClip(New Rectangle(area.Location, New Size(radius, radius)), Drawing2D.CombineMode.Exclude) 'upper-left
        g.SetClip(New Rectangle(New Point(area.Right - radius, area.Top), New Size(radius * 2, radius)), Drawing2D.CombineMode.Exclude) 'upper-right
        g.SetClip(New Rectangle(New Point(area.Left, area.Bottom - radius), New Size(radius, radius * 2)), Drawing2D.CombineMode.Exclude) 'lower-left
        g.SetClip(New Rectangle(New Point(area.Right - radius, area.Bottom - radius), New Size(radius * 2, radius * 2)), Drawing2D.CombineMode.Exclude) 'lower-right

        'Draw the initial rectangle
        g.DrawRectangle(pen, area)
        g.ResetClip()

        'Draw the corners
        g.DrawArc(pen, area.Left, area.Top, radius * 2, radius * 2, 180, 90) 'Upper-Left
        g.DrawArc(pen, area.Right - (radius * 2), area.Top, radius * 2, radius * 2, 270, 90) 'Upper-Right
        g.DrawArc(pen, area.Left, area.Bottom - (radius * 2), radius * 2, radius * 2, 90, 90) 'Lower-Left
        g.DrawArc(pen, area.Right - (radius * 2), area.Bottom - (radius * 2), radius * 2, radius * 2, 0, 90) 'Lower-Right
    End Sub

    ''' <summary>
    ''' Draws a rectangle with it's corners rounded using GDI+
    ''' </summary>
    ''' <param name="g">The graphics that does the drawing</param>
    ''' <param name="pen">System.Drawing.Pen that determines the color, width, and style of the rectangle.</param>
    ''' <param name="location">The x and y coordinates that determine the upper-left corner of the rectangle.</param>
    ''' <param name="size">The width and height that determine the size of the rectangle.</param>
    ''' <param name="radius">System.Int32 that determines the roundness of the corners.</param>
    <Extension()>
    Public Sub DrawRectangle(ByVal g As Graphics, ByVal pen As Pen, ByVal location As Point, ByVal size As Size, ByVal radius As Int32)
        'Create a rectangle
        Dim area As Rectangle = New Rectangle(location, size)

        'Clip out the corners
        g.SetClip(New Rectangle(area.Location, New Size(radius, radius)), Drawing2D.CombineMode.Exclude) 'upper-left
        g.SetClip(New Rectangle(New Point(area.Right - radius, area.Top), New Size(radius * 2, radius)), Drawing2D.CombineMode.Exclude) 'upper-right
        g.SetClip(New Rectangle(New Point(area.Left, area.Bottom - radius), New Size(radius, radius * 2)), Drawing2D.CombineMode.Exclude) 'lower-left
        g.SetClip(New Rectangle(New Point(area.Right - radius, area.Bottom - radius), New Size(radius * 2, radius * 2)), Drawing2D.CombineMode.Exclude) 'lower-right

        'Draw the initial rectangle
        g.DrawRectangle(pen, area)
        g.ResetClip()

        'Draw the corners
        g.DrawArc(pen, area.Left, area.Top, radius * 2, radius * 2, 180, 90) 'Upper-Left
        g.DrawArc(pen, area.Right - (radius * 2), area.Top, radius * 2, radius * 2, 270, 90) 'Upper-Right
        g.DrawArc(pen, area.Left, area.Bottom - (radius * 2), radius * 2, radius * 2, 90, 90) 'Lower-Left
        g.DrawArc(pen, area.Right - (radius * 2), area.Bottom - (radius * 2), radius * 2, radius * 2, 0, 90) 'Lower-Right
    End Sub

    ''' <summary>
    ''' Draws a rectangle with it's corners rounded using GDI+
    ''' </summary>
    ''' <param name="g">The graphics that does the drawing</param>
    ''' <param name="pen">System.Drawing.Pen that determines the color, width, and style of the rectangle.</param>
    ''' <param name="x">The x coordinate that determines the upper-left corner of the rectangle.</param>
    ''' <param name="y">The y coordinate that determines the upper-left corner of the rectangle.</param>
    ''' <param name="width">Width of the rectangle to draw.</param>
    ''' <param name="height">Height of the rectangle to draw.</param>
    ''' <param name="radius">System.Int32 that determines the roundness of the corners.</param>
    <Extension()>
    Public Sub DrawRectangle(ByVal g As Graphics, ByVal pen As Pen, ByVal x As Int32, ByVal y As Int32, ByVal width As Int32, ByVal height As Int32, ByVal radius As Int32)
        'Create a rectangle
        Dim area As Rectangle = New Rectangle(x, y, width, height)

        'Clip out the corners
        g.SetClip(New Rectangle(area.Location, New Size(radius, radius)), Drawing2D.CombineMode.Exclude) 'upper-left
        g.SetClip(New Rectangle(New Point(area.Right - radius, area.Top), New Size(radius * 2, radius)), Drawing2D.CombineMode.Exclude) 'upper-right
        g.SetClip(New Rectangle(New Point(area.Left, area.Bottom - radius), New Size(radius, radius * 2)), Drawing2D.CombineMode.Exclude) 'lower-left
        g.SetClip(New Rectangle(New Point(area.Right - radius, area.Bottom - radius), New Size(radius * 2, radius * 2)), Drawing2D.CombineMode.Exclude) 'lower-right

        'Draw the initial rectangle
        g.DrawRectangle(pen, area)
        g.ResetClip()

        'Draw the corners
        g.DrawArc(pen, area.Left, area.Top, radius * 2, radius * 2, 180, 90) 'Upper-Left
        g.DrawArc(pen, area.Right - (radius * 2), area.Top, radius * 2, radius * 2, 270, 90) 'Upper-Right
        g.DrawArc(pen, area.Left, area.Bottom - (radius * 2), radius * 2, radius * 2, 90, 90) 'Lower-Left
        g.DrawArc(pen, area.Right - (radius * 2), area.Bottom - (radius * 2), radius * 2, radius * 2, 0, 90) 'Lower-Right
    End Sub

    ''' <summary>
    ''' Fills a rectangle with it's corners rounded using GDI+
    ''' </summary>
    ''' <param name="g">The graphics that does the drawing</param>
    ''' <param name="brush">System.Drawing.Brush that determines the color and style of the line.</param>
    ''' <param name="area">System.Drawing.Rectangle that represents the rectangle to fill.</param>
    ''' <param name="radius">System.Int32 that determines the roundness of the corners.</param>
    <Extension()>
    Public Sub FillRectangle(ByVal g As Graphics, ByVal brush As Brush, ByVal area As Rectangle, ByVal radius As Int32)
        Dim path As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath

        'Add the corners
        path.AddArc(area.Left, area.Top, radius * 2, radius * 2, 180, 90) 'Upper-Left
        path.AddArc(area.Right - (radius * 2), area.Top, radius * 2, radius * 2, 270, 90) 'Upper-Right
        path.AddArc(area.Right - (radius * 2), area.Bottom - (radius * 2), radius * 2, radius * 2, 0, 90) 'Lower-Right
        path.AddArc(area.Left, area.Bottom - (radius * 2), radius * 2, radius * 2, 90, 90) 'Lower-Left

        'Fill the rounded rectangle
        g.FillPath(brush, path)
    End Sub

    ''' <summary>
    ''' Fills a rectangle with it's corners rounded using GDI+
    ''' </summary>
    ''' <param name="g">The graphics that does the drawing</param>
    ''' <param name="brush">System.Drawing.Pen that determines the color and style of the rectangle.</param>
    ''' <param name="location">The x and y coordinates that determine the upper-left corner of the rectangle.</param>
    ''' <param name="size">The width and height that determine the size of the rectangle.</param>
    ''' <param name="radius">System.Int32 that determines the roundness of the corners.</param>
    <Extension()>
    Public Sub FillRectangle(ByVal g As Graphics, ByVal brush As Brush, ByVal location As Point, ByVal size As Size, ByVal radius As Int32)
        Dim area As Rectangle = New Rectangle(location, size)
        Dim path As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath

        'Add the corners
        path.AddArc(area.Left, area.Top, radius * 2, radius * 2, 180, 90) 'Upper-Left
        path.AddArc(area.Right - (radius * 2), area.Top, radius * 2, radius * 2, 270, 90) 'Upper-Right
        path.AddArc(area.Right - (radius * 2), area.Bottom - (radius * 2), radius * 2, radius * 2, 0, 90) 'Lower-Right
        path.AddArc(area.Left, area.Bottom - (radius * 2), radius * 2, radius * 2, 90, 90) 'Lower-Left

        'Fill the rounded rectangle
        g.FillPath(brush, path)
    End Sub

    ''' <summary>
    ''' Fills a rectangle with it's corners rounded using GDI+
    ''' </summary>
    ''' <param name="g">The graphics that does the drawing</param>
    ''' <param name="brush">System.Drawing.Pen that determines the color and style of the rectangle.</param>
    ''' <param name="x">The x coordinate that determines the upper-left corner of the rectangle.</param>
    ''' <param name="y">The y coordinate that determines the upper-left corner of the rectangle.</param>
    ''' <param name="width">Width of the rectangle to draw.</param>
    ''' <param name="height">Height of the rectangle to draw.</param>
    ''' <param name="radius">System.Int32 that determines the roundness of the corners.</param>
    <Extension()>
    Public Sub FillRectangle(ByVal g As Graphics, ByVal brush As Brush, ByVal x As Int32, ByVal y As Int32, ByVal width As Int32, ByVal height As Int32, ByVal radius As Int32)
        Dim area As Rectangle = New Rectangle(x, y, width, height)
        Dim path As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath

        'Add the corners
        path.AddArc(area.Left, area.Top, radius * 2, radius * 2, 180, 90) 'Upper-Left
        path.AddArc(area.Right - (radius * 2), area.Top, radius * 2, radius * 2, 270, 90) 'Upper-Right
        path.AddArc(area.Right - (radius * 2), area.Bottom - (radius * 2), radius * 2, radius * 2, 0, 90) 'Lower-Right
        path.AddArc(area.Left, area.Bottom - (radius * 2), radius * 2, radius * 2, 90, 90) 'Lower-Left

        'Fill the rounded rectangle
        g.FillPath(brush, path)
    End Sub

End Module


As you can tell, I essentially clip out the corners and then add them back in. If you want, take my code, remove the lines that clip out the bottom corners and also remove the lines that draw the rounded corners.
Was This Post Helpful? 0
  • +
  • -

#5 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1536
  • View blog
  • Posts: 3,864
  • Joined: 01-February 13

Re: VB.NET Rounded upper corners form

Posted 28 April 2017 - 03:19 AM

Below is an example that will do this. With a GraphicsPath, you can first add the top left Ark, then the top right Ark, and last a line across the bottom going from the right to the left. A GraphicsPath will close the figure by adding lines between the end points of each shape to the starting point of the next shape. On the last shape, a line is added from the end point to the start point of the first shape that was added.

You should also Dispose any New GraphicsPaths that you create by calling its Dispose method when you are done using it, or use a Using/End Using statement to create and dispose it.

Here you can see how using the info above can reduce the code by removing un-needed lines of code. This should make it a little easier to understand what is happening.
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.DoubleBuffered = True
        Me.FormBorderStyle = FormBorderStyle.None

        Using p As New Drawing2D.GraphicsPath() 'create a temporary GraphicsPath

            p.AddArc(0, 0, 40, 40, 180, 90) 'add the top left ark

            p.AddArc(Me.Width - 40, 0, 40, 40, -90, 90) 'add the top right ark

            p.AddLine(Me.Width, Me.Height, 0, Me.Height) 'add a line across the bottom

            'the GraphicsPath will close the figure for you by adding the lines between the Arks and Line.

            Me.Region = New Region(p) 'set the form`s Region to a new Region created from the GraphicsPath

        End Using 'end using the temporary GraphicsPath. This makes sure the GraphicsPath is Disposed.
    End Sub



You may also want to look using the WM_NCHITTEST messages in the WinProc to control Moving and Resizing the form with the mouse. I have an example posted on msdn but, the site is having problems this morning so, if the link below wont work, try it again later or tomorrow.

Look about half way down the page, i showed 4 forms in my post with Black, Red, Green, or Blue caption areas. These are custom drawn forms made from a borderless form like you are doing.
Customize Titlebar

I also showed a real basic example of doing the same thing in this link too.
Custom Form Border Style....
Was This Post Helpful? 0
  • +
  • -

#6 ahudson609   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-April 19

Re: VB.NET Rounded upper corners form

Posted 07 April 2019 - 07:12 PM

Large Quote Removed By Moderator....

The rounded corners look a little pixelated at that radius, is there a way to reduce the radius just a bit? I have played around with the numbers but unsure which values to change.

This post has been edited by IronRazer: 11 April 2019 - 01:50 PM
Reason for edit:: Please do not quote large posts.

Was This Post Helpful? 0
  • +
  • -

#7 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1536
  • View blog
  • Posts: 3,864
  • Joined: 01-February 13

Re: VB.NET Rounded upper corners form

Posted 11 April 2019 - 01:48 PM

Everywhere you see 40 in my example code would need to be made smaller to make the arc smaller. However, this method will leave a jagged looking edge. The only way to get a smooth looking rounded edge would be to create a Layered Window but, that is a more complex way of creating a Form. A WPF window project would probably let you do this but, it is a different style of programming than VB.Net windows forms is.

If you look at the msdn documents on the GraphicsPath.AddArc Method , you can see what each parameter of it is meant for. In the example, I used the "AddArc(Int32, Int32, Int32, Int32, Single, Single)" overload of the AddArc method.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1