「vb.net中webBrowser控件的問題,?如何使彈出的網(wǎng)頁在 本身 webBrowser1 里運(yùn)行?」
日期:2010-5-6
vb.net中webBrowser控件的問題,,如何使彈出的網(wǎng)頁在 本身 webBrowser1 里運(yùn)行? 就是本來在 webBrowser1 已經(jīng)打開一個(gè)網(wǎng)頁了 。點(diǎn)了那個(gè)網(wǎng)頁的鏈接后,, 新的網(wǎng)頁是 從IE 彈出來的 。我想 在原來的 webBrowser1 里顯示 新的網(wǎng)頁 . 具體怎么操作,?
答案一(通過NewWindow事件處理): Dim WithEvents Web_V1 As SHDocVwCtl.WebBrowser_V1 Private Sub Form_Load() Set Web_V1 = WebBrowser1.Object WebBrowser1.Navigate "http://www.163.com" End Sub Private Sub Web_V1_NewWindow(ByVal URL As String, ByVal Flags As Long, ByVal TargetFrameName As String, PostData As Variant, ByVal Headers As String, Processed As Boolean) Processed = True WebBrowser1.Navigate URL End Sub
webBrowser控件里有個(gè)叫NewWindow的事件; private void webBrowser1_NewWindow(object sender, CancelEventArgs e) { e.Cancel = true; webBrowser1.Navigate(this.webBrowser1.StatusText) } 但是,你的做法我覺得不太可行,建議做想IE7那樣的多選擇卡,利用tabControl和webBrowser控件,就是如果是轉(zhuǎn)頁當(dāng)然都在本身的webBrowser連接拉,但是是彈出新窗口的話,就臨時(shí)生成一個(gè)tabControl的tabPage,和webBrowser,把連接賦予臨時(shí)webBrowser. 具體流程: webBrowser彈出-攔截彈出窗口-獲取連接-生成臨時(shí)webBrowser-把連接賦予臨時(shí)webBrowser.部分代碼; private void webBrowser1_NewWindow(object sender, CancelEventArgs e) { e.Cancel = true; WebBrowser Lianjie = (WebBrowser)sender; TabPage xin = new TabPage(); tabControl1.TabPages.Add(xin); WebBrowser xi = new WebBrowser(); xi.Dock = System.Windows.Forms.DockStyle.Fill; xin.Controls.Add(xi); xi.Navigate(Lianjie.StatusText); xi.NewWindow += new CancelEventHandler(webBrowser1_NewWindow); xi.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(xi_DocumentCompleted); tabControl1.SelectedTab = xin; }
|