Click here to Skip to main content
12,511,882 members (52,765 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET
How do I smooth out non-rectangular form edges in VB.net. For example in my circular
form the edges are jagged. How do I smooth them out?


I have tried 2 methods. 1 is using the transparency key, that is, setting the form background colour with the same colour as that of the transparency key. The second method uses the regions, here is the code:

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim gpath As New Drawing2D.GraphicsPath 'gpath
gpath.AddEllipse(New Rectangle(1, 1, 137, 136))
Me.Region = New Region(gpath)
gpath.Dispose()
End Sub
Posted 21-Mar-12 22:03pm
vbGoof538
Updated 24-Mar-12 23:14pm
v3
Comments
SAKryukov 22-Mar-12 4:07am
   
Yes, this is a real problem. My 5 for the question. The solution would not be trivial. But first, how did you get non-rectangular shape? Did you use Region property? Is it System.Windows.Forms?
--SA
vbGoof 23-Mar-12 2:37am
   
I have tried 2 methods. 1 is using the transparency key, that is, setting the form background colour with the same colour as that of the transparency key. The second method uses the regions, here is the code


Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

Dim gpath As New Drawing2D.GraphicsPath

'gpath
gpath.AddEllipse(New Rectangle(1, 1, 137, 136))
Me.Region = New Region(gpath)
gpath.Dispose()


End Sub
losmac 24-Mar-12 7:55am
   
Always use "Improve question" button to upgrade it.
vbGoof 23-Mar-12 2:42am
   
I once came across the code to smooth out edges in VB 6, but dont know how to achieve the same thing in vb 2010. Here is my email address [DELETED]@yahoo.com. U can inbox me and i will send the code to you. VB migration software are expensive cant afford to buy them to change just 1 project

[edit]Never post your email address in any forum, unless you really like spam! If anyone replies to you, you will receive an email to let you know - OriginalGriff[/edit]
losmac 24-Mar-12 7:54am
   
It's not good idea to publish e-mail address, unless you really like spam.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Here's a code that can round form edges as long as the form doesn't have a border, I also made it draggable so that it is a round edged, draggable form:

Public Class Form1
 
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.FormBorderStyle = FormBorderStyle.None
        Me.Height = 300
        Me.Width = 400
        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)
        Me.BackColor = Color.Black
 
    End Sub
    Dim drag As Boolean
    Dim mousex As Integer
    Dim mousey As Integer
 
    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        drag = True 'Sets the variable drag to true.
        mousex = Windows.Forms.Cursor.Position.X - Me.Left 'Sets variable mousex
        mousey = Windows.Forms.Cursor.Position.Y - Me.Top 'Sets variable mousey
        Me.FormBorderStyle = FormBorderStyle.None
        Me.Height = 300
        Me.Width = 400
        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)
        Me.BackColor = Color.Black
    End Sub
 
    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        'If drag is set to true then move the form accordingly.
        If drag Then
            Me.Top = Windows.Forms.Cursor.Position.Y - mousey
            Me.Left = Windows.Forms.Cursor.Position.X - mousex
            Me.FormBorderStyle = FormBorderStyle.None
            Me.Height = 300
            Me.Width = 400
            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)
            Me.BackColor = Color.Black
        End If
    End Sub
 
    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        drag = False 'Sets drag to false, so the form does not move according to the code in MouseMove
        Me.FormBorderStyle = FormBorderStyle.None
        Me.Height = 300
        Me.Width = 400
        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)
        Me.BackColor = Color.Black
    End Sub
End Class

Hope this helps
  Permalink  
Comments
vbGoof 19-Apr-12 3:50am
   
Thank you Newbie but the corners are still rough.I want them to be very smooth. I have come across two codes 1 uses VB 6.0 and the other is in C#.net. I am still trying to make it work in VB.net. The code is something about alphawindow. Here is apart of the code. If you come across a solution plz let me know.
vbGoof 19-Apr-12 3:54am
   
APIHelp.Size newSize = new APIHelp.Size(bitmap.Width, bitmap.Height); // Size window to match bitmap
APIHelp.Point sourceLocation = new APIHelp.Point(0, 0);
APIHelp.Point newLocation = new APIHelp.Point(this.Left, this.Top);
APIHelp.BLENDFUNCTION blend = new APIHelp.BLENDFUNCTION();
blend.BlendOp = APIHelp.AC_SRC_OVER;
blend.BlendFlags = 0 // Always 0
blend.SourceConstantAlpha=255;// Set to 255
blend.AlphaFormat = APIHelp.AC_SRC_ALPHA;
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

APIHelp.Size newSize = new APIHelp.Size(bitmap.Width, bitmap.Height); // Size window to match bitmap
APIHelp.Point sourceLocation = new APIHelp.Point(0, 0);
APIHelp.Point newLocation = new APIHelp.Point(this.Left, this.Top);
APIHelp.BLENDFUNCTION blend = new APIHelp.BLENDFUNCTION();
blend.BlendOp = APIHelp.AC_SRC_OVER;
blend.BlendFlags = 0 // Always 0
blend.SourceConstantAlpha=255;// Set to 255
blend.AlphaFormat = APIHelp.AC_SRC_ALPHA;
  Permalink  
Comments
vbGoof 19-Apr-12 4:33am
   
I have found the solution at http://vbcity.com/forums/t/143931.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160929.1 | Last Updated 19 Apr 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100