How to get the HWND of a window displayed by a web page in Internet Explorer browser assistant objects

I am writing a browser helper object in Visual C ++ that should have a full screenshot of the web page I’ve made. I am currently capturing a DocumentComplete event in my BHO. I get a browser hWnd and can take a screenshot, but that is not what I really need. I really need a window in which the page is displayed (and not a frame with a scroll bar).

In addition, I am currently experiencing a race condition where the browser may not have displayed the page when I take a screenshot. I added a call UpdateWindow, but even after that it returns true, sometimes the window has not yet been shown.

So, summing up:

1) How to get the hWnd of the displayed HTML window 2) What suitable event is available for BHO to take a screenshot of?

EDIT:

Based on the answer below, I created this code:

        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);

Unfortunately, the result is not a valid bitmap. I do not know what I am doing wrong. Please, help.

+4
source share
1 answer

I assume you have an interface IWebBrowser2, right?

Then I would get an interface to the HTML document:

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

and then into the view ( as suggested here ) to draw the content on the supplied 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);
+2
source

All Articles