如何截获WebBrowser控件onbeforeunload事件
2015-01-11
展开全部
本身不显示对话框。返回一个字符串在箱子的主机必须的。由于您的WinForms应用程序是主机它的MessageBox.show()。调用onbeforeunload的是困难的,它是一个IDispatch指针,其(DISPID 0)返回字符串。添加引用c:\windows\system32\stdole2.tlb并粘贴此代码:using System.Runtime.InteropServices;
...
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00020400-0000-0000-C000-000000000046")]
public interface IDispatch {
int dummy1();
int dummy2();
int dummy3();
[PreserveSig]
int Invoke(int dispIdMember, ref Guid riid, int lcid, int dwFlags,
[In, Out] stdole.DISPPARAMS pDispParams,
[Out, MarshalAs(UnmanagedType.LPArray)] object[] pVarResult,
[In, Out] stdole.EXCEPINFO pExcepInfo,
[Out, MarshalAs(UnmanagedType.LPArray)] IntPtr[] pArgErr);
}
你是这样的: protected override void OnFormClosing(FormClosingEventArgs e) {
var window = (IHTMLWindow2)webBrowser1.Document.Window.DomWindow;
var args = new stdole.DISPPARAMS();
var result = new object[1];
var except = new stdole.EXCEPINFO();
var idisp = (IDispatch)window.onbeforeunload;
var iid = Guid.Empty;
int hr = idisp.Invoke(0, ref iid, 1033, 1, args, result, except, null);
if (hr == 0) {
if (MessageBox.Show(this, (string)result[0], "Confirm",
MessageBoxButtons.OKCancel) == DialogResult.Cancel) e.Cancel = true;
}
base.OnFormClosing(e);
}
2. 如果你高兴过早执行事件代码(这可以是任何东西)的下列数据串在你的Window.Load;Object[] args = { @"(" + bu + ")();" };
string result = webBrowser1.Document.InvokeScript("eval", args).ToString();
3. 我刚刚处理了一个类似的问题。这是一个迟到的答案,但希望它可以帮助该解决方案基于此MSKB文章。它也适用于情况下,当网页通过处理onbeforeunload事件attachEvent或addEventListener。void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// this code depends on SHDocVw.dll COM interop assembly,
// generate SHDocVw.dll: "tlbimp.exe ieframe.dll",
// and add as a reference to the project
var activeX = this.webBrowser.ActiveXInstance;
object arg1 = Type.Missing;
object arg2 = true;
((SHDocVw.WebBrowser)activeX).ExecWB(SHDocVw.OLECMDID.OLECMDID_ONUNLOAD, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref arg1, ref arg2);
if (!(bool)arg2)
{
e.Cancel = true;
}
}
上面的代码是的WinForms版本WebBrowser控制。对于WPF版本,ActiveXInstance应通过反射首次获得: var activeX = this.WB.GetType().InvokeMember("ActiveXInstance",
BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
null, this.WB, new object[] { }) as SHDocVw.WebBrowser;
...
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00020400-0000-0000-C000-000000000046")]
public interface IDispatch {
int dummy1();
int dummy2();
int dummy3();
[PreserveSig]
int Invoke(int dispIdMember, ref Guid riid, int lcid, int dwFlags,
[In, Out] stdole.DISPPARAMS pDispParams,
[Out, MarshalAs(UnmanagedType.LPArray)] object[] pVarResult,
[In, Out] stdole.EXCEPINFO pExcepInfo,
[Out, MarshalAs(UnmanagedType.LPArray)] IntPtr[] pArgErr);
}
你是这样的: protected override void OnFormClosing(FormClosingEventArgs e) {
var window = (IHTMLWindow2)webBrowser1.Document.Window.DomWindow;
var args = new stdole.DISPPARAMS();
var result = new object[1];
var except = new stdole.EXCEPINFO();
var idisp = (IDispatch)window.onbeforeunload;
var iid = Guid.Empty;
int hr = idisp.Invoke(0, ref iid, 1033, 1, args, result, except, null);
if (hr == 0) {
if (MessageBox.Show(this, (string)result[0], "Confirm",
MessageBoxButtons.OKCancel) == DialogResult.Cancel) e.Cancel = true;
}
base.OnFormClosing(e);
}
2. 如果你高兴过早执行事件代码(这可以是任何东西)的下列数据串在你的Window.Load;Object[] args = { @"(" + bu + ")();" };
string result = webBrowser1.Document.InvokeScript("eval", args).ToString();
3. 我刚刚处理了一个类似的问题。这是一个迟到的答案,但希望它可以帮助该解决方案基于此MSKB文章。它也适用于情况下,当网页通过处理onbeforeunload事件attachEvent或addEventListener。void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// this code depends on SHDocVw.dll COM interop assembly,
// generate SHDocVw.dll: "tlbimp.exe ieframe.dll",
// and add as a reference to the project
var activeX = this.webBrowser.ActiveXInstance;
object arg1 = Type.Missing;
object arg2 = true;
((SHDocVw.WebBrowser)activeX).ExecWB(SHDocVw.OLECMDID.OLECMDID_ONUNLOAD, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref arg1, ref arg2);
if (!(bool)arg2)
{
e.Cancel = true;
}
}
上面的代码是的WinForms版本WebBrowser控制。对于WPF版本,ActiveXInstance应通过反射首次获得: var activeX = this.WB.GetType().InvokeMember("ActiveXInstance",
BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
null, this.WB, new object[] { }) as SHDocVw.WebBrowser;
推荐于2016-08-08
展开全部
本身不显示对话框。返回一个字符串在箱子的主机必须的。由于您的WinForms应用程序是主机它的MessageBox.show()。调用onbeforeunload的是困难的,它是一个IDispatch指针,其(DISPID 0)返回字符串。添加引用c:\windows\system32\stdole2.tlb并粘贴此代码:using System.Runtime.InteropServices;
...
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00020400-0000-0000-C000-000000000046")]
public interface IDispatch {
int dummy1();
int dummy2();
int dummy3();
[PreserveSig]
int Invoke(int dispIdMember, ref Guid riid, int lcid, int dwFlags,
[In, Out] stdole.DISPPARAMS pDispParams,
[Out, MarshalAs(UnmanagedType.LPArray)] object[] pVarResult,
[In, Out] stdole.EXCEPINFO pExcepInfo,
[Out, MarshalAs(UnmanagedType.LPArray)] IntPtr[] pArgErr);
}
你是这样的: protected override void OnFormClosing(FormClosingEventArgs e) {
var window = (IHTMLWindow2)webBrowser1.Document.Window.DomWindow;
var args = new stdole.DISPPARAMS();
var result = new object[1];
var except = new stdole.EXCEPINFO();
var idisp = (IDispatch)window.onbeforeunload;
var iid = Guid.Empty;
int hr = idisp.Invoke(0, ref iid, 1033, 1, args, result, except, null);
if (hr == 0) {
if (MessageBox.Show(this, (string)result[0], "Confirm",
MessageBoxButtons.OKCancel) == DialogResult.Cancel) e.Cancel = true;
}
base.OnFormClosing(e);
}
2. 如果你高兴过早执行事件代码(这可以是任何东西)的下列数据串在你的Window.Load;Object[] args = { @"(" + bu + ")();" };
string result = webBrowser1.Document.InvokeScript("eval", args).ToString();
3. 我刚刚处理了一个类似的问题。这是一个迟到的答案,但希望它可以帮助该解决方案基于此MSKB文章。它也适用于情况下,当网页通过处理onbeforeunload事件attachEvent或addEventListener。void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// this code depends on SHDocVw.dll COM interop assembly,
// generate SHDocVw.dll: "tlbimp.exe ieframe.dll",
// and add as a reference to the project
var activeX = this.webBrowser.ActiveXInstance;
object arg1 = Type.Missing;
object arg2 = true;
((SHDocVw.WebBrowser)activeX).ExecWB(SHDocVw.OLECMDID.OLECMDID_ONUNLOAD, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref arg1, ref arg2);
if (!(bool)arg2)
{
e.Cancel = true;
}
}
上面的代码是的WinForms版本WebBrowser控制。对于WPF版本,ActiveXInstance应通过反射首次获得: var activeX = this.WB.GetType().InvokeMember("ActiveXInstance",
BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
null, this.WB, new object[] { }) as SHDocVw.WebBrowser;
...
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00020400-0000-0000-C000-000000000046")]
public interface IDispatch {
int dummy1();
int dummy2();
int dummy3();
[PreserveSig]
int Invoke(int dispIdMember, ref Guid riid, int lcid, int dwFlags,
[In, Out] stdole.DISPPARAMS pDispParams,
[Out, MarshalAs(UnmanagedType.LPArray)] object[] pVarResult,
[In, Out] stdole.EXCEPINFO pExcepInfo,
[Out, MarshalAs(UnmanagedType.LPArray)] IntPtr[] pArgErr);
}
你是这样的: protected override void OnFormClosing(FormClosingEventArgs e) {
var window = (IHTMLWindow2)webBrowser1.Document.Window.DomWindow;
var args = new stdole.DISPPARAMS();
var result = new object[1];
var except = new stdole.EXCEPINFO();
var idisp = (IDispatch)window.onbeforeunload;
var iid = Guid.Empty;
int hr = idisp.Invoke(0, ref iid, 1033, 1, args, result, except, null);
if (hr == 0) {
if (MessageBox.Show(this, (string)result[0], "Confirm",
MessageBoxButtons.OKCancel) == DialogResult.Cancel) e.Cancel = true;
}
base.OnFormClosing(e);
}
2. 如果你高兴过早执行事件代码(这可以是任何东西)的下列数据串在你的Window.Load;Object[] args = { @"(" + bu + ")();" };
string result = webBrowser1.Document.InvokeScript("eval", args).ToString();
3. 我刚刚处理了一个类似的问题。这是一个迟到的答案,但希望它可以帮助该解决方案基于此MSKB文章。它也适用于情况下,当网页通过处理onbeforeunload事件attachEvent或addEventListener。void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// this code depends on SHDocVw.dll COM interop assembly,
// generate SHDocVw.dll: "tlbimp.exe ieframe.dll",
// and add as a reference to the project
var activeX = this.webBrowser.ActiveXInstance;
object arg1 = Type.Missing;
object arg2 = true;
((SHDocVw.WebBrowser)activeX).ExecWB(SHDocVw.OLECMDID.OLECMDID_ONUNLOAD, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref arg1, ref arg2);
if (!(bool)arg2)
{
e.Cancel = true;
}
}
上面的代码是的WinForms版本WebBrowser控制。对于WPF版本,ActiveXInstance应通过反射首次获得: var activeX = this.WB.GetType().InvokeMember("ActiveXInstance",
BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
null, this.WB, new object[] { }) as SHDocVw.WebBrowser;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用history API方法解决:
1)go.html
XML/HTML code:
<html>
<head>
<title>Go</title>
</head>
<body>
<input type="button" value="Go" onclick="window.location.href = 'back.html#no-back';" />
</body>
</html>
2)back.html:
XML/HTML code
<html>
<head>
<title>No Backwards</title>
<script type="text/javascript">
var history_api = typeof history.pushState !== 'undefined';
if (location.hash == '#no-back') {
if (history_api)
history.pushState(null, '', '#stay');
else
location.hash = '#stay';
window.onhashchange = function() {
if (location.hash == '#no-back') {
alert("No backwards!");
if (history_api)
history.pushState(null, '', '#stay');
else
location.hash = '#stay';
}
}
}
</script>
</head>
<body>
<div>
No backwards...</div>
</body>
</html>
1)go.html
XML/HTML code:
<html>
<head>
<title>Go</title>
</head>
<body>
<input type="button" value="Go" onclick="window.location.href = 'back.html#no-back';" />
</body>
</html>
2)back.html:
XML/HTML code
<html>
<head>
<title>No Backwards</title>
<script type="text/javascript">
var history_api = typeof history.pushState !== 'undefined';
if (location.hash == '#no-back') {
if (history_api)
history.pushState(null, '', '#stay');
else
location.hash = '#stay';
window.onhashchange = function() {
if (location.hash == '#no-back') {
alert("No backwards!");
if (history_api)
history.pushState(null, '', '#stay');
else
location.hash = '#stay';
}
}
}
</script>
</head>
<body>
<div>
No backwards...</div>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询