Important alert: (current site time 7/16/2013 4:11:44 AM EDT)
 

VB icon

Make a Transparent Area (Any Size) in your Form

Email
Submitted on: 4/23/1999
By: Dalin Nie 
Level: Not Given
User Rating: By 109 Users
Compatibility: VB 5.0, VB 6.0
Views: 43359
 
     This function create a transparent area of dirrent shape (such as rectangle, Circle) in your form, you specify where and how big the hole is. Unlike most other trnsparant routine, this one not only let you see trough it, but also allow you total access access the things in the hole!!! Of course, You can make the entire form transparent or make you form C - shaped! Fully tested in VB5 and VB6.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Make a Transparent Area (Any Size) in your Form
' Description:This function create a transparent area of dirrent shape (such as rectangle, Circle) 
in your form, you specify where and how big the hole is. Unlike most other trnsparant 
routine, this one not only let you see trough it, but also allow you total access 
access the things in the hole!!! Of course, You can make the entire form transparent 
or make you form C - shaped!
Fully tested in VB5 and VB6.
' By: Dalin Nie
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1617&lngWId=1'for details.'**************************************

'1, Declararion
' This should be in the form's General Declaration Area. If you declare in a Modeule,
' you need to omit the word "private"
Private Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 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 CreateEllipticRgn 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
 
 
'2 The Function
' This should be in the form's code. 
Private Function fMakeATranspArea(AreaType As String, pCordinate() As Long) As Boolean
'Name: fMakeATranpArea
'Author: Dalin Nie
'Date: 5/18/98
'Purpose: Create a Transprarent Area in a form so that you can see through
'Input: Areatype : a String indicate what kind of hole shape it would like to make
' PCordinate : the cordinate area needed for create the shape:
' Example: X1, Y1, X2, Y2 for Rectangle
'OutPut: A boolean
Const RGN_DIFF = 4
Dim lOriginalForm As Long
Dim ltheHole As Long
Dim lNewForm As Long
Dim lFwidth As Single
Dim lFHeight As Single
Dim lborder_width As Single
Dim ltitle_height As Single
 On Error GoTo Trap
 lFwidth = ScaleX(Width, vbTwips, vbPixels)
 lFHeight = ScaleY(Height, vbTwips, vbPixels)
 lOriginalForm = CreateRectRgn(0, 0, lFwidth, lFHeight)
 
 lborder_width = (lFHeight - ScaleWidth) / 2
 ltitle_height = lFHeight - lborder_width - ScaleHeight
Select Case AreaType
 
 Case "Elliptic"
 
 ltheHole = CreateEllipticRgn(pCordinate(1), pCordinate(2), pCordinate(3), pCordinate(4))
 Case "RectAngle"
 
 ltheHole = CreateRectRgn(pCordinate(1), pCordinate(2), pCordinate(3), pCordinate(4))
 
 Case "RoundRect"
 
 ltheHole = CreateRoundRectRgn(pCordinate(1), pCordinate(2), pCordinate(3), pCordinate(4), pCordinate(5), pCordinate(6))
 Case "Circle"
 ltheHole = CreateRoundRectRgn(pCordinate(1), pCordinate(2), pCordinate(3), pCordinate(4), pCordinate(3), pCordinate(4))
 
 Case Else
 MsgBox "Unknown Shape!!"
 Exit Function
 End Select
 lNewForm = CreateRectRgn(0, 0, 0, 0)
 CombineRgn lNewForm, lOriginalForm, _
 ltheHole, RGN_DIFF
 
 SetWindowRgn hWnd, lNewForm, True
 Me.Refresh
 fMakeATranspArea = True
Exit Function
Trap:
 MsgBox "error Occurred. Error # " & Err.Number & ", " & Err.Description
End Function
 
 
' 3 How To Call 
 
Dim lParam(1 To 6) As Long
lParam(1) = 100
lParam(2) = 100
lParam(3) = 250
lParam(4) = 250
lParam(5) = 50
lParam(6) = 50
Call fMakeATranspArea("RoundRect", lParam())
'Call fMakeATranspArea("RectAngle", lParam())
'Call fMakeATranspArea("Circle", lParam())
'Call fMakeATranspArea("Elliptic", lParam())


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
4/19/1999 8:27:00 AManti

I'm making a project in VB 3.o [I normally use 5.o tho], and this code is unfortunatly for 32 bit programming only. If anyone has a similar code for 16 bit basic, please email me with it; elite_83@hotmail.com.
(If this comment was disrespectful, please report it.)

 
4/21/1999 7:22:00 PMskafia

