1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > WebBrowser在同一个窗口打开网页 禁止在新窗口打开

WebBrowser在同一个窗口打开网页 禁止在新窗口打开

时间:2019-02-09 09:15:27

相关推荐

WebBrowser在同一个窗口打开网页 禁止在新窗口打开

WebBrowser在同一个窗口打开网页,无论target是否是_blankNewWindow事件中

e.Cancel = true;

然后

取WebBrowser.StatusText属性就是新窗口的地址 private void webBrowser1_NewWindow(object sender, CancelEventArgs e)

{

e.Cancel = true;

webBrowser1.Navigate(webBrowser1.StatusText);

}

上面的方法治标不治本,继续

在加载文档全部加载后的事件中

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

{

//将所有的链接的目标,指向本窗体

foreach (HtmlElement archor in this.webBrowser1.Document.Links)

{

archor.SetAttribute("target", "_self");

}

//将所有的FORM的提交目标,指向本窗体

foreach (HtmlElement form. in this.webBrowser1.Document.Forms)

{

form.SetAttribute("target", "_self");

}

}

90%的网页可以了,用百度-视频栏目测试

还有一种方法那是重写Webbrowser控件新建一个类

using System;

using System.Collections.Generic;

using System.Text;

namespace Bank.UI

{

///

/// 重写Webbrowser类

/// 彻底解决打开新窗口浏览网页的总题

///

public class ExtendedWebBrowser:System.Windows.Forms.WebBrowser

{

System.Windows.Forms.AxHost.ConnectionPointCookie cookie;

WebBrowserExtendedEvents events;

//This method will be called to give you a chance to create your own event sink

protected override void CreateSink()

{

//MAKE SURE TO CALL THE BASE or the normal events won"t fire

base.CreateSink();

events = new WebBrowserExtendedEvents(this);

cookie = new System.Windows.Forms.AxHost.ConnectionPointCookie(this.ActiveXInstance, events, typeof(DWebBrowserEvents2));

}

protected override void DetachSink()

{

if (null != cookie)

{

cookie.Disconnect();

cookie = null;

}

base.DetachSink();

}

//This new event will fire when the page is navigating

public event EventHandler BeforeNavigate;

public event EventHandler BeforeNewWindow;

protected void OnBeforeNewWindow(string url, out bool cancel)

{

EventHandler h = BeforeNewWindow;

WebBrowserExtendedNavigatingEventArgs args = new WebBrowserExtendedNavigatingEventArgs(url, null);

if (null != h)

{

h(this, args);

}

cancel = args.Cancel;

}

protected void OnBeforeNavigate(string url, string frame, out bool cancel)

{

EventHandler h = BeforeNavigate;

WebBrowserExtendedNavigatingEventArgs args = new WebBrowserExtendedNavigatingEventArgs(url, frame);

if (null != h)

{

h(this, args);

}

//Pass the cancellation chosen back out to the events

cancel = args.Cancel;

}

//This class will capture events from the WebBrowser

class WebBrowserExtendedEvents : System.Runtime.InteropServices.StandardOleMarshalObject, DWebBrowserEvents2

{

ExtendedWebBrowser _Browser;

public WebBrowserExtendedEvents(ExtendedWebBrowser browser) { _Browser = browser; }

//Implement whichever events you wish

public void BeforeNavigate2(object pDisp, ref object URL, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel)

{

_Browser.OnBeforeNavigate((string)URL, (string)targetFrameName, out cancel);

}

public void NewWindow3(object pDisp, ref bool cancel, ref object flags, ref object URLContext, ref object URL)

{

_Browser.OnBeforeNewWindow((string)URL, out cancel);

}

}

[System.Import(), System.Runtime.InteropServices.Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"),

System.Runtime.InteropServices.InterfaceTypeAttribute(System.InterfaceType.InterfaceIsIDispatch),

System.Runtime.InteropServices.TypeLibType(System.Runtime.InteropServices.TypeLibTypeFlags.FHidden)]

public interface DWebBrowserEvents2

{

[System.Runtime.InteropServices.DispId(250)]

void BeforeNavigate2(

[System.Runtime.InteropServices.In,

System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)] object pDisp,

[System.Runtime.InteropServices.In] ref object URL,

[System.Runtime.InteropServices.In] ref object flags,

[System.Runtime.InteropServices.In] ref object targetFrameName, [System.Runtime.InteropServices.In] ref object postData,

[System.Runtime.InteropServices.In] ref object headers,

[System.Runtime.InteropServices.In,

System.Runtime.InteropServices.Out] ref bool cancel);

[System.Runtime.InteropServices.DispId(273)]

void NewWindow3(

[System.Runtime.InteropServices.In,

System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)] object pDisp,

[System.Runtime.InteropServices.In, System.Runtime.InteropServices.Out] ref bool cancel,

[System.Runtime.InteropServices.In] ref object flags,

[System.Runtime.InteropServices.In] ref object URLContext,

[System.Runtime.InteropServices.In] ref object URL);

}

public class WebBrowserExtendedNavigatingEventArgs : ponentModel.CancelEventArgs

{

private string _Url;

public string Url

{

get { return _Url; }

}

private string _Frame;

public string Frame

{

get { return _Frame; }

}

public WebBrowserExtendedNavigatingEventArgs(string url, string frame)

: base()

{

_Url = url;

_Frame. = frame;

}

}关闭解决方案后再打开

把原来WEBBROWSER控件去掉

右边把ExtendedWebBrowser拖过来

namespace Bank.UI

{

public partial class test : Form

{

public test()

{

InitializeComponent();

extendedWebBrowser1.BeforeNewWindow+=new EventHandler(extendedWebBrowser1_BeforeNewWindow);

}

private void extendedWebBrowser1_BeforeNewWindow(object sender, WebBrowserExtendedNavigatingEventArgs e)

{

e.Cancel = true;

extendedWebBrowser1.Navigate(e.Url);

}

}

}

在具体使用过程中,发现了一个问题,交行证书用户登录时,无任何提示

解决办法ScriptErrorsSuppressed = false;

代码:private void extendedWebBrowser1_BeforeNewWindow(object sender, WebBrowserExtendedNavigatingEventArgs e)

{

e.Cancel = true;

extendedWebBrowser1.ScriptErrorsSuppressed = false;

extendedWebBrowser1.Navigate(e.Url);

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。