A earlier article showed how to change the shape of a form in .net based VBs. Unfortunately VB 6.0 (Classic Versions, Pre DotNet versions) does not have native support for Region based changes in the Form. Thankfully Microsoft added some API calls in Windows 98 and above that you can use to create custom/abnormal form shapes/regions. This article will show how to create a Elliptic Form Shape. But I will include the APIs to create the basic shapes below…
- Elliptic Shaped
- Round Shaped
- Polygon Shaped
Below are the API Codes required to get this done.
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long Private Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long Private 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 Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long Private Declare Function CreatePolygonRgn Lib "gdi32" (ByVal lpPoint As Any, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Now you simply need to make a variable to recieve the handle to the newly setup region…
' 'Will hold the handle to the new region. Dim hRegion As Long ' 'Below is the code to create a basic elliptic region… ' 'Create an Elliptic Shaped Region. hRegion = CreateEllipticRgn(0, 0, 100, 200)
Now that a new region has been created, simply call the SetWindowRgn Function passing the handle to the Form you want to change the shape in the first parameter. The other parameters are for setting up the elliptic shape on how your want it to look.
' 'Change the Form to the new region shape. SetWindowRgn(frmRegion.hWnd, hRegion, True)
Thats all there is to it! Easy to use APIs makes it possible to do custom form regions using the WindowRgn based functions that are available in Windows 98 and Above. As usual I do have a example for Visual Basic 6.0 at my http://www.vbcodesource.com website. Have fun!