PDA

Click to See Complete Forum and Search --> : Form With A Hole In It


AttnSue
Apr 29th, 2002, 12:27 PM
Over the weekend, I saw this VB program in which one of the forms had a "hole" in it. I mean you could actually see the desktop through this "hole" in the form.

Anyone know how to do that?

Hack
Apr 29th, 2002, 12:35 PM
Create A Form With A Hole In It (http://www.thescarms.com/vbasic/IrregularForms.asp)

SLH
Apr 29th, 2002, 12:36 PM
From www.vbcode.com


'1, Declararion
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


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'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())


P.S. Looks like you've got an interesting interface in the making, judging by your previous post :)

SLH
Apr 29th, 2002, 12:37 PM
So near, yet so far :)

Hack
Apr 29th, 2002, 02:21 PM
Nice piece of code SLH...

AttnSue
Apr 29th, 2002, 02:29 PM
Thanks guys! :D

Megatron
Apr 29th, 2002, 02:51 PM
Here's another piece of code that does it.

Private Declare Function GetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn 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 CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) 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 Const RGN_XOR = 3

Private Sub Form_Load()

Dim hCurRgn As Long
Dim hHoleRgn As Long
Dim hNewRgn As Long
Dim nWidth As Long, nHeight As Long
Dim nSize As Long

nWidth = Me.Width / Screen.TwipsPerPixelX
nHeight = Me.Height / Screen.TwipsPerPixelY

hNewRgn = CreateRectRgn(0, 0, nWidth, nHeight)
hHoleRgn = CreateEllipticRgn(100, 100, 250, 250)
CombineRgn hNewRgn, hNewRgn, hHoleRgn, RGN_XOR
SetWindowRgn hWnd, hNewRgn, True

End Sub

McCain
Apr 29th, 2002, 06:57 PM
Nice code Megatron. But you shouldn't put the code to make the hole in Form_Load, try maximize the form and you'll see what I mean...

Put the code you have in Form_Load in Form_Resize instead and it'll work much better

Hack
Apr 30th, 2002, 09:18 AM
Originally posted by McCain
Nice code Megatron. But you shouldn't put the code to make the hole in Form_Load, try maximize the form and you'll see what I mean...

Put the code you have in Form_Load in Form_Resize instead and it'll work much betterVery good point. I tried Megatron's code yesterday when he posted it (and I like it very much. Much better than the code on the link I posted :D) and it worked great, but, I didn't maximize the form when I was running it.

This does work better in Form_Resize!

Thanks McCain. :)

Megatron
Apr 30th, 2002, 03:13 PM
Oops. Didn't see that.

Although the only problem with putting it in the resize event is that these regions will constantly be created every time the form is resized thereby using up resources.

A couple DestryObjects in there should do the trick though.

McCain
Apr 30th, 2002, 09:05 PM
But how often does the form resize? If it's only when you actually change the size of the form it shouldn't be that bad, should it?

SLH
May 1st, 2002, 01:36 AM
I think the form's resize event gets called WHILE you are resizing it, so it would get called a lot.

Megatron
May 1st, 2002, 03:14 PM
SLH is correct.

And by the way, I made a typo in my previous post. The function name is DeleteObject, not DestroyObject.

cutamacious
May 2nd, 2002, 05:36 AM
How do we make the controls on the form visible even when a hole is punched in the form.

Sastraxi
May 2nd, 2002, 07:25 AM
Just make sure they are out of the way of the hole (in the region specified). If you want them to be in the middle of the hole, make a rectangular region where you want it to be and OR it with the existing region.

McCain
May 2nd, 2002, 09:18 PM
I put this code in just to see how many times form_resize actualy gets called...
Dim intSize As Integer

Private Sub Form_Resize()
intSize = intSize + 1
Me.Caption = intSize
End Sub


If you maximize the form it only gets called once but if you resize it using the mouse it can get called 100+ times... So I guess we do need the DeleteObject...

pavan
May 3rd, 2002, 02:11 AM
Originally posted by Sastraxi
Just make sure they are out of the way of the hole (in the region specified). If you want them to be in the middle of the hole, make a rectangular region where you want it to be and OR it with the existing region.

That is pretty interesting but how do we actually do it?

AlterEgo
May 6th, 2002, 11:12 AM
AttnSue: If I can ask, why you want a form with a hole in it?

Hack
May 21st, 2002, 08:24 AM
Thanks.