How to Copy Entire Screen into a Picture Box in Visual Basic

Last reviewed: December 8, 1995
Article ID: Q140885
The information in this article applies to:

- Professional and Enterprise Editions of Microsoft Visual Basic,

  16-bit only, for Windows, version 4.0

SUMMARY

Using the Windows API call BitBlt, you can capture the entire Microsoft Windows screen and place the image into a Microsoft Visual Basic for Windows picture box.

First, get the handle to the desktop. Then use the desktop window handle to get the handle to the desktop's device context (hDC). Finally, use the Windows API call BitBlt to copy the screen into the Picture property of a Visual Basic for Windows picture box control.

MORE INFORMATION

Step-by-Step Example

  1. Start Visual Basic for Windows (VB.EXE). Form1 is created by default.

  2. Add a picture box (Picture1) to Form1.

  3. Set the following properties:

       Control    Property      Value
       ------------------------------
       Picture1   AutoRedraw    True
       Picture1   Visible       False
    
    

  4. Add the following code to your .BAS module:

Type lrect
   left As Integer
   top As Integer
   right As Integer
   bottom As Integer
End Type
Private Declare Function GetDesktopWindow Lib "user" () As Integer
Private Declare Function GetDC Lib "user" (ByVal hWnd%) As Integer
' Enter the following Declare on one, single line:
Private Declare Function BitBlt Lib "GDI" (ByVal hDestDC%, ByVal X%, ByVal
Y%,
   ByVal nWidth%, ByVal nHeight%, ByVal hSrcDC%, ByVal XSrc%,
   ByVal YSrc%, ByVal dwRop&) As Integer
' Enter the following Declare on one, single line:
Private Declare Function ReleaseDC Lib "User"(ByVal hWnd As Integer, ByVal
hDC As Integer) As Integer
Private Declare Sub GetWindowRect Lib "User" (ByVal hWnd%, lpRect As lrect)
   Global TwipsPerPixel As Single

  • Add the following code to the Form1 Click event procedure:

       Private Sub Form_Click ()
          Call GrabScreen
       End Sub
    
       Public Sub GrabScreen ()
    
          Dim winSize As lrect
    
          ' Assign information of the source bitmap.
          ' Note that BitBlt requires coordinates in pixels.
          hwndSrc% = GetDesktopWindow()
          hSrcDC% = GetDC(hwndSrc%)
          XSrc% = 0: YSrc% = 0
          Call GetWindowRect(hwndSrc%, winSize)
          nWidth% = winSize.right             ' Units in pixels.
          nHeight% = winSize.bottom           ' Units in pixels.
    
          ' Assign informate of the destination bitmap.
          hDestDC% = Form1.Picture1.hDC
          x% = 0: Y% = 0
    
          ' Set global variable TwipsPerPixel and use to set
          ' picture box to same size as screen being grabbed.
          ' If picture box not the same size as picture being
          ' BitBlt'ed to it, it will chop off all that does not
          ' fit in the picture box.
          GetTwipsPerPixel
          Form1.Picture1.Top = 0
          Form1.Picture1.Left = 0
          Form1.Picture1.Width = (nWidth% + 1) * TwipsPerPixel
          Form1.Picture1.Height = (nHeight% + 1) * TwipsPerPixel
    
          ' Assign the value of the constant SRCOPYY to the Raster operation.
          dwRop& = &HCC0020
    
          ' NOTE: function call must be on one line:
          Suc% = BitBlt(hDestDC%, x%, Y%, nWidth%, nHeight%,
                        hSrcDC%, XSrc%, YSrc%, dwRop&)
    
          ' Release the DeskTopWindow's hDC to Windows.
          ' Windows may hang if this is not done.
          Dmy% = ReleaseDC(hwndSrc%, hSrcDC%)
    
          'Make the picture box visible.
          Form1.Picture1.Visible = True
       End Sub
    
       Public Sub GetTwipsPerPixel ()
          ' Set a global variable with the Twips to Pixel ratio.
          Form1.ScaleMode = 3
          NumPix = Form1.ScaleHeight
          Form1.ScaleMode = 1
          TwipsPerPixel = Form1.ScaleHeight / NumPix
       End Sub
    
    

  • Run the program. Click Form1.

  • Using the mouse, change the size of the form to see more of the picture box. With a little work, you can use this as a screen saver program.


  • Additional reference words: 1.00 4.00 print printer vb4win vb416
    KBCategory: kbui kbprg kbcode
    KBSubcategory: APrgWindow APrgGrap



    THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

    Last reviewed: December 8, 1995
    © 1998 Microsoft Corporation. All rights reserved. Terms of Use.