代码很简单(运行环境:WinXP + VS2008 + IE7)
void webBrowser_DocumentComplete(object pDisp, ref object URL)
{
System.Windows.Forms.MessageBox.Show(URL.ToString());//1
try
{
if (webBrowser.ReadyState == tagREADYSTATE.READYSTATE_COMPLETE)
{
document = webBrowser.Document as HTMLDocument;//2
System.Windows.Forms.MessageBox.Show(webBrowser.Document.ToString());//3
}
}
catch (Exception ee)
{
System.Windows.Forms.MessageBox.Show(ee.Message);
}
}如果去掉代码//2一切运行正常,如不然//1和//3都不会执行,有没人知道为什么?
void webBrowser_DocumentComplete(object pDisp, ref object URL)
{
System.Windows.Forms.MessageBox.Show(URL.ToString());//1
try
{
if (webBrowser.ReadyState == tagREADYSTATE.READYSTATE_COMPLETE)
{
document = webBrowser.Document as HTMLDocument;//2
System.Windows.Forms.MessageBox.Show(webBrowser.Document.ToString());//3
}
}
catch (Exception ee)
{
System.Windows.Forms.MessageBox.Show(ee.Message);
}
}如果去掉代码//2一切运行正常,如不然//1和//3都不会执行,有没人知道为什么?
webBrowser.Document
我用的是SHDocvw.WebBrowser,只能用mshtml.HTMLDocument转换
这是个事件,跟这个没关系,也就是说把//2取掉,可以正常输出URL
然后使用document访问HTMLDocument的属性
Dim doc as IHTMLDocument2 = webBrowser1.Document
Document是一个object,文档类型不同的时候类型也不同。在显示HTML文档时支持IHTMLDocument2,显示word文档时支持Word的自动化接口
Dim htm As mshtml.HTMLDocument = DirectCast(axWebBrowser1.Document, mshtml.HTMLDocument) Dim document As HTMLDocument = DirectCast(webBrowser.Document.DomDocument, HTMLDocument)
不好意思,C#里没有DirectCast这个函数
是SHDocvw.WebBrowser.Document.DomDocument
印象中是有的.