Jalaj

February 2, 2007

WinAPI : Making Form Transparent

Filed under: Application, Visual Basic, WinAPI — Jalaj @ 12:37 pm

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.

invform.jpg

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

5 Comments »

  1. [...] Filed under: Application, Visual Basic, WinAPI — Jalaj @ 5:38 am Previous post WinAPI : Making Form Transparent described how to make a form transparent. A similar problem that is often asked is how to modify [...]

    Pingback by Modifying Minimize, Maximize buttons during runtime « Jalaj — February 12, 2007 @ 1:02 pm

  2. [...] Now as mentioned above the function works only on Layered windows for which we will require to change the Window’s Extended style to include WS_EX_LAYERED using GetWindowLong and SetWindowLong as already descibed in pervious posts Modifying Minimize, Maximize buttons during runtime and WinAPI : Making Form Transparent. [...]

    Pingback by WinAPI : Making Forms Translucent « Jalaj — February 23, 2007 @ 6:03 am

  3. Hey, is it possible to make the transperancy blurry?

    Thanks

    Comment by krasav4ik82 — January 14, 2008 @ 9:55 pm

  4. 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.

    Comment by krasav4ik82 — January 14, 2008 @ 9:57 pm

  5. [...] sends you to my post describing an application for creating Pixel Art using a BMP picture. And yes making a form transparent was something that I used to search for before starting this blog and after starting learning [...]

    Pingback by Year One Digest « Jalaj — February 8, 2008 @ 7:22 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.