Library code snippets

Create a custom shaped form

Occasionally, you will notice a non-standard form that is round, or is not a simple rectangle. You can do this in VB by specifying the area of a form you want displayed, enabling you to create a round form, or a form with a hole in the middle... basically whatever you want.

To do this, you create 'regions' using CreateRectRgn, CreatePolygonRgn etc... then you add them to a variable using CombineRgn. Next, you tell windows to apply this region to your form using SetWindowRgn, thus creating your custom shaped form. Take a look at the code below, and try it out yourself!

Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject 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 CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode 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 Const RGN_OR = 2
Private Sub pCreateSkin()
    Dim lReturn   As Long
    Dim lRgnTmp   As Long
    Dim lSkinRgn  As Long
    Dim lWidth As Long
    Dim lHeight As Long
    lWidth = (ScaleWidth) / Screen.TwipsPerPixelX
    lHeight = (ScaleHeight) / Screen.TwipsPerPixelY
    lSkinRgn = CreateRectRgn(0, 0, 10, lHeight)
    lRgnTmp = CreateRectRgn(0, 0, lWidth, 25)
    lReturn = CombineRgn(lSkinRgn, lSkinRgn, lRgnTmp, RGN_OR)
    lRgnTmp = CreateRectRgn(lWidth, 0, lWidth - 10, lHeight)
    lReturn = CombineRgn(lSkinRgn, lSkinRgn, lRgnTmp, RGN_OR)
    lRgnTmp = CreateRectRgn(0, lHeight, lWidth - 10, lHeight - 10)
    lReturn = CombineRgn(lSkinRgn, lSkinRgn, lRgnTmp, RGN_OR)
    Call DeleteObject(lRgnTmp)
    Call SetWindowRgn(hwnd, lSkinRgn, True)
End Sub

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Create a custom shaped form.

Leave a comment

Sign in or Join us (it's free).