On Fri, 2 Jan 2004 10:56:02 +0100, "Henrik"
<dsl122981@vip. cybercity.dk> wrote:
[color=blue]
>
>Hi
>
>how can i make the backcolor of a form transparent
>[/color]
Here is some code that someone posted some time ago
- followed by another demo
Notice the MouseMove
- it shows that, although transparent, it is still 'on top'
The second Demo produces a 'shaped' Form
- that does _not_ get the mouse events in the transparent area
Option Explicit
Private Declare Function SetWindowLong _
Lib "user32" Alias "SetWindowLongA " ( _
ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Public Sub MakeTransparent (frmIn As Form)
' Comments : Sets the form's style to be transparent. This call
should
' be made before the form is shown, for example in the
Load
' event
' Parameters: frmIn - form to modify
' Returns : Nothing
' Source : Total VB SourceBook 6
'
Dim lngResult As Long
Private Sub Form_Load()
Call MakeTransparent (Me)
End Sub
Private Sub Form_MouseMove( Button As Integer, Shift As Integer, X As
Single, Y As Single)
MsgBox "Mouse Move"
End Sub
====== SECOND EXAMPLES =====
Option Explicit
' from Mike Williams
Private Declare Function CreateEllipticR gn _
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 Long _
) As Long
Sub MakeMeRound(The Control As Object)
Dim hRegion As Long, lResult As Long
TheControl.Heig ht = TheControl.Widt h
hRegion = CreateEllipticR gn(0, 0, TheControl.Widt h \ _
Screen.TwipsPer PixelX, TheControl.Heig ht \ _
Screen.TwipsPer PixelY)
lResult = SetWindowRgn(Th eControl.hWnd, hRegion, True)
End Sub
Private Sub Command1_Click( )
MakeMeRound Me
End Sub
> how can i make the backcolor of a form transparent
This is from a post I've previously offered in response to a similar
question...
Rick - MVP
Add a Module to your project (Project/AddModule from VB's menu) and paste
the code at the end of this message into it. To use it, simply call the Sub
with the Form (actually any container; Frame, PictureBox, etc.) as the
argument like so
MakeContainerTr ansparent Form1
assuming your form is named Form1. All controls on the container will show
up but the container itself won't. There is a downside to this method.
OptionButtons, CheckBoxes and Labels will have the background color of their
text portion show; you can't make them transparent using the method I
incorporate below. However, this does make it easier to read their text if a
solid background it behind them.
Private Type POINTAPI
X As Long
Y As Long
End Type
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 CreatePolygonRg n _
Lib "gdi32" _
(lpPoint As POINTAPI, _
ByVal nCount As Long, _
ByVal nPolyFillMode As Long) _
As Long
Private Declare Function CombineRgn _
Lib "gdi32" _
(ByVal hDestRgn As Long, _
ByVal hSrcRgn1 As Long, _
ByVal hSrcRgn2 As Long, _
ByVal CombineMode As Long) _
As Long
Private Declare Function SetWindowRgn _
Lib "user32" _
(ByVal hWnd As Long, _
ByVal hRgn As Long, _
ByVal bRedraw As Long) _
As Long
Private Declare Function DeleteObject _
Lib "gdi32" _
(ByVal hObject As Long) _
As Long
Public Sub MakeContainerTr ansparent(TheCo ntainerObject As Object)
Dim TPPx As Long
Dim TPPy As Long
Dim Ctrl As Control
Dim CtrlShape(3) As POINTAPI
Dim ComboReg As Long
Dim TempReg As Long
Dim ReturnVal As Long
Dim ContainObj As Object
Dim NotFirstControl As Boolean
If TypeOf TheContainerObj ect Is Form Then
Set ContainObj = TheContainerObj ect
Else
Set ContainObj = TheContainerObj ect.Parent
End If
TPPx = Screen.TwipsPer PixelX
TPPy = Screen.TwipsPer PixelY
For Each Ctrl In ContainObj.Cont rols
If Ctrl.Container Is TheContainerObj ect Then
With Ctrl
CtrlShape(0).X = .Left \ TPPx
CtrlShape(0).Y = .Top \ TPPy
CtrlShape(1).X = (.Left + .Width) \ TPPx
CtrlShape(1).Y = .Top \ TPPy
CtrlShape(2).X = (.Left + .Width) \ TPPx
CtrlShape(2).Y = (.Top + .Height) \ TPPy
CtrlShape(3).X = .Left \ TPPx
CtrlShape(3).Y = (.Top + .Height) \ TPPy
If NotFirstControl Then
TempReg = CreatePolygonRg n&(CtrlShape(0) , 4&, ALTERNATE)
ReturnVal = CombineRgn(Comb oReg, ComboReg, TempReg, RGN_OR)
DeleteObject TempReg
Else
ComboReg = CreatePolygonRg n&(CtrlShape(0) , 4&, ALTERNATE)
NotFirstControl = True
End If
End With
End If
Next
Set Ctrl = Nothing
Set ContainObj = Nothing
SetWindowRgn TheContainerObj ect.hWnd, ComboReg, True
DeleteObject ComboReg
End Sub
Comment