We help IT Professionals succeed at work.

How To Create Custom Shaped Windows with VB6

guest321 asked
Medium Priority
Last Modified: 2008-02-01
I've seen many desktop application that have these fancy interfaces with animations/interactions on them.
I'd like to know if I want to create a form with any kind of shape (according to an irregular background picture), how can it be done? I've seen API calls to create windows with simple shapes such as circles, but I can't understand how to create a form with irregular shapes.
Also, how can I create customized controls, like buttons have a roundish look with special effect when mouse over, and customized control box buttons (minimize, restore and close).
Thank you very much!
Watch Question

Lee W, MVPTechnology and Business Process Advisor
Most Valuable Expert 2013

I have know knowledge with this, but I SUSPECT you would create a form that is transparent and put a picture on it.

Other than that "maybe how it's done" comment, I'm here to see how too.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

'Module Code
Option Explicit

Public Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long) As Long
Public Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Public Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Public Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function ReleaseCapture Lib "user32" () As Long
Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Public Const RGN_OR = 2
Public Const HTCAPTION = 2

Public Function MakeRegion(picSkin As PictureBox) As Long
    ' Make a windows "region" based on a given picture box'
    ' picture. This done by passing on the picture line-
    ' by-line and for each sequence of non-transparent
    ' pixels a region is created that is added to the
    ' complete region. I tried to optimize it so it's
    ' fairly fast, but some more optimizations can
    ' always be done - mainly storing the transparency
    ' data in advance, since what takes the most time is
    ' the GetPixel calls, not Create/CombineRgn
    Dim X As Long, Y As Long, StartLineX As Long
    Dim FullRegion As Long, LineRegion As Long
    Dim TransparentColor As Long
    Dim InFirstRegion As Boolean
    Dim InLine As Boolean  ' Flags whether we are in a non-tranparent pixel sequence
    Dim hDC As Long
    Dim PicWidth As Long
    Dim PicHeight As Long
    hDC = picSkin.hDC
    PicWidth = picSkin.ScaleWidth
    PicHeight = picSkin.ScaleHeight
    InFirstRegion = True: InLine = False
    X = Y = StartLineX = 0
    ' The transparent color is always the color of the
    ' top-left pixel in the picture. If you wish to
    ' bypass this constraint, you can set the tansparent
    ' color to be a fixed color (such as pink), or
    ' user-configurable
    TransparentColor = GetPixel(hDC, 0, 0)
    For Y = 0 To PicHeight - 1
        For X = 0 To PicWidth - 1
            If GetPixel(hDC, X, Y) = TransparentColor Or X = PicWidth Then
                ' We reached a transparent pixel
                If InLine Then
                    InLine = False
                    LineRegion = CreateRectRgn(StartLineX, Y, X, Y + 1)
                    If InFirstRegion Then
                        FullRegion = LineRegion
                        InFirstRegion = False
                        CombineRgn FullRegion, FullRegion, LineRegion, RGN_OR
                        ' Always clean up your mess
                        DeleteObject LineRegion
                    End If
                End If
                ' We reached a non-transparent pixel
                If Not InLine Then
                    InLine = True
                    StartLineX = X
                End If
            End If
    MakeRegion = FullRegion
End Function


'Form1, Borderstyle = none
'Picturebox = picMainSkin
'CommandButton = Command1

Option Explicit

Private Sub Command1_Click()
    MsgBox "This is a real working form!"
End Sub

Private Sub Form_Load()
    Dim WindowRegion As Long
    ' I set all these settings here so you won't forget
    ' them and have a non-working demo... Set them in
    ' design time
    picMainSkin.ScaleMode = vbPixels
    picMainSkin.AutoRedraw = True
    picMainSkin.AutoSize = True
    picMainSkin.BorderStyle = vbBSNone
    Me.BorderStyle = vbBSNone
    Set picMainSkin.Picture = LoadPicture(App.Path & "\main.bmp")
    Me.Width = picMainSkin.Width
    Me.Height = picMainSkin.Height
    WindowRegion = MakeRegion(picMainSkin)
    SetWindowRgn Me.hWnd, WindowRegion, True
End Sub

Private Sub picMainSkin_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
      ' Pass the handling of the mouse down message to
      ' the (non-existing really) form caption, so that
      ' the form itself will be dragged when the picture is dragged.
      ' If you have Win 98, Make sure that the "Show window
      ' contents while dragging" display setting is on for nice results.
      SendMessage Me.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&

End Sub

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


I apologise for not getting back earlier.

JackOfPH your example works great!
Here I'd like to add 2 lines of code:
   picMainSkin.Left = 0
   picMainSkin.Top = 0
just after
  Me.Width = picMainSkin.Width
  Me.Height = picMainSkin.Height
to move the picturebox to the form's upper left corner for background image to display correctly in its position.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.