internet-explorer – 如何获取在Internet Explorer浏览器帮助程序对象中呈现网页的窗口的HWND

我正在Visual C中编写一个浏览器助手对象,需要对渲染的网页进行全长截图.目前,我正在我的BHO中捕获DocumentComplete事件.我得到了浏览器的hWnd,并且可以截取它的截图,但这不是我真正需要的.我真的需要渲染页面的窗口(不是带滚动条的框架).

此外,我正在遇到一种竞争条件,当我拍摄屏幕截图时,浏览器可能还没有渲染页面.我已经添加了对UpdateWindow的调用,但即使在返回true之后,有时窗口还没有呈现.

所以,总结一下:

1)如何获取渲染的HTML窗口的hWnd
2)BHO可以截取屏幕截图的适当事件是什么?

编辑:

基于以下答案,我创建了这段代码:

        MSHTML::IHTMLRectPtr pRect2 = pBody2->getBoundingClientRect();

        long width = pRect2->right;
        long height = pRect2->bottom;

        RECTL imageRect = { 0, 0, width, height };

        IViewObject *pViewObject = NULL;
        pHtmlDocument2->QueryInterface(IID_IViewObject, (void**)&pViewObject);

        HDC hdcScreen = GetDC(NULL);
        HDC hCompDc = CreateCompatibleDC(hdcScreen);

        pViewObject->Draw(DVASPECT_CONTENT, -1, NULL, NULL, NULL, hCompDc, NULL, &imageRect, NULL, 0);

        HBITMAP hbmp = CreateCompatibleBitmap(hCompDc, imageRect.right - imageRect.left, imageRect.bottom - imageRect.top);
        SelectObject(hCompDc, hbmp);

        Bitmap *image = new Bitmap(hbmp, NULL);

        long bitLength = (imageRect.right - imageRect.left) * (imageRect.bottom - imageRect.top) * 4;
        byte *bits = (byte*)malloc(bitLength);
        memset(bits, 0, bitLength);

        BITMAPINFO *info = new BITMAPINFO();

        GetDIBits(hCompDc, hbmp, 0, imageRect.bottom - imageRect.top, bits, info, DIB_RGB_COLORS);

        FILE* file = fopen("d:\\screenshot.bmp", "wb");
        fwrite(bits, 1, bitLength, file);
        fclose(file);

不幸的是,输出不是有效的位图.我不知道我做错了什么.请帮忙.

最佳答案 我假设你有IWebBrowser2接口,对吧?

然后我会得到一个HTML文档的接口:

HRESULT IWebBrowser2::get_Document(IDispatch **ppDisp);

然后到视图(as suggested here),在提供的DC上绘制内容:

//hCompDc is a CompatibleDC which select a CompatibleBitmap.
RECTL imageRect = {0, 0, nWidth, nHeight};
pHtmlDocument2->QueryInterface(IID_IViewObject, (void **)&pViewObject);
pViewObject->Draw(DVASPECT_CONTENT, -1, NULL, NULL, NULL, 
                  hCompDc, NULL, &imageRect, NULL, 0);
点赞