获取其中运行 OpenGL 的子窗口的屏幕截图(Windows) [英] Getting screenshot of a child window running OpenGL in it (Windows)

查看:36
本文介绍了获取其中运行 OpenGL 的子窗口的屏幕截图(Windows)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带孩子的主窗口.我需要以编程方式截取屏幕截图以裁剪并将它们绘制回我的主窗口.我这样做的方式是:

I have a main window with children. I need to take screenshots programmatically to crop and draw them back on my main window. The way I do this is:

HDC hDC = GetWindowDC(hWnd);
HDC memDC = CreateCompatibleDC(hDC);
HBITMAP memBM = CreateCompatibleBitmap(hDC, Width, Height);
HBITMAP OldBM = (HBITMAP)::SelectObject(memDC, memBM );
BitBlt(memDC, 0, 0, Width, Height , hDC, BEGINX, BEGINY, SRCCOPY);
int Bpp = GetDeviceCaps(hDC,BITSPIXEL);
int size = Bpp/8 * ( Width * Height );
BYTE *lpBits = new BYTE[size];
GetBitmapBits(memBM, size, lpBits);

但这并没有捕获子窗口的 OpenGL 部分,而只是在 OGL 渲染应该出现的区域中绘制空白.

But this doesn't capture the OpenGL section of the child windows, instead it just draws blank white in the area where OGL render is supposed to be.

推荐答案

我假设您使用的是 win 平台.

I assume you are on win platform.

即使你没有子窗口的源代码,你也可以渲染到位图.

I thing you can render to bitmap even if you do not have the source code for child window.

  1. 首先获取子窗口的正确句柄
    • 正确的句柄搜索非常棘手,因为在 win 中几乎所有东西都是一个窗口(所以有数百个句柄......)
  • 并非所有窗口都能做到这一点!!!
  • 最小化/恢复胜利之类的

请注意,这不是一个非常稳定的过程(OpenGL 可能会在上下文句柄更改期间产生冲突),并且在某些情况下您甚至无法更改上下文句柄

Beware this is not very stable process (OpenGL can make conflicts during context handles change) and in some cases you even cannot change handles to contexts

更安全的是自己截取桌面.

More safe is to screenshot desktop it self.

TCanvas *scr=new TCanvas();
scr->Handle=GetDC(GetDesktopWindow());
// copy scr to your bitmap (do not forget to resize bitmap)

警告不要过于频繁地获取/创建/释放上下文或赢得句柄,因为它有一个小错误(有时 winapi 函数停止返回正确的处理程序值)更好的方法是获得一次句柄并希望没有人关闭窗口......孩子获胜,您确切地知道何时获得新的处理程序)对于 IsWindow() 函数也是如此.

Warning do not obtain/create/release contexts or win handles too often because its a little buggy (sometimes winapi functions stops returning correct handler values) better way is obtain handle once and hope that no one closes window ... for your child wins you know exactly when to get new handler) That is true also for IsWindow() function.

有关更多信息,请查看:

For more info take a look at:

这篇关于获取其中运行 OpenGL 的子窗口的屏幕截图(Windows)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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