The standard way to copy the contents of the screen or a window to a picture box requires you to use a lot of API functions, such as BitBlt. A simpler approach is possible, though: you just have to simulate the typing of the Print Screen key (or Alt+Print Screen if you want to copy the contents of the active window) and then retrieve the contents of the Clipboard.
Unfortunately, you can't use SendKeys to press those keys, and you've to resort to the keybd_event API function. Here is a function that encapsulates all the low level details and also correctly restore the original contents of the Clipboard. |
Click here to copy the following block | Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _ ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long) Private Const KEYEVENTF_KEYUP = &H2
Function GetScreenBitmap(Optional ActiveWindow As Boolean) As Picture Dim pic As StdPicture Set pic = Clipboard.GetData(vbCFBitmap) If ActiveWindow Then keybd_event vbKeyMenu, 0, 0, 0 End If keybd_event vbKeySnapshot, 0, 0, 0 DoEvents
keybd_event vbKeySnapshot, 0, KEYEVENTF_KEYUP, 0 If ActiveWindow Then keybd_event vbKeyMenu, 0, KEYEVENTF_KEYUP, 0 End If DoEvents Set GetScreenBitmap = Clipboard.GetData(vbCFBitmap) Clipboard.SetData pic, vbCFBitmap End Function |
Using the GenScreenBitmap is really straightforward: |
|