简体   繁体   English

在 Windows 中使用 C++ 截取窗口屏幕截图的最佳方法是什么?

[英]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.您必须获取窗口的设备上下文( GetWindowDC() )并从中复制图像( BitBlt() )。 Depending on what else you know about the application you will use different methods to find which window's handle to pass into GetWindowDC() .根据您对应用程序的了解,您将使用不同的方法来查找要传递给GetWindowDC()窗口句柄。

GetDC(NULL) + BitBlt() 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.要捕获半透明/alpha/分层窗口,您必须将CAPTUREBLT标志传递给BitBlt ,如果这样做,光标会闪烁,请阅读此技术网文章以找出原因。

On NT6+, you might be able to use the Magnification API to do what you want.在 NT6+ 上,您或许可以使用Magnification API来做您想做的事。

On the keybd_event function documentation it states that you can use it to take a screenshot and save it to the clipboard.keybd_event函数文档中,它声明您可以使用它来截取屏幕截图并将其保存到剪贴板。 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.在我的版本(安装在我的计算机上的 Visual Studio 2005 帮助)中,它指出您可以通过将第二个参数设置为 0 来截取整个桌面的屏幕截图,或者通过将其设置为 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());你可以做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 .我建议在可执行文件尝试截取屏幕截图时添加来自操作系统的安全检查/警告提示,除非最终用户明确表示他(她)知道发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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