正文

C# WebBrowser 编程 使用mshtml读取网页内容2010-06-25 17:00:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/tujun/51406.html

分享到:

首先需要引用Microsoft.mshtml,AxInterop.SHDocVw 一些方法: webBrowser.ScriptErrorsSuppressed = true;//允许脚本调试 webBrowser.WebBrowserShortcutsEnabled = true;//允许快捷键 /*********************一些事件的处理*******************/ webBrowser.CanGoForwardChanged += new EventHandler(webBrowser_CanGoForwardChanged); webBrowser.CanGoBackChanged += new EventHandler(webBrowser_CanGoBackChanged); webBrowser.StatusTextChanged += new EventHandler(webBrowser1_StatusTextChanged); webBrowser.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating); webBrowser.Navigated += new WebBrowserNavigatedEventHandler(webBrowser1_Navigated); webBrowser.DocumentTitleChanged += new EventHandler(webBrowser1_DocumentTitleChanged); webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted); webBrowser.Disposed += new EventHandler(webBrowser1_Disposed); (webBrowser.ActiveXInstance as SHDocVw.WebBrowser).NewWindow3 += new SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(FrmIEExplorer_NewWindow3); webBrowser.ProgressChanged += new WebBrowserProgressChangedEventHandler(webBrowser1_ProgressChanged); /*********************************************************/  private WebBrowser CreateNewWebBrowser()  {      WebBrowser _axWebBrowser = new WebBrowser();      _axWebBrowser.Tag = new HE_WebBrowserTag();      _axWebBrowser.Dock = DockStyle.Fill;      TabPage _TabPage = new TabPage();      _TabPage.Controls.Add(_axWebBrowser);          RegisterEvent(_axWebBrowser);      flatTabControl1.TabPages.Add(_TabPage);      flatTabControl1.SelectedTab = _TabPage;      _ActiveWebBrowser = _axWebBrowser;      btn_Remove.Enabled = flatTabControl1.TabPages.Count >= 1;      // tblCloseTab.Enabled = true;      return _axWebBrowser;  }  /// <summary>  /// 新窗口事件  /// </summary>  /// <param name="ppDisp"></param>  /// <param name="Cancel"></param>  /// <param name="dwFlags"></param>  /// <param name="bstrUrlContext"></param>  /// <param name="bstrUrl"></param>  void FrmIEExplorer_NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl)  {      Cancel = true;          ppDisp = (this._ActiveWebBrowser.ActiveXInstance as SHDocVw.WebBrowser).Application;//这里是关键      WebBrowser _axWebBrowser = CreateNewWebBrowser();      _axWebBrowser.Navigate(bstrUrl);  } //读取网页内容 private void ReadDoc(IHTMLDocument2 doc) {     int inputcount = doc.all.length;     foreach (IHTMLElement item in doc.all)     {         if (item.id == "_petName" || item.id == "userNameTopLine")         {             this.rich_src.AppendText("     id:");             this.rich_src.AppendText(item.innerText);         }         //this.rich_src.AppendText("     id:");         //this.rich_src.AppendText(item.id == null ? "未设置" : item.id);         //this.rich_src.AppendText("     tag:");         //this.rich_src.AppendText(item.tagName);         //this.rich_src.AppendText("     innerHTML:");         //this.rich_src.AppendText(item.innerHTML == null ? "未设置" : item.innerHTML);         //this.rich_src.AppendText("\r\n-------------\r\n");         if (item.tagName.Equals("FRAME"))         {             //this.rich_src.AppendText(" IFRAME    id:");             //this.rich_src.AppendText(item.id == null ? "未设置" : item.id);             //this.rich_src.AppendText("\r\n-------------\r\n");             mshtml.IHTMLFrameBase2 iFrame = (mshtml.IHTMLFrameBase2)item;             mshtml.IHTMLDocument2 iHTMLDocument2 = iFrame.contentWindow.document;             ReadDoc(iHTMLDocument2);         }     } } 下面2句代码获取IHTMLDocument2 IHTMLDocument2 doc = _ActiveWebBrowser.Document.DomDocument as IHTMLDocument2; ReadDoc(doc);  

阅读(12417) | 评论(4)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册