特定窗口C ++的屏幕截图 [英] Screen capture of specific window c++

查看:106
本文介绍了特定窗口C ++的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想截取某些特定窗口(例如计算器)的屏幕截图.

I want to take a screenshot of some specific window (e.g calculator).

这是我根据讨论内容编写的代码:

Here is the code I have written according to this discussion:

// Get the window handle of calculator application.
HWND hWnd = ::FindWindow(0, _T("Calculator"));
RECT r;
GetWindowRect(hWnd, &r);
int x[2]; int y[2];
x[0] = r.top;  x[1] = r.bottom;
y[0] = r.left; y[1] = r.right;

HDC     hScreen = GetWindowDC(hWnd);
HDC     hDC = CreateCompatibleDC(hScreen);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, y[1] - y[0], x[1] - x[0]);
HGDIOBJ old_obj = SelectObject(hDC, hBitmap);
BitBlt(hDC, 0, 0, y[1] - y[0], x[1] - x[0], hScreen, y[0], x[0], SRCCOPY);

然后,我将位图另存为.bmp图像.

Afterwards, I save the bitmap as a .bmp image.

结果具有正确的计算器窗口大小和位置,但生成的bmp全部为黑色.

The result has correct size and position of the calculator window but the resulting bmp is all black.

我尝试对完整的桌面进行屏幕截图,然后将计算器部分切掉,并且工作正常.但是我希望能够制作该窗口的屏幕截图,即使该窗口已最小化或被另一个窗口覆盖.

I tried to screenshot the full desktop and then cut the calculator part and that worked. But I want to be able to make a screenshot of the window even if it is minimized or covered by another window.

有什么想法为什么该代码不起作用或还有其他方法吗?

Any ideas why this code is not working or is there any other way to do it?

谢谢.

推荐答案

一种可供考虑的方法是

A method for consideration is CreateForWindow.

在目标窗口可移动的情况下,另一个角度将其重新定位到当前桌面的左上角.捕获整个屏幕,然后,根据您的了解,窗口的尺寸,将其裁剪为那些尺寸.
如果安全程序可以暂停或终止,目标窗口不是隐藏的,也不是组合在上面的评论中讨论的方法,或者可以暂时禁用Aero.

Another angle, given the target window is movable, is relocating it to the top left corner of the current desktop. Perform a capture of the entire screen, and then, given you know the dimensions of the window, crop it to those.
This has a better chance of success provided security programs can be suspended or terminated, the target window isn't cloaked, or composited in the way as discussed in the above comments, or when Aero can be temporarily disabled.

这篇关于特定窗口C ++的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