C#用webBrowser开发浏览器如何禁止弹出新窗口是IE的

pwliupeng 2006-05-10 10:31:39
webBrowser怎么解决浏览的网页中带有新建窗口的连接,点击后就是IE打开的。
如何实现点击链接后在本窗口中的tabpage里新建一个TABPAGES和webBrowser显示新的网页
...全文
2804 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhiang75 2006-06-17
  • 打赏
  • 举报
回复
用这个吧.
public class WebBrowserExtendedNavigatingEventArgs : 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;
}
}



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<WebBrowserExtendedNavigatingEventArgs> BeforeNavigate;
public event EventHandler<WebBrowserExtendedNavigatingEventArgs> BeforeNewWindow;

protected void OnBeforeNewWindow(string url, out bool cancel) {
EventHandler<WebBrowserExtendedNavigatingEventArgs> 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<WebBrowserExtendedNavigatingEventArgs> 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.Runtime.InteropServices.ComImport(), System.Runtime.InteropServices.Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"),
System.Runtime.InteropServices.InterfaceTypeAttribute(System.Runtime.InteropServices.ComInterfaceType.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);

}
}
zlkingdom 2006-06-17
  • 打赏
  • 举报
回复
好东西啊,值得收藏,不过顺便也想问一下如果要弹出一个窗口又想不被拦掉该怎么办?
Knight94 2006-05-12
  • 打赏
  • 举报
回复
to 那个压缩包有问题,没法用.
大哥.帮我想想办法啊.急死了

可以用,没有问题。
pwliupeng 2006-05-12
  • 打赏
  • 举报
回复
自己顶一下
Knight94 2006-05-10
  • 打赏
  • 举报
回复
参看
http://www.codeproject.com/csharp/multitabwebbrowser.asp
火麒噬日 2006-05-10
  • 打赏
  • 举报
回复
再要么修改添加一个 Form1的构造函数
public Form1( string str )
{
......
Form1.UPL = str;
}
火麒噬日 2006-05-10
  • 打赏
  • 举报
回复
要么楼主自己做一个只有一个webBrowser的浏览器,然后用args把参数传进去?
class Class1
{
[STAThread]
static void Main(string[] args)
{
System.Diagnostics.Process.Start("1.exe","1 2 3 4 5 6 7 8 9 10");
}
}

1.exe里面
foreach( string s in args )
{
System.Console.WriteLine( s );

}
楼主试试吧
pwliupeng 2006-05-10
  • 打赏
  • 举报
回复
不行。。但我找到了点灵感/
private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
Form1 frmWB;
frmWB = new Form1();
frmWB.Show();
}
这样新建了个窗口。那我怎么把连接地址传给这个新窗口的webbrowser的URL同时禁止弹出新的IE窗口呢
蒋晟 2006-05-10
  • 打赏
  • 举报
回复
http://support.microsoft.com/default.aspx?scid=kb;en-us;815714
pwliupeng 2006-05-10
  • 打赏
  • 举报
回复
那个压缩包有问题,没法用.
大哥.帮我想想办法啊.急死了
Knight94 2006-05-10
  • 打赏
  • 举报
回复
不过我给的例子好像是1.1的。
Knight94 2006-05-10
  • 打赏
  • 举报
回复
其中主要是设置RegisterAsBrowser属性为true,然后去截获探出新窗口的事件。
上面有源码,你可以down下来看看。
pwliupeng 2006-05-10
  • 打赏
  • 举报
回复
补充下我用的C# 2.0
pwliupeng 2006-05-10
  • 打赏
  • 举报
回复
哎。。我还是不明白。
Knight94(愚翁) 发的地址我看不懂英文哈。。
我只想知道。。当点击链接需要新建窗口
pwliupeng 2006-05-10
  • 打赏
  • 举报
回复
哎。。我还是不明白。
Knight94(愚翁) 发的代码我看不懂。。
我只想知道。。怎么禁止弹出新的IE窗口。而是用我自己做的窗口来打开这个网页。
高手们救命啊

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