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 a specific part of the desktop image
DescriptionThis example shows how to grab a specific part of the desktop image in Visual Basic 6. When a timer fires, the program grabs the 100x100 pixel area in the middle of the screen and displays it in its form.
Keywordsdesktop, grab pixels, image, background
CategoriesWindows
 
When the timer fires, its event handler calls subroutine GrabPixels. That routine calls the GetDesktopWindow API function to get the desktop's devicxe context (DC), and it uses GetWindowRect to get the desktop's bounds.

The program then resizes its form to fit the area it will grab and uses StretchBlt to copy part of the desktop's image onto the form.

 
Private Sub Timer1_Timer()
    DoEvents

    ' Grab the middle pixels.
    GrabPixels 50, 50
End Sub

Private Sub GrabPixels(ByVal area_wid As Long, ByVal _
    area_hgt As Long)
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

    ' 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

    ' Size the form.
    Width = ScaleX(area_wid, vbPixels, vbTwips) + Width - _
        ScaleWidth
    Height = ScaleY(area_hgt, vbPixels, vbTwips) + Height - _
        ScaleHeight

    ' Copy the middle of the desktop's image.
    StretchBlt _
        hdc, 0, 0, area_wid, area_hgt, _
        desktop_dc, _
        (desktop_wid - area_wid) \ 2, _
        (desktop_hgt - area_hgt) \ 2, _
        area_wid, area_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