This allows a VB program to capture either the screen or the program window.
It has been tested under Win95 and NT4.0. It derives from a routine by Dan Appleman (VisualBasic 5.0 Programmer’s Guide to the WIN32 API, page 303) which unfortunately does not work reliably under all conditions. Dan Appleman’s exhaustive preliminary tutorial, though, is all it takes to understand the code.
Original Author: Michel D. PRET
Inputs
mode – 0 = screen, 1 = window
a reference to an image control
Assumptions
‘Create a form, define two command controls and an image control, insert the following code:
Private Sub Command1_Click()
GetWindowSnapShot 0, Image1
End Sub
Private Sub Command2_Click()
GetWindowSnapShot 1, Image1
End Sub
Side Effects
no known side effect
API Declarations
Declare Function MapVirtualKey Lib “user32” Alias “MapVirtualKeyA” (ByVal wCode As Long, ByVal wMapType As Long) As Long
Declare Sub keybd_event Lib “user32” (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Declare Function GetVersionEx Lib “kernel32” Alias “GetVersionExA” _
(LpVersionInformation As OSVERSIONINFO) As Long
Public Const VK_MENU = &H12
Public Const KEYEVENTF_KEYUP = &H2
Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128 ‘ Maintenance string for PSS usage
End Type
Code
'Insert this in a module:
Public Sub GetWindowSnapShot(Mode As Long, ThisImage As Image)
' mode = 0 -> Screen snapshot
' mode = 1 -> Window snapshot
Dim altscan%, NT As Boolean, nmode As Long
NT = IsNT
If Not NT Then
If Mode = 0& Then Mode = 1& Else Mode = 0&
End If
If NT And Mode = 0 Then
keybd_event vbKeySnapshot, 0&, 0&, 0&
Else
altscan = MapVirtualKey(VK_MENU, 0)
keybd_event VK_MENU, altscan, 0, 0
DoEvents
keybd_event vbKeySnapshot, Mode, 0&, 0&
End If
DoEvents
ThisImage = Clipboard.GetData(vbCFBitmap)
keybd_event VK_MENU, altscan, KEYEVENTF_KEYUP, 0
End Sub
Public Function IsNT() As Boolean
Dim verinfo As OSVERSIONINFO
verinfo.dwOSVersionInfoSize = Len(verinfo)
If (GetVersionEx(verinfo)) = 0 Then Exit Function
If verinfo.dwPlatformId = 2 Then IsNT = True
End Function