July 1, 1995
When developing an application in Microsoft® Visual Basic®, you may need to design a transparent form. This article explains how to create transparent forms.
A transparent form is a form that, when displayed, does not cover up the underlying windows beneath it. The Microsoft® Windows® application programming interface (API) function SetWindowLong can be used to change the style settings of a form or window. You can create a transparent window by setting the WS_EX_TRANSPARENT style bit.
To use the SetWindowLong function within your Microsoft Visual Basic® program, include the following Declare statement in the General Declarations section of a form (note that it must be typed as a single line of code):
Private Declare Function SetWindowLong Lib "User" (ByVal hWnd As Integer, ByVal
nIndex As Integer, ByVal dwNewLong As Long) As Long
The SetWindowLong function takes the following arguments.
hWnd | An integer value containing the window's handle. | |
nIndex | An integer value that describes the type of information you want to set. This value may be one of the following: | |
GWL_EXSTYLE | Set the extended window style | |
GWL_STYLE | Set the window style | |
GWL_WNDPROC | The window function's address | |
dwNewLong | A long value containing the new style bits to be given to the window. |
Because you want to make the specified form transparent, you call the SetWindowLong function with the nIndex argument set to GWL_EXSTYLE, specifying the transparency style bit.
When you run the example program shown below, the form will be displayed in transparent mode.
This program shows how to create a transparent form.
Private Sub Command1_Click()
Form2.Show
End Sub
Private Declare Function SetWindowLong Lib "User" (ByVal hWnd As Integer, ByVal
nIndex As Integer, ByVal dwNewLong As Long) As Long
Const WS_EX_TRANSPARENT = &H20&
Const GWL_EXSTYLE = (-20)
Private Sub Form_Load()
Dim Ret As Long
Ret = SetWindowLong(Form2.hWnd, GWL_EXSTYLE, WS_EX_TRANSPARENT)
End Sub
Run the example program by pressing F5. Click the "Show Form" command button. Form2 is displayed directly over Form1. It appears as if only Form1 is displayed because Form2 has the transparent attribute.