WinAPI : Making Form Transparent
Getting ahead with WinAPI functions related to window, we will discuss here two API functions GetWindowLong and SetWindowLong.
When you need to retrieve the information about a particular window you can use the SetWindowLong function. The function takes for input the handle to the window that you want to get information of, and a index which specifies what type of information is required by you from among the constants as defined below. The return value is a long that contains the requisite information. The return value for Styles and Extended styles are combination of what is already discussed in earlier post RECT applied to Windows.
Private Declare Function GetWindowLong Lib "user32" Alias _ "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long Const GWL_STYLE = (-16) ' Get Window's Style Const GWL_EXSTYLE = (-20) ' Get Window's Extended Style Const GWL_WNDPROC = (-4) ' Get handle to address of window procedure Const GWL_HINSTANCE = (-6) ' Get handle to Application Instance Const GWL_HWNDPARENT = (-8) ' Get handle to parent window Const GWL_ID As Long = (-12) ' Get identifier of the window Const GWL_USERDATA = (-21) ' Get user data associated with the window.
Similarly you can also modify / set the window's information using the SetWindowLong function. Other than parameters as above the function takes additional parameter for specifying the new value to be set. If the function fails the return value is zero, otherwise it returns the previous value corresponding to the information being set.
Private Declare Function SetWindowLong Lib "user32" Alias _
"SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Const GWL_STYLE = (-16) ' Set Window's Style
Const GWL_EXSTYLE = (-20) ' Set Window's Extended Style
Const GWL_WNDPROC = (-4) ' Set handle to address of window procedure
Const GWL_HINSTANCE = (-6) ' Set a new Application Instance handle
Const GWL_ID As Long = (-12) ' Set identifier of the window
Const GWL_USERDATA = (-21) ' Set user data associated witht he window.
Now what purpose these functions will serve? Imagine if you could make the form invisible leaving only the visible controls! Why not do it!!
Create a new project and place one or two controls as text box and Command buttons over it. Copy the code to the code section of the form. The code below declares the two required functions and the constants for Index, Styles and Extended Styles.
The load event of the form gets the current Extended styles of the form using GetWindowLong function and then adds the WS_EX_TRANSPARENT style if not already set. It then Sets the new Extended style of the form which in turn renders the form transparent while the controls over it still visible. Form1 is then successively made invisible and then visible ensuring that the form is repainted to accomodate the changes.

The picture showing the code page behind the form which itself is transparent while the controls on it are fully visible.
Private Declare Function GetWindowLong Lib "user32" Alias _
"GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias _
"SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
' Window Indexes
Const GWL_STYLE = (-16) ' Get Window's Style
Const GWL_EXSTYLE = (-20) ' Get Window's Extended Style
Const GWL_WNDPROC = (-4) ' Get handle to address of window procedure
Const GWL_HINSTANCE = (-6) ' Get handle to Application Instance
Const GWL_HWNDPARENT = (-8) ' Get handle to parent window
Const GWL_ID As Long = (-12) ' Get identifier of the window
Const GWL_USERDATA = (-21) ' Get user data associated witht he window.
'Styles
Const WS_BORDER = &H800000
Const WS_CAPTION = &HC00000
Const WS_CHILD = &H40000000
Const WS_CHILDWINDOW = WS_CHILD
Const WS_CLIPCHILDREN = &H2000000
Const WS_CLIPSIBLINGS = &H4000000
Const WS_DISABLED = &H8000000
Const WS_DLGFRAME = &H400000
Const WS_GROUP = &H20000
Const WS_MINIMIZE = &H20000000
Const WS_ICONIC = WS_MINIMIZE
Const WS_MAXIMIZE = &H1000000
Const WS_MAXIMIZEBOX = &H10000
Const WS_MINIMIZEBOX = &H20000
Const WS_POPUP = &H80000000
Const WS_SYSMENU = &H80000
Const WS_POPUPWINDOW = (WS_POPUP Or WS_BORDER Or WS_SYSMENU)
Const WS_THICKFRAME = &H40000
Const WS_SIZEBOX = WS_THICKFRAME
Const WS_TABSTOP = &H10000
Const WS_VISIBLE = &H10000000
'Extended Styles
Const WS_EX_ACCEPTFILES = &H10&
Const WS_EX_APPWINDOW = &H40000
Const WS_EX_CLIENTEDGE = &H200
Const WS_EX_COMPOSITED = &H2000000
Const WS_EX_CONTEXTHELP = &H400
Const WS_EX_CONTROLPARENT = &H10000
Const WS_EX_DLGMODALFRAME = &H1
Const WS_EX_LAYERED = &H80000
Const WS_EX_LAYOUTRTL = &H400000
Const WS_EX_LEFT = &H0
Const WS_EX_MDICHILD = &H40
Const WS_EX_NOACTIVATE = &H8000000
Const WS_EX_NOINHERITLAYOUT = &H100000
Const WS_EX_NOPARENTNOTIFY = &H4
Const WS_EX_TOPMOST = &H8
Const WS_EX_TOOLWINDOW = &H80
Const WS_EX_WINDOWEDGE = &H100
Const WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE Or WS_EX_CLIENTEDGE)
Const WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE Or WS_EX_TOOLWINDOW Or WS_EX_TOPMOST)
Const WS_EX_RIGHT = &H1000
Const WS_EX_RIGHTSCROLLBAR = &H0
Const WS_EX_RTLREADING = &H2000
Const WS_EX_STATICEDGE = &H20000
Const WS_EX_TRANSPARENT = &H20
Const WS_EX_LEFTSCROLLBAR = &H4000
Const WS_EX_LTRREADING = &H0
Private Sub Form_Load()
Dim lngCurStyle As Long
Dim lngNewStyle As Long
lngCurStyle = GetWindowLong(Form1.hWnd, GWL_EXSTYLE)
lngNewStyle = lngCurStyle Or WS_EX_TRANSPARENT
SetWindowLong Form1.hWnd, GWL_EXSTYLE, lngNewStyle
Form1.Visible = False
Form1.Visible = True
End Sub




January 14th, 2008 - 21:55
Hey, is it possible to make the transperancy blurry?
Thanks
January 14th, 2008 - 21:57
Hello, I am new at VB6 and at programing in general. Do you think is it possible to make windows transparency- transparent blurry in vb6?
Thanks.
July 27th, 2008 - 18:45
Hi jalaj,
You are discussing a Subject that I looked at with facination and always wanted to understand more. I have download some MS-Access Database with Startup Screens with Fading in and out and I am using it in some my Application. But the intricasies at large for me. I think I need to visit your site from the start of the page where you have started the Subject. The constant declarations are the most confusing part. I have good knowledge of VBA, hex codes but the usage is unknown. You have any suggestion for a book or something that I can use to learn on a paced manner?
Regards,
a.p.r. pillai
http://www.msaccesstips.com
September 29th, 2008 - 10:17
Hello I am getting this error
Can anybody is having solution?
PInvokeStackImbalance was detected
Message: A call to PInvoke function ‘WindowsApplication1!WindowsApplication1.Form1::GetWindowLong’ has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
Thanks in advance