Irregular Forms and Region Clipping for VB6 and VB.NET, Part 2 (Advanced)

Applies To

OS:
VB:
NT, 9x, 2000
5, 6, .NET

Author: Brian Yule (byule at PembrokeTechnology.com)
Note: if all this region clipping stuff is brand new to you,
it might be useful to get the basics in Part I of this article first.

Download... the VB 6 project. (478 kb)
Download... the VB.NET project. (77 kb)

Unless you've been living at the command line, you've probably seen those snazzy skinnable media players that have non-rectangular forms. Sonique, Windows Media, Real One are prime examples. Windows Media If you read part 1 of this article, you know that creating anything more complicated than a circle is a gigantic pain.

So wouldn't it be cool if you could specify an image, and the code would create the form for you, discarding the transparent color for you. Such functionality would be great. That is exactly what Brian Yule created. The code does the following:

  1. Loads the bitmap into memory
  2. Scans the bitmap
  3. Figures out the transparent color (either you specify or the first pixel)
  4. Iterating through the pixels, builds small regions carefully skipping transparent colors
  5. Combines all the regions into one and tells Windows to create a form from it.
Brian made it as simple to build as just adding these lines of code into Form_Load.
Option Explicit

Dim rgnBasic As New Region
Dim rgnExtended As New Region
Dim CurrentRgn As Long
Dim pic(0 To 1) As New StdPicture

Private Sub Form_Load()
    ' Load the image
    Set pic(1) = LoadPicture(App.Path & "\x.bmp", 0, 0, 0, 0)
    
    ' Scan the image
    Call rgnBasic.ScanPicture(pic(1))
    
    ' Offset the Shape to allow for the form header.
    Call rgnBasic.OffsetHeader(Me)
        
    Me.Picture = pic(1) ' Set the Form Background
    Call rgnBasic.ApplyRgn(Me.hWnd) ' Set the Form Shape
    CurrentRgn = rgnBasic.hndRegion ' Set the Current Shape

End Sub

In VB.NET it is as simple as adding the following to your form class:

Dim oClip As New Clipping()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    With oClip
        .ScanBitmap(Application.StartupPath & "\Sonique Small Extended.bmp")
        .SetFormScannedBitmapSize(Me)
        .ApplyScannedBitmap(Me.BackgroundImage)
        .ApplyScannedBitmapRegion(Me.Region)
    End With
End Sub

The coolest this about it is that this technique allows you to build disjointed forms. For instance, if you have an image of two guys shooting at each other on a white background, you'll get a form where you'll be able to drag it by the shooters' bodies, but not by the transparent space in between. This feature is illustrated by the image of Bart Simpson riding his skateboard in screenshots at the bottom of this article.

Screenshots


Image of Bart Simpson as the form. Note that the color in his eyes is transparent and is therefore not the part of the form. Clicking on his eyes will not cause the Form_Click event, even though his shirt will fire Click event.
 

Note that navigational labels. This shows that you can still have various controls on the actual derived form.
 

 

Download... the VB 6 project. (478 kb)
Download... the VB.NET project. (77 kb)

To get in touch with the author of this example, email Brian Yule at byule at PembrokeTechnology.com