简体   繁体   中英

What is the best way to take screenshots of a Window with C++ in Windows?

在 Windows 下使用 C++ 截取正在运行的应用程序的屏幕截图的最佳(最简单)方法是什么?

You have to get the device context of the window ( GetWindowDC() ) and copy image ( BitBlt() ) from it. Depending on what else you know about the application you will use different methods to find which window's handle to pass into GetWindowDC() .

GetDC(NULL) + BitBlt()

To capture translucent/alpha/layered windows, you must pass the CAPTUREBLT flag to BitBlt , if you do that, the cursor blinks, read this technet article to find out why.

On NT6+, you might be able to use the Magnification API to do what you want.

On the keybd_event function documentation it states that you can use it to take a screenshot and save it to the clipboard. For example:

keybd_event(VK_SNAPSHOT, 0, KEYEVENTF_SILENT, 0);

In my version (Visual Studio 2005 help installed on my computer) it states that you can take a screenshot of the whole desktop by setting the second parameter to 0, or a screen shot of just the current application by setting it to 1.

Taking it out of the clipboard buffer is left as an exercise for the reader.

However I'd think carefully before doing this as it will turf whatever image data was already present in the clipboard.

Here is an example code
You can do CaptureAnImage(GetDesktopWindow()); to make a screen capture.

Desktop is a "virtual" devices that interacts with user .Accordingly , it potentially contains privacy-related information . I suggest that a security check/warning prompt from OS is added when the executable tries to take screenshot unless the end user indicates explicitly that he(she) knows what is happening .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM