ShaneBrennan
Programmer
Hi I've found a lovely piece of code to make a form transparent and It works great!
Except for the fact that it does not display Image components on the form. it will display PictureBox Controls but not Image controls.
I wish to display an image of Santa on the screen, with a little speech bubble but no background. As far as I can tell the PictureBox component will display gifs but NOT transparent gifs, like the Image component can.
Can any one help me? I've enclosed the text of the procedure here, if anyone is interested. But I really need to display just transparent gifs with no background!
Thanks Shane Brennan
-------------------------------------------
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Declare Function CreateRectRgn Lib "gdi32" _
(ByVal X1 As Long, ByVal Y1 As Long, _
ByVal X2 As Long, ByVal Y2 As Long) As Long
Declare Function ScreenToClient Lib "user32" _
(ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Declare Function SetWindowRgn Lib "user32" _
(ByVal hwnd As Long, ByVal hRgn As Long, _
ByVal bRedraw As Boolean) As Long
Public Const RGN_AND = 1
Public Const RGN_COPY = 5
Public Const RGN_DIFF = 4
Public Const RGN_OR = 2
Public Const RGN_XOR = 3
Type POINTAPI
X As Long
Y As Long
End Type
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Sub MakeTransparent(frm As Form)
Dim rctClient As RECT, rctFrame As RECT
Dim hClient As Long, hFrame As Long
'// Grab client area and frame area
GetWindowRect frm.hwnd, rctFrame
GetClientRect frm.hwnd, rctClient
'// Convert client coordinates to screen coordinates
Dim lpTL As POINTAPI, lpBR As POINTAPI
lpTL.X = rctFrame.Left
lpTL.Y = rctFrame.Top
lpBR.X = rctFrame.Right
lpBR.Y = rctFrame.Bottom
ScreenToClient frm.hwnd, lpTL
ScreenToClient frm.hwnd, lpBR
With rctFrame
.Left = lpTL.X
.Top = lpTL.Y
.Right = lpBR.X
.Bottom = lpBR.Y
End With
With rctClient
.Left = Abs(rctFrame.Left)
.Top = Abs(rctFrame.Top)
.Right = rctClient.Right + Abs(rctFrame.Left)
.Bottom = rctClient.Bottom + Abs(rctFrame.Top)
End With
With rctFrame
.Right = rctFrame.Right + Abs(rctFrame.Left)
.Bottom = rctFrame.Bottom + Abs(rctFrame.Top)
.Top = 0
.Left = 0
End With
'// Convert RECT structures to region handles
hClient = CreateRectRgn(rctClient.Left, rctClient.Top, rctClient.Right, rctClient.Bottom)
hFrame = CreateRectRgn(rctFrame.Left, rctFrame.Top, rctFrame.Right, rctFrame.Bottom)
'// Create the new "Transparent" region
CombineRgn hFrame, hClient, hFrame, RGN_XOR
'// Combine the regions of the controls
'// on the form with the "transparent" region.
Dim C As Control, rctControl As RECT, hControl As Long
For Each C In frm
On Error Resume Next
'// Get the control's area
GetWindowRect C.hwnd, rctControl
'// Convert to client coordinates
lpTL.X = rctControl.Left
lpTL.Y = rctControl.Top
lpBR.X = rctControl.Right
lpBR.Y = rctControl.Bottom
ScreenToClient frm.hwnd, lpTL
ScreenToClient frm.hwnd, lpBR
With rctControl
.Left = lpTL.X + rctClient.Left
.Top = lpTL.Y + rctClient.Top
.Right = lpBR.X + rctClient.Left
.Bottom = lpBR.Y + rctClient.Top
End With
hControl = CreateRectRgn(rctControl.Left, rctControl.Top, rctControl.Right, rctControl.Bottom)
CombineRgn hFrame, hControl, hFrame, RGN_OR
Next C
'// Now lock the window's area to this created region
SetWindowRgn frm.hwnd, hFrame, True
End Sub
----------------------------------
TO use it:
Private Sub Form_Load()
MakeTransparent me
End Sub
Shane Brennan
Shane.Brennan@tcat.ac.uk
Except for the fact that it does not display Image components on the form. it will display PictureBox Controls but not Image controls.
I wish to display an image of Santa on the screen, with a little speech bubble but no background. As far as I can tell the PictureBox component will display gifs but NOT transparent gifs, like the Image component can.
Can any one help me? I've enclosed the text of the procedure here, if anyone is interested. But I really need to display just transparent gifs with no background!
Thanks Shane Brennan
-------------------------------------------
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Declare Function CreateRectRgn Lib "gdi32" _
(ByVal X1 As Long, ByVal Y1 As Long, _
ByVal X2 As Long, ByVal Y2 As Long) As Long
Declare Function ScreenToClient Lib "user32" _
(ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Declare Function SetWindowRgn Lib "user32" _
(ByVal hwnd As Long, ByVal hRgn As Long, _
ByVal bRedraw As Boolean) As Long
Public Const RGN_AND = 1
Public Const RGN_COPY = 5
Public Const RGN_DIFF = 4
Public Const RGN_OR = 2
Public Const RGN_XOR = 3
Type POINTAPI
X As Long
Y As Long
End Type
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Sub MakeTransparent(frm As Form)
Dim rctClient As RECT, rctFrame As RECT
Dim hClient As Long, hFrame As Long
'// Grab client area and frame area
GetWindowRect frm.hwnd, rctFrame
GetClientRect frm.hwnd, rctClient
'// Convert client coordinates to screen coordinates
Dim lpTL As POINTAPI, lpBR As POINTAPI
lpTL.X = rctFrame.Left
lpTL.Y = rctFrame.Top
lpBR.X = rctFrame.Right
lpBR.Y = rctFrame.Bottom
ScreenToClient frm.hwnd, lpTL
ScreenToClient frm.hwnd, lpBR
With rctFrame
.Left = lpTL.X
.Top = lpTL.Y
.Right = lpBR.X
.Bottom = lpBR.Y
End With
With rctClient
.Left = Abs(rctFrame.Left)
.Top = Abs(rctFrame.Top)
.Right = rctClient.Right + Abs(rctFrame.Left)
.Bottom = rctClient.Bottom + Abs(rctFrame.Top)
End With
With rctFrame
.Right = rctFrame.Right + Abs(rctFrame.Left)
.Bottom = rctFrame.Bottom + Abs(rctFrame.Top)
.Top = 0
.Left = 0
End With
'// Convert RECT structures to region handles
hClient = CreateRectRgn(rctClient.Left, rctClient.Top, rctClient.Right, rctClient.Bottom)
hFrame = CreateRectRgn(rctFrame.Left, rctFrame.Top, rctFrame.Right, rctFrame.Bottom)
'// Create the new "Transparent" region
CombineRgn hFrame, hClient, hFrame, RGN_XOR
'// Combine the regions of the controls
'// on the form with the "transparent" region.
Dim C As Control, rctControl As RECT, hControl As Long
For Each C In frm
On Error Resume Next
'// Get the control's area
GetWindowRect C.hwnd, rctControl
'// Convert to client coordinates
lpTL.X = rctControl.Left
lpTL.Y = rctControl.Top
lpBR.X = rctControl.Right
lpBR.Y = rctControl.Bottom
ScreenToClient frm.hwnd, lpTL
ScreenToClient frm.hwnd, lpBR
With rctControl
.Left = lpTL.X + rctClient.Left
.Top = lpTL.Y + rctClient.Top
.Right = lpBR.X + rctClient.Left
.Bottom = lpBR.Y + rctClient.Top
End With
hControl = CreateRectRgn(rctControl.Left, rctControl.Top, rctControl.Right, rctControl.Bottom)
CombineRgn hFrame, hControl, hFrame, RGN_OR
Next C
'// Now lock the window's area to this created region
SetWindowRgn frm.hwnd, hFrame, True
End Sub
----------------------------------
TO use it:
Private Sub Form_Load()
MakeTransparent me
End Sub
Shane Brennan
Shane.Brennan@tcat.ac.uk