I cant seem to get this code working, it keeps giving me errors with the source, im using vb5, email me if you can help.
(If this comment was disrespectful, please report it.)

 
4/22/1999 5:09:00 AMDalin

I just tested it again, and it works fine.
Did you declare in a module? What is the error
message?
(If this comment was disrespectful, please report it.)

 
4/22/1999 9:33:00 AMDJLawlor

This one is so cooooooooooooooool!!!
(If this comment was disrespectful, please report it.)

 
4/23/1999 6:24:00 PMJohn Barden

I have not yet used this, but will it allow me have a maximized transparent form and run other applications within it?
(If this comment was disrespectful, please report it.)

 
4/24/1999 4:05:00 AMDalin

To John Barden: Yes!!!
(If this comment was disrespectful, please report it.)

 
5/1/1999 7:57:00 AMIan Ippolito

This code worked for me on the first try. This is really cool!
(If this comment was disrespectful, please report it.)

 
5/1/1999 11:46:00 AMEric M

Im unclear about the positioning you have in this..I cant seem to figure it out.. What is lparam 1-6 like lparam 1 in the example is that the topright ? anyways im familiar with x1 and y1 but thats only 4 numbers you have 6 numbers..
(If this comment was disrespectful, please report it.)

 
5/2/1999 6:52:00 PMJay

Can someone direct me trought the proccess of this source code please i follwed the instruction and exceute he code but nothig happens i only see the regular form no hols or any thing in it so please help me with this .

thanks in advance.
jay...
(If this comment was disrespectful, please report it.)

 
5/2/1999 10:48:00 PMTerrance Henry Shaw

I don't really get how you use this code. It is so confusing. Where do I enter the code?
(If this comment was disrespectful, please report it.)

 
5/3/1999 8:29:00 AMDoug Bumbalough

Great code! Worked on first try!
(If this comment was disrespectful, please report it.)

 
5/3/1999 9:27:00 PMZeRoFrEEzE

Kool stuff, im 16 years old, and got it on the first try. Anyone interested on testing out software, please e-mail me and ill send you some stuff i created...
(If this comment was disrespectful, please report it.)

 
5/4/1999 7:43:00 AMDOROSMAN

Super!
(If this comment was disrespectful, please report it.)

 
5/4/1999 5:23:00 PMAsim

This is so wicked. The only thing is that it doesn't support curne shapes. if know how please email me. thnaks
(If this comment was disrespectful, please report it.)

 
5/5/1999 7:47:00 AMdave shields

superb!

worked first time

to all those having problems....
have you followed the instructions and applied the calling code to an action (a button for instance)?
(If this comment was disrespectful, please report it.)

 
5/5/1999 2:55:00 PMSoPa

Great Code!!!
Worked on the first try!
Thanxs a lot man!
But can u perhaps make a note of each step
of the code??

(If this comment was disrespectful, please report it.)

 
5/5/1999 5:19:00 PMFlip

really really coool.
Not sure what I'll use this for, but I'm sure to think of something!

for those having probs...don't skip step three and assign this event to Form_Load if nothing else.
(If this comment was disrespectful, please report it.)

 
5/6/1999 5:02:00 AMMarcus

Excellent piece of work, its a bit iffy when you move the form, but thats about it. I'm not sure what I'm going to use it for though.


(If this comment was disrespectful, please report it.)

 
5/6/1999 5:28:00 AMMarcus

Add the following code to a timer and watch the zany efects:

Private Sub Timer1_Timer()
Dim lParam(1 To 6) As Long

lParam(1) = Int(Rnd() * Me.Width / 15)
lParam(2) = Int(Rnd() * Me.Height / 15)
lParam(3) = Int(Rnd() * Me.Width / 15)
lParam(4) = Int(Rnd() * Me.Height / 15)
lParam(5) = 50
lParam(6) = 50

Select Case Int(Rnd() * 4)
Case 0
Call fMakeATranspArea("RoundRect", lParam())
Case 1
Call fMakeATranspArea("RectAngle", lParam())
Case 2
Call fMakeATranspArea("Circle", lParam())
Case 3
Call fMakeATranspArea("Elliptic", lParam())
End Select
End Sub
(If this comment was disrespectful, please report it.)

 
5/6/1999 10:03:00 AMeric chastain

Cool function
(If this comment was disrespectful, please report it.)

 
5/6/1999 11:20:00 AMRuss

