在WebBrowser中截获弹出对话框内容并将其屏蔽
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MyWebBrowser1.WBDocHostShowUIShowMessage += new MyWebBrowser.DocHostShowUIShowMessageEventHandler(MyWebBrowser1_WBDocHostShowUIShowMessage);
}
MyWebBrowser MyWebBrowser1 = new MyWebBrowser();
private void Form1_Load(object sender, EventArgs e)
{
this.Controls.Add(MyWebBrowser1);
MyWebBrowser1.Visible = true;
MyWebBrowser1.Navigate("about:blank");
MyWebBrowser1.Document.Write("<mce:script type="text/<a href="http://lib.csdn.net/base/javascript" class='replace_word' title="JavaScript知识库" target='_blank' style='color:#df3434; font-weight:bold;'>JavaScript</a>"><!--
alert('我就是确定按钮啊')
// --></mce:script>");
}
void MyWebBrowser1_WBDocHostShowUIShowMessage(object sender, ExtendedBrowserMessageEventArgs e)
{
e.Cancel = true;//屏蔽弹出,自行处理
if (MessageBox.Show("是否要屏蔽Alert对话框", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
{
MessageBox.Show(e.Text, e.Caption);
}
}
}
public class MyWebBrowser : WebBrowser
{
//委托、事件
public delegate void DocHostShowUIShowMessageEventHandler(object sender, ExtendedBrowserMessageEventArgs e);
public event DocHostShowUIShowMessageEventHandler WBDocHostShowUIShowMessage = null; #region ExtendedWebBrowserSite
class ExtendedWebBrowserSite : WebBrowser.WebBrowserSite, UnsafeNativeMethods.IDocHostShowUI
{
private MyWebBrowser _Browser;
public ExtendedWebBrowserSite(MyWebBrowser host)
: base(host)
{
_Browser = host;
}
int UnsafeNativeMethods.IDocHostShowUI.ShowMessage(IntPtr hwnd, string lpstrText,
string lpstrCaption, uint dwType,
string lpstrHelpFile, uint dwHelpContext, ref int lpResult)
{
int iRet = 1; //Hresults.S_FALSE
if (_Browser.WBDocHostShowUIShowMessage != null)
{
ExtendedBrowserMessageEventArgs args = new ExtendedBrowserMessageEventArgs(hwnd, lpstrText, lpstrCaption, dwType, lpstrHelpFile, dwHelpContext);
_Browser.WBDocHostShowUIShowMessage(this, args);
if (args.Cancel == true)
{ iRet = 0;//Hresults.S_OK;
lpResult = args.pResult;
}
}
return iRet;
}
int UnsafeNativeMethods.IDocHostShowUI.ShowHelp(IntPtr hwnd, string pszHelpFile, uint uCommand, uint dwData, UnsafeNativeMethods.tagPOINT ptMouse, object pDispatchObjectHit)
{
return unchecked((int)0x80004001);//Hresults.E_NOTIMPL
}
}
protected override WebBrowserSiteBase CreateWebBrowserSiteBase()
{
return new ExtendedWebBrowserSite(this);
}
#endregion
}
public class UnsafeNativeMethods
{
#region IDocHostShowUI
[StructLayout(LayoutKind.Explicit, Pack = 4)]
public struct __MIDL_IWinTypes_0009
{
// Fields
[FieldOffset(0)]
public int hInproc;
[FieldOffset(0)]
public int hRemote;
}
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct _RemotableHandle
{
public int fContext;
public __MIDL_IWinTypes_0009 u;
}
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct tagPOINT
{
public int x;
public int y;
}
[ComImport, ComVisible(true)]
[Guid("C4D244B0-D43E-11CF-893B-00AA00BDCE1A")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDocHostShowUI
{
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int ShowMessage(
IntPtr hwnd,
[MarshalAs(UnmanagedType.LPWStr)] string lpstrText,
[MarshalAs(UnmanagedType.LPWStr)] string lpstrCaption,
[MarshalAs(UnmanagedType.U4)] uint dwType,
[MarshalAs(UnmanagedType.LPWStr)] string lpstrHelpFile,
[MarshalAs(UnmanagedType.U4)] uint dwHelpContext,
[In, Out] ref int lpResult);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int ShowHelp(
IntPtr hwnd,
[MarshalAs(UnmanagedType.LPWStr)] string pszHelpFile,
[MarshalAs(UnmanagedType.U4)] uint uCommand,
[MarshalAs(UnmanagedType.U4)] uint dwData,
[In, MarshalAs(UnmanagedType.Struct)] tagPOINT ptMouse,
[Out, MarshalAs(UnmanagedType.IDispatch)] object pDispatchObjectHit);
}
#endregion
}
#region ExtendedBrowserMessageEventArgs
public class ExtendedBrowserMessageEventArgs : CancelEventArgs
{
private int _plResult;
public int pResult
{
get { return _plResult; }
}
public System.Windows.Forms.DialogResult DlgResult
{
set { _plResult = (int)value; }
}
private IntPtr _hwnd;
public IntPtr hwnd
{
get { return _hwnd; }
}
private string _lpstrText;
public string Text
{
get { return _lpstrText; }
}
private string _lpstrCaption;
public string Caption
{
get { return _lpstrCaption; }
}
private uint _dwType;
public System.Windows.Forms.MessageBoxButtons DlgButtons
{
get
{
switch (_dwType & (int)MsgBoxButton.MASK)
{
case (int)MsgBoxButton.MB_OKCANCEL:
return System.Windows.Forms.MessageBoxButtons.OKCancel;
case (int)MsgBoxButton.MB_ABORTRETRYIGNORE:
return System.Windows.Forms.MessageBoxButtons.AbortRetryIgnore;
case (int)MsgBoxButton.MB_YESNOCANCEL:
return System.Windows.Forms.MessageBoxButtons.YesNoCancel;
case (int)MsgBoxButton.MB_YESNO:
return System.Windows.Forms.MessageBoxButtons.YesNo;
case (int)MsgBoxButton.MB_RETRYCANCEL:
return System.Windows.Forms.MessageBoxButtons.RetryCancel;
case (int)MsgBoxButton.MB_OK:
default:
return System.Windows.Forms.MessageBoxButtons.OK;
}
}
}
public System.Windows.Forms.MessageBoxIcon DlgIcon
{
get
{
switch (_dwType & (int)MsgBoxIcon.MASK)
{
case (int)MsgBoxIcon.MB_ICONHAND:
return System.Windows.Forms.MessageBoxIcon.Hand;
case (int)MsgBoxIcon.MB_ICONQUESTION:
return System.Windows.Forms.MessageBoxIcon.Question;
case (int)MsgBoxIcon.MB_ICONEXCLAMATION:
return System.Windows.Forms.MessageBoxIcon.Exclamation;
case (int)MsgBoxIcon.MB_ICONASTERISK:
return System.Windows.Forms.MessageBoxIcon.Asterisk;
case (int)MsgBoxIcon.MB_ICONNONE:
case (int)MsgBoxIcon.MB_USERICON:
default:
return System.Windows.Forms.MessageBoxIcon.None;
}
}
}
public System.Windows.Forms.MessageBoxDefaultButton DlgDefaultButtons
{
get
{
switch (_dwType & (int)MsgBoxDefButton.MASK)
{
case (int)MsgBoxDefButton.MB_DEFBUTTON2:
return System.Windows.Forms.MessageBoxDefaultButton.Button2;
case (int)MsgBoxDefButton.MB_DEFBUTTON3:
return System.Windows.Forms.MessageBoxDefaultButton.Button3;
case (int)MsgBoxDefButton.MB_DEFBUTTON1:
default:
return System.Windows.Forms.MessageBoxDefaultButton.Button1;
}
}
}
public System.Windows.Forms.MessageBoxOptions DlgOptions
{
get
{
switch (_dwType & (int)MsgBoxOption.MASK)
{
case (int)MsgBoxOption.MB_DEFAULT_DESKTOP_ONLY:
return System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnly;
case (int)MsgBoxOption.MB_RIGHT:
return System.Windows.Forms.MessageBoxOptions.RightAlign;
case (int)MsgBoxOption.MB_RTLREADING:
return System.Windows.Forms.MessageBoxOptions.RtlReading;
case (int)MsgBoxOption.MB_SERVICE_NOTIFICATION1:
case (int)MsgBoxOption.MB_SERVICE_NOTIFICATION2:
return System.Windows.Forms.MessageBoxOptions.ServiceNotification;
default:
return 0;
}
}
}
public bool displayHelpButton
{
get { return (_dwType & (int)MsgBoxHelpButton.MASK) == (int)MsgBoxHelpButton.MB_HELP; }
}
private string _lpstrHelpFile;
public string lpstrHelpFile
{
get { return _lpstrHelpFile; }
}
private uint _dwHelpContext;
public uint dwHelpContext
{
get { return _dwHelpContext; }
}
public ExtendedBrowserMessageEventArgs(IntPtr Hwnd, string LpstrText, string LpstrCaption, uint DwType, string LpstrHelpFile, uint DwHelpContext)
: base()
{
this._hwnd = Hwnd;
_lpstrText = LpstrText;
_lpstrCaption = LpstrCaption;
_dwType = DwType;
_lpstrHelpFile = LpstrHelpFile;
_dwHelpContext = DwHelpContext;
switch (DlgButtons)
{
case System.Windows.Forms.MessageBoxButtons.OKCancel:
case System.Windows.Forms.MessageBoxButtons.RetryCancel:
case System.Windows.Forms.MessageBoxButtons.YesNoCancel:
DlgResult = System.Windows.Forms.DialogResult.Cancel;
break;
case System.Windows.Forms.MessageBoxButtons.YesNo:
DlgResult = System.Windows.Forms.DialogResult.No;
break;
case System.Windows.Forms.MessageBoxButtons.AbortRetryIgnore:
DlgResult = System.Windows.Forms.DialogResult.Abort;
break;
case System.Windows.Forms.MessageBoxButtons.OK:
default:
DlgResult = System.Windows.Forms.DialogResult.OK;
break;
}
}
}
public enum MsgBoxButton
{
MASK = 0x0000000F,
MB_OK = 0x00000000,
MB_OKCANCEL = 0x00000001,
MB_ABORTRETRYIGNORE = 0x00000002,
MB_YESNOCANCEL = 0x00000003,
MB_YESNO = 0x00000004,
MB_RETRYCANCEL = 0x00000005
}
public enum MsgBoxIcon
{
MASK = 0x000000F0,
MB_ICONNONE = 0x00000000,
MB_ICONHAND = 0x00000010,
MB_ICONSTOP = 0x00000010,
MB_ICONERROR = 0x00000010,
MB_ICONQUESTION = 0x00000020,
MB_ICONEXCLAMATION = 0x00000030,
MB_ICONWARNING = 0x00000030,
MB_ICONASTERISK = 0x00000040,
MB_ICONINFORMATION = 0x00000040,
MB_USERICON = 0x00000080
}
public enum MsgBoxDefButton
{
MASK = 0x00000F00,
MB_DEFBUTTON1 = 0x00000000,
MB_DEFBUTTON2 = 0x00000100,
MB_DEFBUTTON3 = 0x00000200
}
public enum MsgBoxOption
{
MASK = 0x003F0000,
MB_DEFAULT_DESKTOP_ONLY = 0x00020000,
MB_RIGHT = 0x00080000,
MB_RTLREADING = 0x00100000,
MB_SERVICE_NOTIFICATION1 = 0x00200000,
MB_SERVICE_NOTIFICATION2 = 0x00040000
}
public enum MsgBoxHelpButton
{
MASK = 0x00004000,
MB_HELP = 0x00004000
}
#endregion
}
v
在WebBrowser中截获弹出对话框内容并将其屏蔽的更多相关文章
- Eclipse无法正常启动,弹出对话框内容为 A Java Runtime...
1.Eclipse无法正常启动,弹出对话框内容为 A Java Runtime...如下图: 原因分析:由于软件版本的更新或者安装其他开发软件无意之间修改了配置文件中的路径,众所周知,Java虚拟机( ...
- SharePoint 2010 Pop-Up Dialogs SharePoint 2010 弹出对话框
SharePoint 2010 Pop-Up Dialogs SharePoint 2010 弹出对话框 SharePoint 2010 使得往你的站点加入对话框内容变得出乎意料的简单 ...
- AlertDialog.Builder弹出对话框
在Android中,弹出对话框使用AlertDialog.Builder方法. new AlertDialog.Builder(MainActivity.this).setTitle("本机 ...
- Windows服务(system权限)程序显示界面与用户交互,Session0通知Session1里弹出对话框(真的很牛) good
源码资源下载:http://download.csdn.net/detail/stony1980/4512984 1.VC2008中编写“Windows服务”(Windows Service)程序 ...
- form WebBrowser自动点击弹出提示框alert、弹出对话框confirm、屏蔽弹出框、屏蔽弹出脚本错误的解决办法
针对WebBrowser控件中自动点击弹出框及禁用脚本提示问题得到如下几种实际情况的解决办法,绝对管用. 1.屏蔽弹出错误脚本 将WebBrowser控件ScriptErrorsSuppressed设 ...
- JavaScript中的三种弹出对话框
学习过js的小伙伴会发现,我们在一些实例中用到了alert()方法.prompt()方法.prompt()方法,他们都是在屏幕上弹出一个对话框,并且在上面显示括号内的内容,使用这种方法使得页面的交互性 ...
- 经验总结:WebBrowser自动点击弹出提示框alert、弹出对话框confirm、屏蔽弹出框、屏蔽弹出脚本错误的解决办法
经验总结:WebBrowser自动点击弹出提示框alert.弹出对话框confirm.屏蔽弹出框.屏蔽弹出脚本错误的解决办法 网上有好多解决方法,可是不一定好使,本人经过多次试验,针对WebBrows ...
- CGridCtrl只点击规定行中的按钮才弹出对话框
在头文件中添加: afx_msg void OnClick(NMHDR* pNMHDR, LRESULT* pResult); 添加映射:ON_NOTIFY(NM_CLICK, IDC_CUSTOM1 ...
- 10.JAVA之GUI编程弹出对话框Dialog
在上节基础上添加对话框显示错误信息. 代码如下: /*弹出对话框显示错误信息,对话框一般不单独出现,一般依赖于窗体.*/ /*练习-列出指定目录内容*/ import java.awt.Button; ...
随机推荐
- 如何做好GPS平台软硬件集成测试
集成测试是为了构建一个更大的系统或平台,这个系统的几个部分通常是由不同的团队或甚至不同的公司开发的,以前在做信息化的软件开发时,面临的集成测试通常是不同软件子系统之间的集成测试,往往被这一阶段的测试搞 ...
- 限制action所接受的请求方式或请求参数
原文:http://www.cnblogs.com/liukemng/p/3726897.html 2.限制action所接受的请求方式(get或post): 之前我们在HelloWorldContr ...
- hibernate 多表查询
Hibernate主要支持两种查询方式:HQL查询和Criteria查询.前者应用较为广发,后者也只是调用封装好的接口. 现在有一个问题,就是实现多表连接查询,且查询结果集不与任何一个实体类对应,怎么 ...
- CryptographicException异常处理方法
在调用System.Security.Cryptography.ProtectedData.Protect方法来保护私密信息时,IIS可能会报以下错误:CryptographicException: ...
- c#制作一个屏幕保护程序
代码已上传github 实现思路:纯黑窗体去边框,加入标签. 使用Timmer让windows 10标签运动.限制标签的行为. 代码: int deltX = 10; int deltY ...
- SQL 还原数据库
1. 查看 SQL Server 2000 中 Northwind 数据库文件的逻辑文件名(logical file name)和物理文件路径(operation system file name): ...
- MySql SELECT INTO 记录为空的问题
SELECT Id INTO @Id FROM GiftCode WHERE Status = 1 AND GiftId = #GiftId# ORDER BY Id ASC LIMIT 1; ...
- ffmpeg编译参数详解
Usage: configure [options]用 法:configure [选项]Options: [defaults in brackets after descriptions]选 项: ...
- 报表控件NCreport教程:报表高级设计
本次文章中将讲解NCreport一些高级功能的应用,我们会先定义一个组,接下来会添加summary变量到示例报表中. 一.对summary添加变量 对于提供的数量和总量来说,变量是特殊的数值项,它们每 ...
- s3c2440 上txt 小说阅读器
文件结构 Makefile: CROSSCOMPILE := arm-linux- CFLAGS := -Wall -O2 -c LDFLAGS := -lm -lfreetype CC := $(C ...