What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleHide and then grab the desktop image after a delay
DescriptionThis example shows how to hide and then grab the desktop image after a delay in Visual Basic 6.
Keywordsdesktop image, background, delay
CategoriesGraphics, Windows
Thanks to Galen Somerville.

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

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

Private Sub cmdGo_Click()

    If CSng(txtDelay.Text) = 0 Then
        tmrGrabScreen.Interval = 1
        tmrGrabScreen.Interval = CSng(txtDelay.Text) * 1000
    End If
    tmrGrabScreen.Enabled = True
End Sub

Private Sub tmrGrabScreen_Timer()
    tmrGrabScreen.Enabled = False
    GrabScreen txtFile.Text
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, _
    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.