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 after a delay
DescriptionThis example shows how to grab the desktop image after a delay in Visual Basic 6. It uses a Timer to control when the grab begins.
Keywordsdesktop image, background, delay
CategoriesGraphics, Windows
 
I needed this feature to capture an image of the Task Manager.

When the user clicks the Go button, the program sets the tmrGrabScreen Timer's Interval property to the number of seconds indicated and enables the Timer.

When the Timer fires, it disables itself and calls subroutine GrabScreen.

 
Private Sub cmdGo_Click()
    Screen.MousePointer = vbHourglass

    tmrGrabScreen.Interval = CSng(txtDelay.Text) * 1000
    tmrGrabScreen.Enabled = True
End Sub

Private Sub tmrGrabScreen_Timer()
    tmrGrabScreen.Enabled = False
    GrabScreen txtFile.Text
    Screen.MousePointer = vbDefault
End Sub
 
Subroutine GrabScreen uses the GetDesktopWindow API function to get the hWnd of the desktop window. It uses GetDC to get a device context for that window and calls GetWindowRect to see how big the desktop window is.

It then uses StretchBlt to copy the desktop window's image onto the hidden PictureBox picHidden. It uses ReleaseDC to free the desktop's device context and finally saves the image in picHidden into a bitmap file.

 
' Copy the desktop's image into the file.
Private Sub GrabScreen(ByVal file_name As String)
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
    picHidden.Width = ScaleX(wid, vbPixels, vbTwips)
    picHidden.Height = ScaleY(hgt, vbPixels, vbTwips)

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

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

    ' Save the result into the file.
    SavePicture picHidden.Picture, file_name
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated