winfrom的webBrowser从当前页面打开新的页面

来源:互联网 发布:淘宝上挑三捡四的人 编辑:程序博客网 时间:2024/06/05 15:12

以下代码,好像对于有嵌套行为的页面,无效,不过略作修改应该也能兼容所有情况:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace localindex{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            webBrowser1.ScriptErrorsSuppressed = true; //禁用错误脚本提示            webBrowser1.IsWebBrowserContextMenuEnabled = false; // 禁用右键菜单            webBrowser1.WebBrowserShortcutsEnabled = false; //禁用快捷键            webBrowser1.AllowWebBrowserDrop = false; // 禁止文件拖动            webBrowser1.Navigate(Application.StartupPath + @"\assets\index.html");            webBrowser1.Document.Window.Error += Window_Error;            webBrowser1.NewWindow += CancelEventHandler;            webBrowser1.DocumentCompleted += webBrowser_DocumentCompleted;        }        private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)        {            foreach (HtmlElement archor in webBrowser1.Document.Links)            {                archor.SetAttribute("target", "_self");            }            foreach (HtmlElement form in webBrowser1.Document.Forms)            {                form.SetAttribute("target", "_self");            }        }        private void Window_Error(object sender, HtmlElementErrorEventArgs e)        {            e.Handled = true;        }        //禁用新窗口打开        public void CancelEventHandler(object sender, CancelEventArgs e)        {            //webBrowser1.StatusText 判断是否是url类            //System.Diagnostics.Process.Start(webBrowser1.StatusText);            e.Cancel = true;        }    }}
webBrowser_DocumentCompleted里面讲页面调转的地方全部修改了标签模式。注意,CancelEventHandler中需要将e.Cancel = true;进行设置

阅读全文
0 0
原创粉丝点击