How To Create Custom Shaped Windows with VB6

Posted on 2006-03-31
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!
Question by:guest321
    LVL 95

    Expert Comment

    by:Lee W, MVP
    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.
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    LVL 15

    Accepted Solution

    '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 WM_NCLBUTTONDOWN = &HA1
    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

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now