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

查看:481
本文介绍了获取在其中运行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. 首先获得正确的句柄到子窗口
    • 正确的句柄搜索非常棘手,因为获胜的几乎所有东西都是一个窗口(因此有数百个句柄...)
  1. First obtain correct Handle to the child window
    • correct handle search is very tricky because almost everything in win is a window (so there are hundreds handles...)
  • 并非所有Windows都能做到!
  • 最小化/恢复胜利之类的东西

请注意,这不是一个非常稳定的过程(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天全站免登陆