1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
| Option Explicit
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, |