Rectangle with rounded corners

R

Rod Gill

Hi,

VB.Net, Visual Studio 2003, Windows Application

I want to avoid the old Windows rectangle shape, but I don't need or want
complex bitmaps. What's the easiest way to create a form with rounded
corners please?

I'd would rather not use a bitmap and transparency as I've read in MSDN that
this isn't reliable on video cards with high color resolution. The other way
of using a graphics path and region looks better, but I simply can't seem to
get the AddArcs drawing method working well to create the corners.

Code to just create the arcs for the 4 corners would be magic.

many thanks,
 
H

Herfried K. Wagner [MVP]

Rod Gill said:
I want to avoid the old Windows rectangle shape, but I don't need or want
complex bitmaps. What's the easiest way to create a form with rounded
corners please?

I'd would rather not use a bitmap and transparency as I've read in MSDN
that this isn't reliable on video cards with high color resolution. The
other way of using a graphics path and region looks better, but I simply
can't seem to get the AddArcs drawing method working well to create the
corners.

\\\
Me.FormBorderStyle = FormBorderStyle.None
Me.Height = 300
Me.Width = 400
Dim p As New 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)
Me.BackColor = Color.Red
p.Dispose()
///
 
J

jaytm

Remember to add Reference to System.Drawing and add
Imports System.Drawing.Drawing2D to form

Joe Marino
 
G

gillardg

what about that
work also on controls but i have not yet tested all



Public Sub RoundCoin(ByVal radius As Integer, ByVal ctrl As Object)
Dim frmToRnd As Object = ctrl
Dim regionRects(radius * 2 + 2) As System.Drawing.Rectangle
Dim circle As New Bitmap(radius * 2, radius * 2)
Dim g As System.Drawing.Graphics =
System.Drawing.Graphics.FromImage(circle)
g.Clear(Color.White)
g.FillEllipse(Brushes.Black, 0, 0, circle.Width, circle.Height)
Dim col As Integer = 0
For row As Integer = 0 To radius - 1
For col = 0 To radius - 1
If circle.GetPixel(col, row) <>
System.Drawing.Color.FromArgb(255, 255, 255, 255) Then Exit For
Next
regionRects(row * 2) = New System.Drawing.Rectangle(col, row,
frmToRnd.Width - 2 * col, 1)
regionRects(row * 2 + 1) = New System.Drawing.Rectangle(col,
frmToRnd.Height - row - 1, frmToRnd.Width - 2 * col, 1)
Next
regionRects(radius * 2 + 2) = New System.Drawing.Rectangle(0,
radius, frmToRnd.Width, frmToRnd.Height - circle.Height)
Dim myPath As New Drawing2D.GraphicsPath
myPath.AddRectangles(regionRects)
frmToRnd.Region = New Region(myPath)

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

For Each ctl As Control In Me.Controls
Try
RoundCoin(10, ctl)
Catch ex As Exception

End Try
Next
RoundCoin(20, Me)
Button1.FlatStyle = FlatStyle.Popup
TextBox1.BorderStyle = BorderStyle.None
RichTextBox1.BorderStyle = BorderStyle.None
MonthCalendar1.FirstDayOfWeek=Day.Monday
End Sub



"Tomas Mattsson" <[email protected]> a écrit dans le message de
groupe de discussion :
(e-mail address removed)...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top