logo
vbRad Home
Source Code
Book Reviews
Forum
Links
About Us
Contribute

Compare Databases with SQL Effects Clarity
 
 Irregular Forms and Region Clipping, Part 2 (Advanced)

Posted on
8/9/2002
Author:
Brian Yule
Email:
Not Shown
Applies To OS:
NT, 9x, 2000
Product:
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





Add Your Comment  

Name: Email Address: all fields optional
Notify me via email when someone responds to this message (valid email required).

Enter the word:
 



Comments
#1. By Nagoor Kani. Posted on 3/10/2006 7:10:57 PM
Simply Superb!

#2. By Peter Lewis. Posted on 4/12/2006 3:53:44 PM
I thought something like this would only work in Windows 2000 or above...oh how I was wrong! I've just implemented it in my testing app and it works on Windows 95 too!!! This code is truly amazing!!! Thank you!!!

#3. By Gaal. Posted on 6/13/2006 6:19:40 PM
This is pretty good stuff. Now if only I could have the program automatically generate a temporary image to use which would place the visible regions to equal the exact shape/dimentions of all visible controls on the form, and make changes based on the appearance or disappearance of those controls during the running of the program. Then I might actually be able to use this.

#4. By Steve. Posted on 12/20/2006 11:57:43 AM
Nice code. Actually you can set the bacground picture of the form to the picture you want to use first to guide you as you put other controls like buttons e.t.c. good job.

#5. By d'artagnan. Posted on 3/20/2007 5:14:45 AM
kewl!!! really, wow!!!

#6. By Mike. Posted on 4/22/2007 4:10:30 PM
perfect

#7. By arvind. Posted on 10/11/2007 12:21:14 PM
Lot of thanks this article is very very useful

#8. By Reuben. Posted on 11/25/2007 11:11:30 PM
hi, would you be able to explain how to do this in Visual Basic 2008 Express Edition, if i copy you code i get "arrays cannot be defined as stdpicture".

#9. By Anonymous. Posted on 11/26/2007 5:18:59 PM
To Reuben (#8): I don't think the Express products support Interop. You are out of luck.

#10. By Anonymous. Posted on 12/26/2007 11:30:54 AM
i think You are open type becs. submit the net.

#11. By siva. Posted on 12/26/2007 11:30:57 AM
i think You are open type becs. submit the net.

#12. By siva. Posted on 12/26/2007 11:31:01 AM
i think You are open type becs. submit the net.

#13. By PRADEEP KAUSHIK. Posted on 12/28/2007 4:07:55 PM
very nice programming thanks for help

#14. By VB'R. Posted on 2/25/2008 7:08:30 PM
Fab code, works well....
Gave me an idea >?< :D ...

Grab a few screenshots of the users desktop, place them as Top Most form backgrounds and offset the by a cupple of pixels each time....

The Result ...

The user will be seing double .... (Also maybe set a timer to keep updating the screen shots!)

HAHAHA!

I have loads of pranks that I made in VB/C#/AHK ... Email me if you would like a copy of any (or maybe ill write one for your needs) :) - L___F@hotmail.com

#15. By albert. Posted on 4/17/2008 5:54:50 PM
great code
have been looking for this for a long time ---
keep up the good work
thanks