2011-05-13 179 views
1

我有一種情況,我想通過IE9附帶的IHTMLDocument7接口使用一些方法。特別是getElementsByTagNameNS()方法,因爲我想使用特定的標記類型(比解析整個文檔要容易得多)。有沒有辦法使用TCppWebBrowser從C++ Builder(或Delphi)最新的IE9 MSHTML接口?

我當前的代碼如下所示:

IHTMLDocument2* doc = NULL; 

    if (browser->ControlInterface->Document) // make sure TCppWebBrowser is OK 
    { 
     if (SUCCEEDED(browser->ControlInterface->Document->QueryInterface(IID_IHTMLDocument2, (void**)&doc))) 
     { 
      IHTMLElement* body; 

      HRESULT hr = doc->get_body(&body); 
      if (SUCCEEDED(hr)) 
      { 
       WideString innerHtml; 
       body->get_innerHTML(&innerHtml); 
       txtInfo->Text = innerHtml; 

       body->Release(); 
      } 

      doc->Release(); 
     } 
    } 

這工作,並可能有問題,但我最感興趣的是得到我現在想要的功能。

如果我改變了代碼,使用與IE9中的新界面:

browser->ControlInterface->Document->QueryInterface(IID_IHTMLDocument7, (void**)&doc) 

我得到以下編譯器錯誤:

[BCC32 Error] Unit2.cpp(134): E2451 Undefined symbol 'IID_IHTMLDocument7' 
    Full parser context 
    Unit2.cpp(129): parsing: void _fastcall TForm2::Button4Click(TObject *) 

[BCC32 Error] Unit2.cpp(134): E2285 Could not find a match for 'IUnknown::QueryInterface(undefined,void * *)' 
    Full parser context 
    Unit2.cpp(129): parsing: void _fastcall TForm2::Button4Click(TObject *) 

看來,它無法找到該界面匹配。

  • 我應該怎麼做才能使這個 接口可用?我猜 與BCB 一起發貨的Windows SDK版本可能已過時,或不知道有關IE9 版本的MSHTML的類型庫的 。
  • 有沒有辦法來 做出相應的頭文件 提供此接口 (IID_IHTMLDocument7),並保持 TCppWebBrowserControl?或者我需要 導入一個單獨的ActiveX控件?

我在使用IE9的Windows 7(x64)上使用C++ Builder Starter XE(15.0.3953.35171)。

回答

1

至少在C++ Builder 2010中,IHTMLDocument7未在mshtml.h中定義,但是IHTMLDocument6是。如果您從Microsoft下載最新的SDK,則可以將IHTMLDocument7定義直接複製到現有代碼中。

或者,嘗試查看BCCSDK是否已更新爲支持IHTMLDocument7。

3

使用IHTMLDocument3 Interface代替IHTMLDocument7,或執行JavaScript來回報你需要的東西,如:

IHTMLDocument2 *doc = NULL; 
IHTMLWindow2 *win; 

if(SUCCEEDED(CppWebBrowser->Document->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&doc))) { 
    HRESULT hr = doc->get_parentWindow(&win); 
    if (SUCCEEDED(hr)) { 
     BSTR cmd = L"function deltag(){\ 
    var all = this.document.getElementsByTagNameNS('IMG'); \ 
    var images = [];       \ 
    for(var a=0;a<all.length;++a)    \ 
    {           \ 
     if(all[a].tagName == 'NAME')   \ 
      images.push(all[a]);   \ 
    }            \ 
    for(var i=0;i<images.length;++i)    \ 
    {            \ 
     images[i].parentNode.removeChild(images[i]); \ 
    } " ;   \       

    VARIANT v; 
    VariantInit(&v); 
    win->execScript(cmd,NULL,&v); 
    VariantClear(&v); 
    win->Release(); 
    } 
    doc->Release(); 
    } 
相關問題