Library code snippets

Making Windows Transparent

Here's some code demonstrating how to make a form in VB transparent!

Option Explicit

Private Declare Function SendMessage Lib "User32" Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
 lParam As Any) As Long
Private Declare Sub ReleaseCapture Lib "User32" ()

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 Long, ByVal bAlpha As Byte, _
 ByVal dwFlags As Long) As Long

Const WM_NCLBUTTONDOWN = &HA1
Const HTCAPTION = 2

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


Private Sub Form_Load()
DarkMe
End sub

Public Function DarkMe()
Dim rtn As Long
rtn = GetWindowLong(hWnd, GWL_EXSTYLE)
rtn = rtn Or WS_EX_LAYERED
SetWindowLong hWnd, GWL_EXSTYLE, rtn
SetLayeredWindowAttributes hWnd, 0, 200, LWA_ALPHA
End Function

Comments

  1. 02 Feb 2008 at 16:17

    As you will see, if you change the window style in the form load event, then the bitblting becomes very choppy.  If you wait and change the window style in the form unload event, just before the fade out effect, then you will see an ugly flicker.

    ==================================================================== 

    The only solution I find is capture the image of the window to the clipboard, and load a wide form with BorderStyle = 0 and put the image of the main form, unload the main form, and do the effect of transparency with this form, the capturewindow image code is how this:

     Option Explicit

    Public Type RECT_Type

       left As Long
       top As Long
       right As Long
       bottom As Long

    End Type

    'The following Public Declare statements are case sensitive.

    Public Declare Function GetActiveWindow Lib "User32" () As Long
    Public Declare Function GetDesktopWindow Lib "User32" () As Long
    Public Declare Sub GetWindowRect Lib "User32" (ByVal Hwnd As Long, _
                                        lpRect As RECT_Type)
    Public Declare Function GetDC Lib "User32" (ByVal Hwnd As Long) As Long
    Public Declare Function CreateCompatibleDC Lib "Gdi32" (ByVal hdc As Long) _
                                        As Long
    Public Declare Function CreateCompatibleBitmap Lib "Gdi32" (ByVal hdc _
                                        As Long, ByVal nWidth As Long, _
                                        ByVal nHeight As Long) As Long
    Public Declare Function SelectObject Lib "Gdi32" (ByVal hdc As Long, _
                                        ByVal hObject As Long) As Long
    Public Declare Function BitBlt Lib "Gdi32" (ByVal hDestDC As Long, _
                                        ByVal X As Long, ByVal Y _
                                        As Long, ByVal nWidth As Long, _
                                        ByVal nHeight As Long, _
                                        ByVal hSrcDC As Long, _
                                        ByVal XSrc As Long, _
                                        ByVal YSrc As Long, _
                                        ByVal dwRop As Long) As Long
    Public Declare Function OpenClipboard Lib "User32" (ByVal Hwnd As Long) As Long
    Public Declare Function EmptyClipboard Lib "User32" () As Long
    Public Declare Function SetClipboardData Lib "User32" (ByVal wFormat As Long, _
                                        ByVal hMem As Long) As Long
    Public Declare Function CloseClipboard Lib "User32" () As Long
    Public Declare Function ReleaseDC Lib "User32" (ByVal Hwnd As Long, _
                                        ByVal hdc As Long) As Long
    Public Declare Function DeleteDC Lib "Gdi32" (ByVal hdc As Long) As Long

    Global Const SRCCOPY = &HCC0020
    Global Const CF_BITMAP = 2

    Public Function CaptureWindowArea(AccessHwnd As Long)
       Dim DeskHwnd As Long
       Dim hdc As Long
       Dim hdcMem As Long
       Dim rect As RECT_Type
       Dim junk As Long
       Dim fwidth As Long, fheight As Long
       Dim hBitmap As Long

       'DoCmd.Hourglass True

       '---------------------------------------------------
       ' Get window handle to Windows and Microsoft Access
       '---------------------------------------------------
       DeskHwnd = GetDesktopWindow()
       AccessHwnd = GetActiveWindow()

       '---------------------------------------------------
       ' Get screen coordinates of Microsoft Access
       '---------------------------------------------------
       Call GetWindowRect(AccessHwnd, rect)
       fwidth = rect.right - rect.left
       fheight = rect.bottom - rect.top

       '---------------------------------------------------
       ' Get the device context of Desktop and allocate memory
       '---------------------------------------------------
       hdc = GetDC(DeskHwnd)
       hdcMem = CreateCompatibleDC(hdc)
       hBitmap = CreateCompatibleBitmap(hdc, fwidth, fheight)

       If hBitmap <> 0 Then
          junk = SelectObject(hdcMem, hBitmap)

          '---------------------------------------------
          ' Copy the Desktop bitmap to memory location
          ' based on Microsoft Access coordinates.
          '---------------------------------------------
          junk = BitBlt(hdcMem, 0, 0, fwidth, fheight, hdc, rect.left, _
                        rect.top, SRCCOPY)

          '---------------------------------------------
          ' Set up the Clipboard and copy bitmap
          '---------------------------------------------
          junk = OpenClipboard(DeskHwnd)
          junk = EmptyClipboard()
          junk = SetClipboardData(CF_BITMAP, hBitmap)
          junk = CloseClipboard()
       End If

       '---------------------------------------------
       ' Clean up handles
       '---------------------------------------------
       junk = DeleteDC(hdcMem)
       junk = ReleaseDC(DeskHwnd, hdc)

       'DoCmd.Hourglass False

    End Function


          
     

  2. 07 Mar 2007 at 10:26
    Im a computer student and I have a case study which is i will connect my databased to Media Player anyone can elp me please?..
  3. 12 Aug 2005 at 02:59

    Thanks for the code. Works a treat, but the form is not entirely transparent - just semi-transparent

  4. 02 Aug 2004 at 08:39

    Yes, thanks for the help.  The following code is an example of the problem:


    Option Explicit


    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.dll" (ByVal _
    hwnd As Long, ByValcrKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Boolean


    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
    (ByVal hwnd As Long, ByVal nIndex As Long) As Long


    Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X _
    As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal _
    hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long


    Private Const SRCCOPY = &HCC0020
    Private Const LWAALPHA = 2
    Private Const GWL
    EXSTYLE = (-20)
    Private Const WSEXLAYERED = &H80000


    Dim iLeft As Integer


    Private Sub FormLoad()
       'Change window style to layered
       'Comment out the code for smooth scrolling (disables fade effect)
       SetWindowLong Form1.hwnd, GWL
    EXSTYLE, GetWindowLong(Form1.hwnd, _
       GWLEXSTYLE) Or WSEXLAYERED
       SetLayeredWindowAttributes Form1.hwnd, 0, 255, LWA
    ALPHA
    End Sub


    Private Sub FormUnload(Cancel As Integer)
       'Can't change the window style here, because it creates flicker
       'Cut and paste the Form
    Load code here to see    


       'Fade effect
       Timer1.Enabled = False
       Dim i As Integer
       For i = 255 To 0 Step -5
           SetLayeredWindowAttributes Me.hwnd, 0, i, LWA_ALPHA
           DoEvents
       Next
    End Sub


    Private Sub Timer1_Timer()
       'BitBLT scrolling
       iLeft = iLeft + 1
       If iLeft = ((Form1.Width + Picture1.Width) / Screen.TwipsPerPixelX) Then _
       iLeft = 0
       BitBlt Picture1.hDC, 0, 0, Picture1.Width, Picture1.Height, Form1.hDC, _
       iLeft, 0, SRCCOPY
    End Sub


    To use the code, add a small picture box to your form and add a timer (enable it, and set the interval to 1).  Then load a large picture into the form (the form1.picture property)


    As you will see, if you change the window style in the form load event, then the bitblting becomes very choppy.  If you wait and change the window style in the form unload event, just before the fade out effect, then you will see an ugly flicker.  


    I would like to change the window style to layered, only when i need it at form unload, but would like to avoid the flicker problem.


    Thanks for the help,
    -Joe

  5. 30 Jul 2004 at 18:54

    if possible, can u send the code to me? maybe i wil help u to solve the problem


    again, when u use the code, it wil slow ur application down.

  6. 30 Jul 2004 at 13:49

    The problem with the putting the code in the form_load event is that the form will be significantly slowed down.  Since my application uses many bitblt's to the form, setting the form to a layered, but 255 opacity, destroyed the bitblting effects entirely.


    So now, i change the form to a layered in the form unload event, but I still have the associated flicker of changing the window style outside of the form_load event.


    Any ideas?

  7. 02 Jul 2004 at 23:16

    I really want to thank you for sharing your code for making a form true transparent. your the best.


    thank you

  8. 20 Feb 2004 at 22:22
    thx for the opinion....
  9. 20 Feb 2004 at 11:07
    for better result i had to do like follows (on my pc the form was flickering while moving until i resize it)
    put the code in Form_Load event
     SetWindowLong Me.hwnd, GWL_EXSTYLE, GetWindowLong(Me.hwnd, GWL_EXSTYLE) Or WS_EX_LAYERED
     SetLayeredWindowAttributes Me.hwnd, 0, 255, LWA_ALPHA   'make it not transparent
    and no need to resize it
    and set the transparency when you want in the code
    for instance at the unload
    Private Sub Form_Unload(Cancel As Integer)
    Dim i As Integer
     For i = 255 To 0 Step -20
       SetLayeredWindowAttributes Me.hwnd, 0, i, LWA_ALPHA
       DoEvents
     Next
    End Sub
    flicker  free


  10. 01 Jan 1999 at 00:00

    This thread is for discussions of Making Windows Transparent.

Leave a comment

Sign in or Join us (it's free).

Yew Chin Sing

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!