Jalaj

February 4, 2007

WinAPI : Making Forms Translucent

Filed under: Application, Visual Basic, WinAPI — Jalaj @ 10:43 am

Your Application Window hides everything behind it, which is the default behaviour of all windows. Making the window transparent on the other hand hides the form and all static controls as label over it, to make the background visible. Well there’s a third way! using SetLayeredWindowAttributes function. SetLayeredWindowAttributes function enables to set the form’s opacity such that the form and the controls contained in it as well as the background are visible. The function is available on Windows 2000 and above versions and works on Layered Windows.

Now what’s the Layered Window?

All windows by default are assigned WS_OVERLAPPED style, making it hide everything that’s behind it. Contrast to it is the WS_EX_LAYERED style which enables accessing other windows behind it.

Let’s get into the details of the SetLayeredWindowAttributes function. The function takes four parameters, first of which is the handle to the window on which function is to be applied. The second and the third parameter are respectively Color key and alpha, only one of which is to be given. The fourth parameter is the flag which specifies whether second or third parameter is to be considered while applying the function and can be one of the constants defined below.

For now we will focus on changing the opacity of the form which is achieved by populating the third parameter bAlpha which takes value from 0 (completely transparent) to 255 (Opaque)

Private Declare Function SetLayeredWindowAttributes Lib "user32" _
(ByVal hWnd As Long, ByVal crKey As Byte, ByVal bAlpha As Byte, _
    ByVal dwFlags As Long) As Long

Const LWA_COLORKEY = &H1
Const LWA_ALPHA = &H2

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.

Create a new project and make the required function and constant declarations.

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

Private Declare Function SetLayeredWindowAttributes Lib "user32" _
(ByVal hWnd As Long, ByVal crKey As Byte, ByVal bAlpha As Byte, _
    ByVal dwFlags As Long) As Long

Const GWL_EXSTYLE = (-20)
Const WS_EX_LAYERED = &H80000
Const LWA_COLORKEY = &H1
Const LWA_ALPHA = &H2

Public lngAlpha As Long

lngAlpha variable as declared above will take values from 0 to 255 according to which the layered window’s attributes would be changed. We would set the value of alpha as 150 in the load event of the form. Before applying the alpha we would require to change the window’s extended style to include WS_EX_LAYERED style, and then apply the SetLayeredWindowAttributes function.

Private Sub Form_Load()

    lngAlpha = 150

    Dim lngCurStyle As Long
    Dim lngNewStyle As Long

    lngCurStyle = GetWindowLong(Form1.hWnd, GWL_EXSTYLE)
    lngNewStyle = lngCurStyle Or WS_EX_LAYERED

    SetWindowLong Form1.hWnd, GWL_EXSTYLE, lngNewStyle

    SetLayeredWindowAttributes Form1.hWnd, 0, lngAlpha, LWA_ALPHA

End Sub

alphaform.jpg

Add a control or two on the form and that’s all. Execute to see the effect. Now let’s make opacity changes dynamically, say when mouse is moved with Left button and Shift key pressed the opacity increases and similarly decreases with Right button and Shift key.

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

    If Shift = 1 Then
        If Button = 2 And lngAlpha > 25 Then
            lngAlpha = lngAlpha - 5
        End If
       If Button = 1 And lngAlpha < 220 Then
            lngAlpha = lngAlpha + 5
        End If

        SetLayeredWindowAttributes Form1.hWnd, 0, lngAlpha, LWA_ALPHA

    End If

End Sub

That’s all with opacity. Let’s take the other option i.e. using color key next time.

4 Comments »

  1. [...] under: Application, Visual Basic, WinAPI — Jalaj @ 6:58 am In the previous post WinAPI : Making Forms Translucent we took one way of using the SetLayeredWindowAttributes function. Let’s take here the other [...]

    Pingback by Form With a Hole « Jalaj — February 13, 2007 @ 11:52 am

  2. [...] interface differs from that of the earlier versions in that it now has translucent windows. Now when you want to search your desktop it no longer opens the browsers window, but a [...]

    Pingback by Google Desktop Now in Hindi « Jalaj — April 27, 2007 @ 11:18 am

  3. [...] WinAPI : Making Forms Translucent [...]

    Pingback by Just Looking Back « Jalaj — May 28, 2007 @ 6:26 am

  4. [...] The Blog Revisited - 5 By now I had jumped into using WinAPI function to the level I never imagined that I would get into… I always had some queries as how not to make rectangle forms etc that always got answer that can only happen on C++… was it totally true… NO that’s what I realized when dug into MSDN library and the answer was manipulating Device Context, the virtual device that we always write on to in Windows. For starting on it see WinAPI : Starting with Device Context How do we make a form transparent, now doing it seemed like a child’s play. See it for yourself at WinAPI : Making Form Transparent or how to make it translucent at WinAPI : Making Forms Translucent. [...]

    Pingback by The Blog Revisited - 5 « Jalaj — November 16, 2007 @ 4:49 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.