Very nicely done! Worked first time! With a slight tweak here and there, I now have the function working in a .Bas module where I can pass any form name to the function. Also, using a Type declaration and an Enumerated data type made it a little tidier.

Anyone interested in seeing my changes, feel free to e-mail me.
(If this comment was disrespectful, please report it.)

 
5/6/1999 5:43:00 PMGyuri van de Bilt

Hi there,
Indeed really cool piece of coding-art.
BUT:
On resizing a window that was made transparant (for a small part) you won't see more of the window than when the 'fMakeATranspArea' function was called.
The solution to this problem (test it and see) is to call the function (with the same param) again when resizing the window (Form1_Resize()).

It will now work perfectly!
(If this comment was disrespectful, please report it.)

 
5/12/1999 5:55:00 AMRoche Moore

really really excellent
works a treat
now, any ideas (good) where to use it?!
(If this comment was disrespectful, please report it.)

 
5/12/1999 8:10:00 AMJoel León Malpartida

it's great code
I'm a peruvian systems programmner.
greetings.
(If this comment was disrespectful, please report it.)

 
5/12/1999 10:37:00 PMNikhil Acharya

Good Code , Thanks , I have created a
Telephone shape form using above code
for a dialup application.
(If this comment was disrespectful, please report it.)

 
5/14/1999 8:55:00 AMJaw-Breaker

I haven't test it yet. I'm working on a hackers control station, I want everthing for hacking in one program. I think I can really use this to shape up the program.

Jaw-Breaker from Noth-Side hackers
(If this comment was disrespectful, please report it.)

 
5/18/1999 11:30:00 AMdj277

My teacher makes usdo this vb crap, and I know just about jack about it.
Oh, Well.
Help!!
I think he's coming!!!
(If this comment was disrespectful, please report it.)

 
5/18/1999 12:09:00 PMt30od

I cant seem to get it to work, can someone please send me their source?
Thanks
(If this comment was disrespectful, please report it.)

 
5/19/1999 2:57:00 AMHuman

COOOL !!!
try it, it works ...
(If this comment was disrespectful, please report it.)

 
5/19/1999 2:14:00 PMLOGIX

Wow this is some cool code, if any one is confused email me and i will explain
(If this comment was disrespectful, please report it.)

 
5/20/1999 6:26:00 AMjs

CCCCCCCCCCCCCCOOOOOOOOOOOOOLLLLLLLLLLLLLLLL CCCCCCCCCCOOOOOOOOOOOODDDDDDDDDDEEEEEEEEE

Check this site:

http://browse.to/nm
(If this comment was disrespectful, please report it.)

 
5/21/1999 1:05:00 PMkinema

Code is wicked ... how can I bitblt stuff from invisible picture boxes into that transparent area? That way you eliminate the need for screen capturing.

Oh, and I'm looking for kewl graphics effects. thanks.

listen.to/kinema
(If this comment was disrespectful, please report it.)

 
5/23/1999 9:46:00 AMah Boon

transparent part in form
(If this comment was disrespectful, please report it.)

 
5/23/1999 9:18:00 PMcryptic

How do u make it so objects are visible if u
on top of the transparent area?
(If this comment was disrespectful, please report it.)

 
5/24/1999 8:26:00 AMEric Gaulin

Would it be possible to exclude the specified region, so the border of the form will be transparent instead?
(If this comment was disrespectful, please report it.)

 
5/24/1999 9:21:00 AMDAVID PALCZEWSKI -----------

I cant get this to work! It seems it will be awesome, but can someone send a example to me. I am using VB 4.0.
(If this comment was disrespectful, please report it.)

 
5/24/1999 11:54:00 PMAlistair Crawford

Excellent code, worked like a charm.
Is there anyway to make the transparent area, just transparent and not be able to click other applications through the hole. I want to make a screen capture, draggable cropping frame, so users must not be able to click the applications inside the frame. Please HELP!!!! Anybody.
(If this comment was disrespectful, please report it.)

 
5/25/1999 10:21:00 PMDarkNemesis

Code Works Great..
Heres my situation..

I want to make my entire form transparent, but still show an image on top ( the image dont go transparent ).

Can anyone help me with this?
(If this comment was disrespectful, please report it.)

 
5/25/1999 11:40:00 PMDavid Ulloa P.

Excellent stuff!!!!
(If this comment was disrespectful, please report it.)

 
5/26/1999 7:39:00 AMaluminumblue

nice code.. worked 1st time.. new toy for my mind, thanks.
(If this comment was disrespectful, please report it.)

 
5/26/1999 9:41:00 AMSolomon

