471,850 Members | 894 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,850 software developers and data experts.

transparent backcolor


how can i make the backcolor of a form transparent

best regards
Jul 17 '05 #1
2 7256
On Fri, 2 Jan 2004 10:56:02 +0100, "Henrik"
<ds*******@vip.cybercity.dk> wrote:


how can i make the backcolor of a form transparent

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

Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_TRANSPARENT = &H20&

Public Sub MakeTransparent(frmIn As Form)
' Comments : Sets the form's style to be transparent. This call
' be made before the form is shown, for example in the
' event
' Parameters: frmIn - form to modify
' Returns : Nothing
' Source : Total VB SourceBook 6
Dim lngResult As Long

On Error GoTo PROC_ERR

lngResult = SetWindowLong(frmIn.hwnd, _

Exit Sub

MsgBox "Error: " & Err.Number & ". " & Err.Description, , _

End Sub
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 CreateEllipticRgn _
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(TheControl As Object)
Dim hRegion As Long, lResult As Long
TheControl.Height = TheControl.Width
hRegion = CreateEllipticRgn(0, 0, TheControl.Width \ _
Screen.TwipsPerPixelX, TheControl.Height \ _
lResult = SetWindowRgn(TheControl.hWnd, hRegion, True)
End Sub

Private Sub Command1_Click()
MakeMeRound Me
End Sub

Jul 17 '05 #2
> how can i make the backcolor of a form transparent

This is from a post I've previously offered in response to a similar

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

MakeContainerTransparent 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 CreatePolygonRgn _
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

Private Const RGN_OR = 2
Private Const ALTERNATE = 1

Public Sub MakeContainerTransparent(TheContainerObject 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 TheContainerObject Is Form Then
Set ContainObj = TheContainerObject
Set ContainObj = TheContainerObject.Parent
End If
TPPx = Screen.TwipsPerPixelX
TPPy = Screen.TwipsPerPixelY
For Each Ctrl In ContainObj.Controls
If Ctrl.Container Is TheContainerObject 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 = CreatePolygonRgn&(CtrlShape(0), 4&, ALTERNATE)
ReturnVal = CombineRgn(ComboReg, ComboReg, TempReg, RGN_OR)
DeleteObject TempReg
ComboReg = CreatePolygonRgn&(CtrlShape(0), 4&, ALTERNATE)
NotFirstControl = True
End If
End With
End If
Set Ctrl = Nothing
Set ContainObj = Nothing
SetWindowRgn TheContainerObject.hWnd, ComboReg, True
DeleteObject ComboReg
End Sub
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Efkas | last post: by
4 posts views Thread by jcrouse | last post: by
2 posts views Thread by edoepke | last post: by
2 posts views Thread by Pascal | last post: by
3 posts views Thread by vul | last post: by
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.