IWebBrowser绘制到HDC IViewObject Draw

时间:2016-06-08 20:14:13

标签: c++ iwebbrowser2

如何使此方法有效? IViewObject :: Draw返回S_OK但是没有办法在HDC上绘制它我只得到一个黑页。

我的代码段

IEInit();
//visit the giving url of the image
BSTR bstrURL = SysAllocString(L"example.com");
hr = pBrowser2->Navigate(bstrURL, &vEmpty, &vEmpty, &vEmpty, &vEmpty);
SysFreeString(bstrURL);
//wait till browser has finish loading
PauseTillBrowserFinished();

Delay(2000);
HRESULT          hr    = E_FAIL;
IDispatch*       pDisp = NULL;
IHTMLDocument2*  pDoc  = NULL;

pBrowser2->get_Document(&pDisp);
if(pDisp!=NULL){
    if(SUCCEEDED(hr = pDisp->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc)))
    {
        IHTMLElement* pElem=NULL;
        if(SUCCEEDED(pDoc->get_body(&pElem))){
            IHTMLBodyElement* pBody;
            if(pElem->QueryInterface(IID_IHTMLBodyElement, (void**)&pBody)==S_OK){
                IViewObject *pRender = NULL;
                pDoc->QueryInterface(IID_IViewObject, (void **) &pRender);
                if(pRender!=NULL){
                    RECT Client_Rect;
                    GetClientRect(hWnd,&Client_Rect);
                    int win_width = Client_Rect.right - Client_Rect.left;
                    int win_height = Client_Rect.bottom - Client_Rect.top;
                    HDC Memhdc;
                    HDC hdc;
                    HBITMAP Membitmap;
                    hdc=GetDC(hWnd);
                    Memhdc = CreateCompatibleDC(hdc);
                    Membitmap = CreateCompatibleBitmap(hdc, win_width,     win_height);
                    SelectObject(Memhdc, Membitmap);

                    DrawText(hdc,L"Hello World",11,&Client_Rect,DT_BOTTOM);
                    //drawing code goes in here
                    RECTL ClientRect;
                    ClientRect.left=0;ClientRect.top=0;
                    pBrowser2->get_Width(&ClientRect.right);
                    pBrowser2->get_Height(&ClientRect.bottom);

                    if(pRender->Draw(DVASPECT_CONTENT, 1, NULL, NULL, 
                        hdc,Memhdc, &ClientRect, NULL, NULL, 0)==S_OK){
                            BitBlt(hdc, 0, 0, win_width, win_height,     Memhdc,0, 0, SRCCOPY);
                    }
                    DeleteObject(Membitmap);
                    DeleteDC    (Memhdc);
                    ReleaseDC(hWnd,hdc);
                    MessageBox(NULL,L"",L"",NULL);
                    pRender->Release();
                }
                pBody->Release();
            }
            pElem->Release();
        }
    }
    pDisp->Release();
}

如何将Web浏览器内容绘制到ContextDC?有人请帮助

0 个答案:

没有答案