Copy the contents of the screen or the active window

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.

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' Return the current contents of the screen or the active window'' It works by simulating the typing of the Print-Screen key' (and Alt key if ActiveWindow is True), which dumps the screen' to the clipboard. The original contents of the clipboard is then' restored, but this action might affect the behavior of other' applications that are monitoring the clipboard.Function GetScreenBitmap(Optional ActiveWindow As Boolean) As Picture    ' save the current picture in the clipboard, if any    Dim pic As StdPicture    Set pic = Clipboard.GetData(vbCFBitmap)        ' Alt-Print Screen captures the active window only    If ActiveWindow Then        ' Press the Alt key        keybd_event vbKeyMenu, 0, 0, 0    End If    ' Press the Print Screen key    keybd_event vbKeySnapshot, 0, 0, 0    DoEvents    ' Release the Print Screen key    keybd_event vbKeySnapshot, 0, KEYEVENTF_KEYUP, 0    If ActiveWindow Then        ' Release the Alt key        keybd_event vbKeyMenu, 0, KEYEVENTF_KEYUP, 0    End If    DoEvents        ' return the bitmap now in the clipboard    Set GetScreenBitmap = Clipboard.GetData(vbCFBitmap)    ' restore the original contents of the clipboard    Clipboard.SetData pic, vbCFBitmap    End Function

Using the GenScreenBitmap is really straightforward:

' capture the contents of the screen to a picture boxSet Picture1.Picture = GenScreenBitmap()' capture the contents of the active window to a picture boxSet Picture2.Picture = GenScreenBitmap(True)

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS