读书人

怎么在cppwebbrowser里面修改网页内容

发布时间: 2012-03-27 13:44:24 作者: rapoo

如何在cppwebbrowser里面修改网页内容

Delphi(Pascal) code
procedure TForm1.Button2Click(Sender: TObject); var        Range:IHTMLTxtRange; begin   Range := ((WebBrowser1.Document as IHTMLDocument2).body as     IHTMLBodyElement).createTextRange;   Range.collapse(False);   Range.pasteHTML(' <br> <b>插入的内容</b>'); end; 

上面是我找的Delphi方法,

C/C++ code
void __fastcall TForm1::Button2Click(TObject *Sender){    IHTMLTxtRange* Range =0;    IHTMLDocument2* HTMLDoc =0;    IHTMLBodyElement* HTMLBodyElem=0;    HTMLDoc = dynamic_cast<IHTMLDocument2*>(WebBrowser1.Document);    if(HTMLDoc){        HTMLBodyElem =dynamic_cast<IHTMLBodyElement*>(HTMLDoc);        if(HTMLBodyElem)        {            HTMLBodyElem->createTextRange(&Range);            if(Range)            {                Range->collapse(false);                 Range.pasteHTML(" <br> <b>插入的内容</b>");                 // 加入你要的东东            }        }    }}

上面是我找的,相对应的代码转换。
但是不能用,HTMLDoc = dynamic_cast<IHTMLDocument2*>(WebBrowser1.Document);应该是webbrowser->Document,改了之后报错,不能cast IHTMLDocument2和_di_IDispatch.

Range.pasteHTML(" <br> <b>插入的内容</b>")应该是->

其他可能还有问题,跪求修改源码,我已经折腾了好几天了,各种方法不会。
还有个问题,用Delphi那个我在WebBrowser1DocumentComplete里面改内容的,但是每次是先显示原来的网页,再显示我修改后的网页,请问有什么办法能跳过显示原来的页面。
在线等,分没了,全部奉上,急!!!!!!!!!!



[解决办法]
IDispatch *HTMLDoc=static_cast<IDispatch*>(CppWebBrowser1->Document);
[解决办法]
不能匹配<span>之类的标记符,可能是因为pBodyElement->createTextRange(&pRange);这里的创建的Range是纯文本的,也就是HTML页面显示出来的文本,而不是带有<>标记的HTML文本.

读书人网 >C++ Builder

热点推荐