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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/531684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:49:37  来源:igfitidea点击:

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

c++windowsscreenshot

提问by mgiza

What is the best (easiest) way to take a screenshot of an running application with C++ under Windows?

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

采纳答案by sharptooth

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().

您必须获取窗口 ( GetWindowDC())的设备上下文并BitBlt()从中复制图像 ( )。根据您对应用程序的了解,您将使用不同的方法来查找要传入的窗口句柄GetWindowDC()

回答by Daemin

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

keybd_event函数文档中,它声明您可以使用它来截取屏幕截图并将其保存到剪贴板。例如:

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.

但是,在执行此操作之前我会仔细考虑,因为它会覆盖剪贴板中已经存在的任何图像数据。

回答by sflee

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

这是 您可以用来制作屏幕截图的示例代码
CaptureAnImage(GetDesktopWindow());

回答by Daniel

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 .

桌面是与用户交互的“虚拟”设备。因此,它可能包含与隐私相关的信息。我建议在可执行文件尝试截取屏幕截图时添加来自操作系统的安全检查/警告提示,除非最终用户明确表示他(她)知道发生了什么。