Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleGrab the desktop image
Keywordsdesktop image, background
CategoriesGraphics, Windows
 
Use GetDesktopWindow to get the hWnd of the desktop window. Use GetDC to get a device context for that window. Call GetWindowRect to see how big the desktop window is.

Then use StretchBlt to copy the desktop window's image onto the program's form. Use ReleaseDC to free the device context.

 
' 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

    ' 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
    Width = ScaleX(Width - ScaleWidth + wid, vbPixels, _
        vbTwips)
    Height = ScaleY(Height - ScaleHeight + hgt, vbPixels, _
        vbTwips)

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

    ' Release the desktop's device context.
    ReleaseDC desktop_win, desktop_dc
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated