Visual Basic Bitmap Routines
BitBlt: Mimicking the PrintScreen Function
Posted:   Thursday December 26, 1996
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6 and VB3, VB4-16 with appropriate declarations
Developed with:   VB4-32, Windows 95
OS restrictions:   None
Author:   VBnet - Randy Birch


BitBlt: Mimicking the PrintScreen Function
BitBlt: Mimicking PrintScreen to Create a 'PrintForm'
CreateEnhMetaFile: Saving a PrintScreen as a Windows Enhanced Metafile
InflateRect: Highlighting External Windows
keybd_event: Calling Windows' PrintScreen Function
OleCreatePictureIndirect: Mimicking PrintScreen Using OLE

The following code will copy the contents of the desktop (the screen) into a picture box on a form.
 BAS Module Code

 Form Code
To a form add a command button (Command1) and a picture (Picture1), along with the following code:

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Private Declare Function BitBlt Lib "gdi32" _ 
  (ByVal hDCDest As Long, ByVal XDest As Long, _ 
   ByVal YDest As Long, ByVal nWidth As Long, _
   ByVal nHeight As Long, ByVal hDCSrc As Long, _
   ByVal XSrc As Long, ByVal YSrc As Long, _
   ByVal dwRop As Long) As Long

Private Declare Function GetDesktopWindow Lib "user32" () As Long

Private Declare Function GetWindowDC Lib "user32" _
  (ByVal hWnd As Long) As Long

Private Declare Function ReleaseDC Lib "user32" _
  (ByVal hWnd As Long, ByVal hdc As Long) As Long

Private Sub Command1_Click()


End Sub

Private Sub PrintScreen()

   Dim hWndDesk As Long
   Dim hDCDesk As Long
   Dim LeftDesk As Long
   Dim TopDesk As Long
   Dim WidthDesk As Long
   Dim HeightDesk As Long
  'define the screen coordinates (upper 
  'corner (0,0) and lower corner (Width, Height)
   LeftDesk = 0
   TopDesk = 0
   WidthDesk = Screen.Width \ Screen.TwipsPerPixelX
   HeightDesk = Screen.Height \ Screen.TwipsPerPixelY
  'get the desktop handle and display context
   hWndDesk = GetDesktopWindow()
   hDCDesk = GetWindowDC(hWndDesk)
  'copy the desktop to the picture box
   Call BitBlt(Picture1.hdc, 0, 0, _
              WidthDesk, HeightDesk, hDCDesk, _
              LeftDesk, TopDesk, vbSrcCopy)

   Call ReleaseDC(hWndDesk, hDCDesk)

End Sub
The real keyboard PrintScreen command cannot be activated using SendKeys, nor by setting any combination of key codes in Visual Basic without the use of APIs. However, it can be called to copy the screen to the clipboard (see links at the top of this page).

Also see OleCreatePictureIndirect. In addition, the Bitmap page also contains the API code to call the real PrintScreen routine. See the links at the top of this page.


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy




Hit Counter