C ++ - IWebBrowser2 - Javascript未激活

时间:2018-04-17 15:26:12

标签: javascript c++ iwebbrowser2

我已经在' codeproject.com'中测试了项目代码。网站(https://www.codeproject.com/Articles/3365/Embed-an-HTML-control-in-your-own-window-using-pla

这是代码的一部分:

long EmbedBrowserObject(HWND hwnd)
{
   IOleObject           *browserObject;
   IWebBrowser2     *webBrowser2;
   RECT             rect;
   char             *ptr;
   IOleInPlaceFrameEx   *iOleInPlaceFrameEx;
   _IOleClientSiteEx    *_iOleClientSiteEx;

   if (!(ptr = (char *)GlobalAlloc(GMEM_FIXED, sizeof(IOleInPlaceFrameEx) + sizeof(_IOleClientSiteEx) + sizeof(IOleObject *))))
      return(-1);

   iOleInPlaceFrameEx = (IOleInPlaceFrameEx *)(ptr + sizeof(IOleObject *));
   iOleInPlaceFrameEx->frame.lpVtbl = &MyIOleInPlaceFrameTable;
   iOleInPlaceFrameEx->window = hwnd;

   _iOleClientSiteEx = (_IOleClientSiteEx *)(ptr + sizeof(IOleInPlaceFrameEx) + sizeof(IOleObject *));
   _iOleClientSiteEx->client.lpVtbl = &MyIOleClientSiteTable;
   _iOleClientSiteEx->inplace.inplace.lpVtbl = &MyIOleInPlaceSiteTable;
   _iOleClientSiteEx->inplace.frame = iOleInPlaceFrameEx;

   if (!OleCreate(&CLSID_WebBrowser, &IID_IOleObject, OLERENDER_DRAW, 0, (IOleClientSite *)_iOleClientSiteEx, &MyIStorage, (void**)&browserObject))
   {

       *((IOleObject **)ptr) = browserObject;
       SetWindowLong(hwnd, GWL_USERDATA, (LONG)ptr);
       browserObject->lpVtbl->SetHostNames(browserObject, L"My Host Name", 0);

       GetClientRect(hwnd, &rect);

       if (!OleSetContainedObject((struct IUnknown *)browserObject, TRUE) && !browserObject->lpVtbl->DoVerb(browserObject, OLEIVERB_SHOW, NULL, (IOleClientSite *)_iOleClientSiteEx, -1, hwnd, &rect) && !browserObject->lpVtbl->QueryInterface(browserObject, &IID_IWebBrowser2, (void**)&webBrowser2))
    {
        webBrowser2->lpVtbl->put_Left(webBrowser2, 0);
        webBrowser2->lpVtbl->put_Top(webBrowser2, 0);
        webBrowser2->lpVtbl->put_Width(webBrowser2, rect.right);
        webBrowser2->lpVtbl->put_Height(webBrowser2, rect.bottom);
        webBrowser2->lpVtbl->Release(webBrowser2);
        return(0);
    }

    UnEmbedBrowserObject(hwnd);
    return(-3);
}

GlobalFree(ptr);
return(-2);
} 

并且:

long DisplayHTMLPage(HWND hwnd, LPTSTR webPageName)
{
    IWebBrowser2    *webBrowser2;
    VARIANT         myURL;
    IOleObject      *browserObject;

    browserObject = *((IOleObject **)GetWindowLong(hwnd, GWL_USERDATA));

    if (!browserObject->lpVtbl->QueryInterface(browserObject, &IID_IWebBrowser2, (void**)&webBrowser2))
    {
        VariantInit(&myURL);
        myURL.vt = VT_BSTR;

        size_t convertedChars = 0;
        wchar_t wcstring[50];
        mbstowcs_s(&convertedChars, wcstring, 50, webPageName, _TRUNCATE);
        myURL.bstrVal = SysAllocString(wcstring);

        if (!myURL.bstrVal)
        {
           badalloc:    webBrowser2->lpVtbl->Release(webBrowser2);
           return(-6);
        }

        webBrowser2->lpVtbl->Navigate2(webBrowser2, &myURL, 0, 0, 0, 0);

        VariantClear(&myURL);

        webBrowser2->lpVtbl->Release(webBrowser2);

        return(0);
    }

    return(-5);
   }

当我启动他们的代码时,我没有任何问题网站在没有Javascript的情况下工作。但是现在,如果我使用Javascript打开一个网站,我会在屏幕上看到很多错误。

Error Message in picture

Second error message in picture

我该如何解决?

谢谢

2 个答案:

答案 0 :(得分:0)

确保您的嵌入式Web浏览器以最高的IE版本运行。

HKEY_CURRENT_USER \ SOFTWARE \ Microsoft \ Internet Explorer \ Main \ FeatureControl \ FEATURE_BROWSER_EMULATION

https://weblog.west-wind.com/posts/2011/May/21/Web-Browser-Control-Specifying-the-IE-Version

答案 1 :(得分:0)

我在HTML中添加了以下说明:

<meta http-equiv="X-UA-Compatible" content="IE=edge" /> 

有效。