Great code! You must have gotten the idea from PeepShow (prog that uses this feature)...nice translation into VB. Thanks!!!
(If this comment was disrespectful, please report it.)

 
5/26/1999 3:22:00 PMSharkero

Great, it work the first time!.. I copy
the code for step 3 to a command button
Command 1 event. Added a button to the
form and double clicked it. Then I copy
the code and then REMed it out from the
form. It worked when I clicked the button
you have to enlarged the form a little
bit to see the hole and then move the form
around and you will see your code
or other windows behind it. It is cool!
(If this comment was disrespectful, please report it.)

 
5/31/1999 12:48:00 AMFilip

Superb code, but I cannot run the function multiple times for one form, because you don't start from the form's current region, but from a square region with the size of the form.

I tried to use the API function GetWindowRgn, but I always got 0 for the rgn.

I also tried the API function CreatePolygonalRgn but could not make it work.

Could you try it?

PS: Does anyone know how to add a menu to the Windows Explorer File/Folder right-click dropdown menu?
To do something like WinZip.

Thanks anyway,
Filip
(If this comment was disrespectful, please report it.)

 
5/31/1999 3:48:00 AMKenny

Great site!!!
I wonder about gettig a combobox that
looks like the comboboxes for load/save in office97, with both drives and folders in the same combobox.
Please help me someone!!!!!
(If this comment was disrespectful, please report it.)

 
6/5/1999 8:42:00 PM-

This code is good.
Anyone know of a place where I can get my fat fingers something that'll show me ani-gifs.
I wanna create a proggie that is like one of those message ones....
(If this comment was disrespectful, please report it.)

 
6/10/1999 4:14:00 PMHewDude

Cool Script, it should be pointed outCool Script, it should be pointed out that the last ten or so lines should be between "Private Sub Form_Load()" and "End Sub" Has anyone modified this script to show images or buttons infront of the Transparent area ?
(If this comment was disrespectful, please report it.)

 
6/12/1999 6:09:00 PMMichael

I got the code to work on the second try, but that's ok. I just don't get how to make my own shapes, like the one guy who make a phone shape. Can someone explain to me how to make the different shapes?

Email me if you can tell me
(If this comment was disrespectful, please report it.)

 
6/21/1999 10:38:00 PMhey

im having trouble with the first word in the following part: "SetWindowRgn" hWnd, lNewForm, True
Me.Refresh
fMakeATranspArea = True
Exit Function
Trap:
MsgBox "error Occurred. Error # " & Err.Number & ", " & Err.Description
End Function
(If this comment was disrespectful, please report it.)

 
7/1/1999 10:28:00 AMDaZZlock

Nåja den är väl ganska bra men jag har inte testat den än
(If this comment was disrespectful, please report it.)

 
7/16/1999 6:11:00 PMschneider


Be careful when changing region while
resizing: the code may run, but the compiled may not! -bug I solved a few months ago...........
(If this comment was disrespectful, please report it.)

 
7/17/1999 9:52:00 AMJason Petralia

For those of you who can't get this to work, here's what you do. Near the end of the code, it says "'3 How to call" Put everything above that line in the declarations area of your form. Put everything below that line where you want the transparency to be activated, such as a command button.

(If this comment was disrespectful, please report it.)

 
8/5/1999 12:59:00 PMmew

this makes the whole thing transparent,
even the controls in the form..
how can i prevent the controls from being transparent 2...;(
(If this comment was disrespectful, please report it.)

 
8/14/1999 7:49:00 PMChris

Very Nice Code, now how can I make an image or control show up in front of the transparent area?
(If this comment was disrespectful, please report it.)

 
8/25/1999 9:23:00 AMjaQx

Fun, but cant have other controls within transparent area without those turning transparent also...
(If this comment was disrespectful, please report it.)

 
9/7/1999 12:14:00 PMSure it works... whatever

Could someone just put a plain example for usage? something like:

Private Sub Form_Load()
fMakeATranspArea("RectAngle", lParam(5, 10, 10, 10))
End Sub

that's what I have but it just says 'expected ='
I'm not an |diot but people really need more documentation included with their coding
(If this comment was disrespectful, please report it.)

 
9/7/1999 4:42:00 PMHomer

Sure it works... whatever, ill call him that or: that guy up there ^. you are an |diot(i realize u need the | because |diot is innapropriat language?), and dont let any one tell you otherwise, because thats a horrible HORRIBLE lie. u should learn Visual Basics before looking up other peoples source code, because u wouldnt need extra documentation if you knew VB. IM me on AOL: homer3685 for help u need with VB, in case you want to LEARN it. :)
(If this comment was disrespectful, please report it.)

 
9/8/1999 4:01:00 PMLorenz

