开发者

How to access webbrowser object on this code? C++

开发者 https://www.devze.com 2022-12-31 04:35 出处:网络
I found this example http://www.mvps.org/user32/webhost.cab that host an Internet Explorer WebBrowser object, and it uses this code to access the object

I found this example http://www.mvps.org/user32/webhost.cab that host an Internet Explorer WebBrowser object, and it uses this code to access the object

void webhostwnd::CreateEmbeddedWebControl(void)
{
  OleCreate(CLSID_WebBrowser,IID_IOleObject,OLERENDER_DRAW,0,&site,&storage,(void**)&mpWebObject);

  mpWebObject->SetHostNames(L"Web Host",L"Web View");

  // I have no idea why this is necessary. remark it out and everything works perfectly.
  OleSetContainedObject(mpWebObject,TRUE);

  RECT rect;
  GetClientRect(hwnd,&rect);

  mpWebObject->DoVerb(OLEIVERB_SHO开发者_运维问答W,NULL,&site,-1,hwnd,&rect);

  IWebBrowser2* iBrowser;
  mpWebObject->QueryInterface(IID_IWebBrowser2,(void**)&iBrowser);

  VARIANT vURL;
  vURL.vt = VT_BSTR;
  vURL.bstrVal = SysAllocString(L"http://google.com");
  VARIANT ve1, ve2, ve3, ve4;
  ve1.vt = VT_EMPTY;
  ve2.vt = VT_EMPTY;
  ve3.vt = VT_EMPTY;
  ve4.vt = VT_EMPTY;

  iBrowser->put_Left(0);
  iBrowser->put_Top(0);
  iBrowser->put_Width(rect.right);
  iBrowser->put_Height(rect.bottom);

  iBrowser->Navigate2(&vURL, &ve1, &ve2, &ve3, &ve4);

  VariantClear(&vURL);

  iBrowser->Release();
}

I don't have much experience with cpp, I want to know how to access that same ie object (to use Navigate2 for example) from a button or something like that. How could I achieve this?


mpWebObject is a member of the class webhostwnd. You can use the code,

IWebBrowser2* iBrowser;
  mpWebObject->QueryInterface(IID_IWebBrowser2,(void**)&iBrowser);

anywhere in the class to access the browser interface( once the mpWebObject is created).

If you are not hell bent on using the same code, here is a better example that could serve your purpose.

0

精彩评论

暂无评论...
验证码 换一张
取 消