C# webBrowser禁止在新窗口打开,强制在本窗口打开
进行webBrower开发的时候,肯定都会遇到一个问题。
那就是怎么样强制在本窗口打开新窗口的问题。
网上最常见的解决方法就是,
在NewWindow事件中取得要打开的网址,取消打开新窗口,然后在本窗口打开要转向的网址
p rivate void webBrowser_1_NewWindow(object sender, CancelEventArgs e)
{
WebBrowser webBrowser_temp = (WebBrowser)sender;
string newUrl = webBrowser_temp.Document.ActiveElement.GetAttribute("href");
webBrowser_1.Url = new Uri(newUrl);
e.Cancel = true;
}
虽然这种方法能够解决大部分的要求,但是治标不治本。
而且当网址里面还有汉字的时候就更加麻烦了,比如在百度MP3,歌曲名字都是汉字,和百度空间里面,大部分用户名都是汉字,所以取的网址都用乱码。虽然,可以对网址进行编码,但是并不是所有网页都是uft-8编码,对于如何得知网页的编码又是一个课题了。
最根本的方法就是重写了。
新建一个类
using System;
using System.Collections.Generic;
using System.Text;
namespace webTestRecorder
{
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);
}
}
public class WebBrowserExtendedNavigatingEventArgs : System.ComponentModel.CancelEventArgs
{
p rivate string _Url;
public string Url
{
get { return _Url; }
}
p rivate string _Frame;
public string Frame
{
get { return _Frame; }
}
public WebBrowserExtendedNavigatingEventArgs(string url, string frame)
: base()
{
_Url = url;
_Frame = frame;
}
}
}
然后把webBrowser换成我们重写的ExtendedWebBrowser,添加事件处理
ieBrowser = new ExtendedWebBrowser();
ieBrowser.BeforeNewWindow += new EventHandler<WebBrowserExtendedNavigatingEventArgs>(ieBrowser_BeforeNewWindow);
然后在其BeforeNewWindow事件中:
void ieBrowser_BeforeNewWindow(object sender, WebBrowserExtendedNavigatingEventArgs e) {
e.Cancel=true;
((ExtendedWebBrowser)sender).Navigate(e.Url);
}
---------------------另一方案下下----------------------
要解决这个问题,可以使用下面的方法:
在日常的开发中,大家有时需要用WebBrowser加载URL,来实现某些功能。而这时,我们就不希望所打开的页面中的链接,在新窗口中打开,因为这样的话,实际上是用系统默认的浏览器打开了,从而脱离了你的WebBrowser,也就不能被你所控制了。
假设WebBrowser的Name是 webBrowser1
******* 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"); }}
******* void webBrowser1_NewWindow(object sender, CancelEventArgs e){ e.Cancel = true;}
记得将 WebBrowser 的 AllowWebBrowserDrop 设为 false
将 WebBrowser 的 WebBrowserShortcutsEnabled 设为 false
将 WebBrowser 的 IsWebBrowserContextMenuEnabled 设为 false
转载于:https://www.cnblogs.com/hackpig/archive/2010/02/15/1668396.html
C# webBrowser禁止在新窗口打开,强制在本窗口打开相关推荐
- 在HTML中,如何设置新窗口打开和在原窗口打开
在你连接的地方代码加上target="_blank" 新窗口打开target="_parent" 同一窗口打开 或者不要加target="_paren ...
- html打开界面的时候新建浏览器选项卡,IE11新选项卡怎么设置 IE11一个窗口打开多个页面设置方法...
昨天小编电脑突然出现系统问题,导致最终只能重装系统解决,不过在新版系统中,自带的是IE11浏览器,每次打开网页新窗口的时候,默认都是"新窗口"打开,而不是大多数浏览器中熟悉的&qu ...
- 修改 idea 打开新项目时关闭老项目窗口
在用idea打开新项目的时候,不小心选择了自己不想选择的选项,而且还选中了不再询问我,导致打开新项目时老项目的窗口就被关闭的问题,今天找了一下idea的设置,发现只需要修改 File -> Se ...
- Kali Linux打开多个终端窗口
Kali Linux打开多个终端窗口 在Kali Linux系统中,大部分工具都是命令行工具.大学霸IT达人所以,用户需要在终端窗口执行.但是,一些操作可能需要同时执行多个命令,因此需要同时打开多个窗 ...
- python打开后的界面-Python - tkinter:打开和关闭对话框窗口
我是Python新手,必须编写一个简单的GUI程序,为了简单起见,我选择在tkinter中这样做. 我想要的GUI应该非常类似于在Windows上安装程序时经常遇到的对话框(您想要安装的位置,您想要的 ...
- python提示对话框自动关闭_Python - tkinter:打开和关闭对话框窗口
我是Python新手,必须编写一个简单的GUI程序,为了简单起见,我选择在tkinter中这样做. 我想要的GUI应该非常类似于在Windows上安装程序时经常遇到的对话框(您想要安装的位置,您想要的 ...
- window.open不重复打开同一个名称的窗口_干货满满|Ctrl键的正确打开方式
"ctrl"是键盘中一个常用的键,全名为"control",中文意为"控制",在计算机基础中称为"控制键". 那么你知道 ...
- #region的快捷键+++从一个页面中弹出一个新窗口,当新窗口关闭时刷新原窗口!...
ctrl + k,s 先输入#region,再按Tab键,会自动弹出#endregion,并且焦点停留在#region处.不要急于改变焦点,这时输入你需要的注释后,再按回车,焦点会跳转到下一行 ...
- 【Selenium】控制当前已经打开的 chrome浏览器窗口
前言 有过几个小伙伴问过我如何利用 Selenium 获取已经打开的浏览器窗口,这里给安排了,还安排了两篇. 标题 链接 [Selenium]控制当前已经打开的 chrome浏览器窗口 https:/ ...
最新文章
- webstorm命令行提示‘node‘ 或‘npm‘不是内部或外部命令,也不是可运行的程序
- 在ASP.NET中跟踪和恢复大文件下载
- 光伏电价断崖式下跌 企业遭遇成长烦恼
- android 图片读写,Android系统中图片的读写
- 安卓使用Socket发送中文,C语言服务端接收乱码问题解决方式
- 访客门禁系统供应商 首选钱林厂家
- 猛料来啦!Autodesk全线产品二次开发视频录像下载!!
- Opencv 中cv开头的函数和没有cv的区别,例如cvWaitkey()和waitKey()的区别
- 图像处理课程设计大报告 MATLAB GUI APP实现直方图均衡化、几何变换和加噪滤波
- 电脑耳机声音小怎么调大_湘乡电脑耳机怎么样,IP话机推荐
- 如何防止JAVA反射对单例类的攻击?
- 《电磁场与电磁波》课程笔记(一)——矢量与坐标系
- Excel 宏编程-使用excel宏编写第一个Hello World程序实例演示!
- ai交互剧本_AI可以制作音乐,剧本和诗歌。 电影呢?
- oracle spatial 更新,oracle Spatial(空间数据库)概述
- RuntimeError: xxx.pth is a zip archive (did you mean to use torch.jit.load()?)
- 69.46.68.92 index.php,【英联雅思】搞定四六级又战托福雅思?先测测自己的词汇量有多少吧~...
- OrangePi PC 玩Linux主线内核踩坑之旅(二)之制作镜像后的查遗补缺
- Spring Boot启动报错问题: The Bean Validation API is on the classpath but no implementation could be found
- Android多线程断点续传下载原理及实现,移动开发工程师简历