Results 1 to 22 of 22

Thread: [RESOLVED] How to make a form with round corners ?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Resolved [RESOLVED] How to make a form with round corners ?

    I want to make a form with round corners.

    Kind of like the shape control with Shape property = "4 - Rounded Rectangle" ?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to make a form with round corners ?

    Try this:
    VB Code:
    1. Private Declare Function CreateEllipticRgn Lib "gdi32" _
    2. (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, _
    3. ByVal Y2 As Long) As Long
    4. Private Declare Function SetWindowRgn Lib "user32" _
    5. (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Long) As Long
    6.  
    7. Private Sub MakeRound(frm As Form)
    8. Dim lngNewWnd As Long
    9. Dim lngNewRgn As Long
    10. Dim lngWidth As Long
    11. Dim lngHeight As Long
    12. ' Calculate the current width
    13. lngWidth = frm.Width / Screen.TwipsPerPixelX
    14. ' Calculate the current height
    15. lngHeight = frm.Height / Screen.TwipsPerPixelY
    16. ' Create the new region
    17. lngNewWnd = CreateEllipticRgn(0, 0, lngWidth, lngHeight)
    18. ' Apply the new region
    19. lngNewRgn = SetWindowRgn(frm.hWnd, lngNewWnd, True)
    20. End Sub
    21.  
    22. Private Sub Form_Load()
    23. 'To make a circular form
    24. MakeRound Me
    25. End Sub

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: How to make a form with round corners ?

    Yea, that's pretty close...

    But it's too big... how do I make it so it rounds only a little bit of the corners ?

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: How to make a form with round corners ?

    Nevermind, I found out how to do it.

    I changed from CreateEllipticRgn to CreateRoundRectRgn, like this:
    VB Code:
    1. lngNewWnd = CreateRoundRectRgn(0, 0, _
    2.         Me.Width / Screen.TwipsPerPixelX + 1, Me.Height / Screen.TwipsPerPixelY + 1, _
    3.         30, 30)

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to make a form with round corners ?

    Quote Originally Posted by CVMichael
    Nevermind, I found out how to do it.

    I changed from CreateEllipticRgn to CreateRoundRectRgn, like this:
    VB Code:
    1. lngNewWnd = CreateRoundRectRgn(0, 0, _
    2.         Me.Width / Screen.TwipsPerPixelX + 1, Me.Height / Screen.TwipsPerPixelY + 1, _
    3.         30, 30)
    I like it. It is very subtle.

  6. #6
    Lively Member
    Join Date
    Jun 2006
    Posts
    89

    Cool Re: [RESOLVED] How to make a form with round corners ?

    Wow, do I like this.

    I have been looking for a way to round the corners of my picturebox,
    which I am using as a command button.

    I want to do this so I can establish a background image, and draw some text on it (caption), and I also change the border color on mouseover (and back on mouseover the form).

    But the picturebox has square corners, and I want round corners.

    This code (modified a bit) does exactly what I want.

    FINALLY !!!!!

    Thanks guys

  7. #7
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: [RESOLVED] How to make a form with round corners ?

    Antithesus, Thanks would be much appreciated if you might think of posting your modified version as well

  8. #8
    Lively Member
    Join Date
    Jun 2006
    Posts
    89

    Cool Re: [RESOLVED] How to make a form with round corners ?

    OK, I'll post the whole thing.....
    but I'll create a new thread, and NOT Hijack this one.

  9. #9
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: [RESOLVED] How to make a form with round corners ?

    but I'll create a new thread, and NOT Hijack this one
    still better post it in the code bank

  10. #10
    Addicted Member sigid's Avatar
    Join Date
    May 2006
    Location
    Massachusetts, USA
    Posts
    182

    Re: [RESOLVED] How to make a form with round corners ?

    It might be worth mentioning here that this CALL -

    Private Declare Function DeleteObject _
    Lib "gdi32" (ByVal hObject As Long) As Long

    is useful for freeing up the lngNewRgn when you are finished using it...VB will not do this for you...

  11. #11
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Re: [RESOLVED] How to make a form with round corners ?

    hi Hack,
    any component or reference required ?
    quote:
    Code:
    '"Private Declare Function CreateEllipticRgn Lib "gdi32" _"
    Private Declare Function CreateRoundRectRgn Lib "gdi32" _
    (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, _
    ByVal Y2 As Long) As Long
    Private Declare Function SetWindowRgn Lib "user32" _
    (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Long) As Long
    Private Sub MakeRound(frm As Form)
    Dim lngNewWnd As Long
    Dim lngNewRgn As Long
    Dim lngWidth As Long
    Dim lngHeight As Long
    ' Calculate the current width
    lngWidth = frm.Width / Screen.TwipsPerPixelX
    ' Calculate the current height
    lngHeight = frm.Height / Screen.TwipsPerPixelY
    ' Create the new region
    'lngNewWnd = CreateEllipticRgn(0, 0, lngWidth, lngHeight)
    lngNewWnd = CreateRoundRectRgn(0, 0, _
    Me.Width / Screen.TwipsPerPixelX + 1, Me.Height / Screen.TwipsPerPixelY + 1, _
    30, 30)
    ' Apply the new region
    lngNewRgn = SetWindowRgn(frm.hWnd, lngNewWnd, True)
    End Sub
    Private Sub Form_Load()
    MakeRound Me
    End Sub
    thanks

    cesin

  12. #12
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [RESOLVED] How to make a form with round corners ?

    No...it should work as is.

  13. #13
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Re: [RESOLVED] How to make a form with round corners ?

    thanks for replay
    Code:
    '"Private Declare Function CreateEllipticRgn Lib "gdi32" _"
    Private Declare Function CreateRoundRectRgn Lib "gdi32" _
    (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, _
    ByVal Y2 As Long) As Long
    Private Declare Function SetWindowRgn Lib "user32" _
    (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Long) As Long
    Code:
    Private Sub MakeRound(frm As Form)
    Dim lngNewWnd As Long
    Dim lngNewRgn As Long
    Dim lngWidth As Long
    Dim lngHeight As Long
    ' Calculate the current width
    lngWidth = frm.Width / Screen.TwipsPerPixelX
    ' Calculate the current height
    lngHeight = frm.Height / Screen.TwipsPerPixelY
    ' Create the new region
    'lngNewWnd = CreateEllipticRgn(0, 0, lngWidth, lngHeight)
    lngNewWnd = CreateRoundRectRgn(0, 0, _
    Me.Width / Screen.TwipsPerPixelX + 1, Me.Height / Screen.TwipsPerPixelY + 1, _
    30, 30)
    ' Apply the new region
    lngNewRgn = SetWindowRgn(frm.hWnd, lngNewWnd, True)
    End Sub
    Private Sub Form_Load()
    MakeRound Me
    End Sub
    this code come with compile error
    "Wrong number of argument or invalid property assignment" on bolt text

    Regard,
    cesin

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: [RESOLVED] How to make a form with round corners ?

    The declaration you have is wrong, it should be this:
    Code:
    Public Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long

  15. #15
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Re: [RESOLVED] How to make a form with round corners ?

    Thanks cvmichael for respond

    It come with a "compile error, constant, fix-lenght string,arrays,user-defines types and declare statement not allowed as Public members of object module"

    on Public Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long

    Regards,
    cesin

  16. #16
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: [RESOLVED] How to make a form with round corners ?

    Quote Originally Posted by sigid
    It might be worth mentioning here that this CALL -

    Private Declare Function DeleteObject _
    Lib "gdi32" (ByVal hObject As Long) As Long

    is useful for freeing up the lngNewRgn when you are finished using it...VB will not do this for you...
    Normally this is a must with any GDI Objects you create, this is one of the exceptions.
    Quote Originally Posted by MSDN
    After a successful call to SetWindowRgn, the system owns the region specified by the region handle hRgn. The system does not make a copy of the region. Thus, you should not make any further function calls with this region handle. In particular, do not delete this region handle. The system deletes the region handle when it no longer needed.

  17. #17

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: [RESOLVED] How to make a form with round corners ?

    Quote Originally Posted by ksuwanto8ksd
    Thanks cvmichael for respond

    It come with a "compile error, constant, fix-lenght string,arrays,user-defines types and declare statement not allowed as Public members of object module"

    on Public Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long

    Regards,
    cesin
    So change the Public to Private then...

    I guess you have this code in a Class or a Form where it needs to be Private, I usually put Declarations in a Module, where it usually is Public...

  18. #18

  19. #19
    Addicted Member sigid's Avatar
    Join Date
    May 2006
    Location
    Massachusetts, USA
    Posts
    182

    Re: [RESOLVED] How to make a form with round corners ?

    To Milk:
    - Thanks for the link! "I was unaware of that!"

  20. #20
    Member
    Join Date
    Sep 2011
    Posts
    56

    Re: [RESOLVED] How to make a form with round corners ?

    Thanks for this. It works great!

    Is it possible to have only the two top corners of the form rounded?
    My form to be rounded is a pop up window attached to a main form. It would be cool if the bottom corners could remain sharp corners.

    Thanks

    :Ron

  21. #21
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,748

    Re: [RESOLVED] How to make a form with round corners ?

    You can use a mask picture to create regions:
    http://thescarms.com/VBasic/IrregularForms.aspx

  22. #22
    Member
    Join Date
    Sep 2011
    Posts
    56

    Re: [RESOLVED] How to make a form with round corners ?

    Quote Originally Posted by Arnoutdv View Post
    You can use a mask picture to create regions:
    http://thescarms.com/VBasic/IrregularForms.aspx
    Excellent

    Thanks

    :Ron

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width