C#的WebBrowser操作frame如此简单

刚学c#不久,也不太懂什么IHTMLDocument、IHTMLDocument2、IWebBrowser2等等。自己琢磨了好久,终于知道了怎么用WebBrowser操作frame和iframe。

1.获取frame的源文件

MessageBox.Show(webBrowser1.Document.Window.Frames["main"].Document.Body.InnerHtml);

2.获取frame的HTMLDocument接口

HTMLDocument doc = (HTMLDocument)webBrowser1.Document.DomDocument;
object j;
for (int i = 0; i < doc.parentWindow.frames.length; i++)
{
      j = i;
      HTMLWindow2Class frame = doc.parentWindow.frames.item(ref j) as HTMLWindow2Class;
      if (frame.name == "main")
       {                   
             MessageBox.Show(frame.document.title);                    
       }
}    

3.获取frame的IHTMLDocument2接口

IHTMLDocument2 doc = (IHTMLDocument2)webBrowser1.Document.Window.Frames["main"].Document.DomDocument;

4.取得frame中被点击的连接

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
     string url = webBrowser1.Document.Window.Frames["main"].Document.ActiveElement.GetAttribute("src");
}

 

 

原文:http://blog.csdn.net/llj1985/archive/2007/09/01/1768147.aspx

posted @ 2010-04-15 09:19  单康丽  阅读(536)  评论(0编辑  收藏  举报