What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake a form that displays the desktop image below itself so it looks transparent
Keywordsform, transparent, desktop
CategoriesControls, Tips and Tricks
When the program starts, it calls subroutine GrabScreen. That routine uses API functions to copy the desktop image into a PictureBox. It then copies the part of the image that lies under the form onto the form. This makes the form look transparent, although the difference is obvious if you move the form.
' Copy the desktop's image into the form.
Private Sub GrabScreen()
Dim desktop_rect As RECT
Dim desktop_win As Long
Dim desktop_dc As Long
Dim desktop_wid As Long
Dim desktop_hgt As Long
Dim x As Long
Dim y As Long
Dim wid As Long
Dim hgt As Long
Dim border_width As Single

    ' Get the desktop size in pixels.
    desktop_win = GetDesktopWindow()
    desktop_dc = GetDC(desktop_win)
    GetWindowRect desktop_win, desktop_rect
    desktop_wid = desktop_rect.Right
    desktop_hgt = desktop_rect.Bottom

    ' Get the printable area.
    x = 0
    wid = desktop_wid
    y = 0
    hgt = desktop_hgt
    picHidden.Width = ScaleX(Width - ScaleWidth + wid, _
        vbPixels, vbTwips)
    picHidden.Height = ScaleY(Height - ScaleHeight + hgt, _
        vbPixels, vbTwips)

    ' Copy the desktop's image.
    picHidden.AutoRedraw = True
    StretchBlt picHidden.hdc, x, y, wid, hgt, _
        desktop_dc, 0, 0, desktop_wid, desktop_hgt, _
    picHidden.Picture = picHidden.Image

    ' Release the desktop's device context.
    ReleaseDC desktop_win, desktop_dc

    ' Copy the part of the screen under the form onto the
    ' form.
    picHidden.Visible = False

    border_width = (Width - ScaleWidth) / 2
    PaintPicture picHidden.Picture, _
        0, 0, ScaleWidth, ScaleHeight, _
        Left + border_width, _
        Top + (Height - ScaleHeight - border_width), _
        ScaleWidth, ScaleHeight
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.