Hi! I'm from philippines, and I'd like to say nice code! First try it works.... but I have a question what is lparam(5) and lparam(6) for?..
But anyway keep up the good work... thanx..
(If this comment was disrespectful, please report it.)

 
9/9/1999 3:25:00 PMAnDroiD

For those who can't run the code here is how...

1. Paste all the code in general declaration.
2. Create a command button.
3. Rename "' 3 How To Call" to "Private Sub Command1_Click()"
4. Don't forget to put an "End Sub" at the last part of the code.

There ya go it should work now... :)
(If this comment was disrespectful, please report it.)

 
9/10/1999 10:22:00 PMRob Volk

This Code is SO easy to use... Don't know why every1's haveing such a problem... BUT, How do I
place objects on top of the transparent part?
(If this comment was disrespectful, please report it.)

 
9/18/1999 9:33:00 PMGrant

Nice code, and easy! But is there a way to make a picture box stay on top? If so please email me
-thanks-
(If this comment was disrespectful, please report it.)

 
10/5/1999 6:04:00 PMRoger Taylor

This code worked fine using VB 6.0. The first time I tried it.
Great code no problems.
(If this comment was disrespectful, please report it.)

 
10/6/1999 11:48:00 PMbugsmalli

Hey there Dalin,
great code. May I suggest a simple modification. in the declarations section just declare one more api call:

Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
And anywhere in the form jus declare this sub:
Public Sub FormOnTop(handle As Long, ontop As Boolean)
Dim wFlags As Long, posflag As Long
wFlags = SWP_NOMOVE Or SWP_NOACTIVATE Or SWP_NOSIZE Or SWP_SHOWWINDOW
Select Case ontop
Case True
posflag = HWND_TOPMOST
Case False
posflag = HWND_NOTOPMOST
End Select
SetWindowPos handle, posflag, 0, 0, 0, 0, wFlags
End Sub

And add this piece of line AFTER the call to fMakeATranspArea:

formonTop me.hwnd, true

and see the fun.
have fun all,
bugsmalli.
(If this comment was disrespectful, please report it.)

 
10/11/1999 11:31:00 PMIrman Jamil

You got the target! Nice code. worked great.
I think every body should also try
sugestion from bugsmalli
(If this comment was disrespectful, please report it.)

 
7/5/2000 2:04:42 PMTommy

Hi, thanks for the great code! It worked fine creating a hole in my form, but I can't figure out what each of the
(If this comment was disrespectful, please report it.)

 
7/5/2000 2:10:29 PMTommy

Hi, this code worked fine creating a hole in my form, but I can't figure out what each of the "lParam()"s do. I can't figure out which one controls the top, bottom, left, and right. If anybody could explain what each of the 1-6 does, I'd greatly eppreciate it. Thanks!
(If this comment was disrespectful, please report it.)

 
6/16/2002 3:31:52 AMRichard

Astounding! Great job! I'm going to study this code... you've given me much to think about.
(If this comment was disrespectful, please report it.)

 
3/23/2004 10:37:06 AMGTCriswell

Hey there is a hole in my form!
(If this comment was disrespectful, please report it.)

 
4/2/2005 1:24:15 AM

For those who want to know what each IParam() is:

IParam(1) = the x1 point
IParam(2) = the y1 point
both Iparam (1 and 2) declare were the top left of the hole starts.
IParam(3) = distances from x1(Note: in pixels)
IParam(4) = distance from y1(Note: in pixels)
I think not 100% certain that IParam(5 & 6) control how round the edges will be on a roundrectanlge.
I find it best to set the property boderstyle to 0 - none if you still not the orignal border add an extra 29pixels to IParam(2) if you want to hide evrything but the boder or want to start at bottom edge of the border.
Hey does anyone know how to add more then one hole if so send me a message telling me how at dandan.kun@gmail.com and also very nice code delin nie.

(If this comment was disrespectful, please report it.)

 
6/19/2005 2:56:23 PMZimDale

Umm is there anyway to make it where it takes the transparency away. Like so I can set up a timer where it will flash transparent then go opeque again?

if there is could ya plese email it to Zimdale@gmail.com
(If this comment was disrespectful, please report it.)

 
8/30/2005 3:01:30 AMSPY-3

donno why you say unlike most others from what ive seen on psc so far every transparency program lets you access the area in the hole but still does what it says so good job!
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.