如何在WebBrowser控件中拦截onbeforenload事件

本文关键字:onbeforenload 事件 控件 WebBrowser | 更新日期: 2023-09-27 18:20:31

我有一个WinForms应用程序,在该应用程序中,我在WebBrowser控件中托管了一个网页。

网页内容如下:

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
  <title>onbeforeunload test</title>
  <meta charset="utf-8">
</head>
<body>
<a href="#" onclick="window.location.reload();">Test</a>
<script type="text/javascript">
    window.onbeforeunload = function () {
        return 'Are you sure you want to leave this page?';
    };
</script>
</body>
</html>

正如您所看到的,我已经订阅了onbeforeunload事件,该事件允许在离开此页面之前显示确认对话框。当我点击重新加载页面的锚点时,这很好。此时会显示确认框,用户可以取消页面的重新加载。这在WinForms托管的控件中运行良好。

现在,当用户关闭WinForms应用程序(例如单击X按钮)时,我遇到的困难是拦截并执行此事件。

我可以在WinForms应用程序中获取此函数的内容,但无论我尝试了什么,我都无法获取此函数返回的字符串的内容,以便稍后在用户尝试关闭应用程序时使用它来伪造MessageBox:

webBrowser1.Navigated += (sender, e) =>
{
    webBrowser1.Document.Window.Load += (s, ee) =>
    {
        // In order to get the IHTMLWindow2 interface I have referenced
        // the Microsoft HTML Object Library (MSHTML) COM control
        var window = (IHTMLWindow2)webBrowser1.Document.Window.DomWindow;
        // the bu variable contains the script of the onbeforeunload event
        var bu = window.onbeforeunload();
        // How to get the string that is returned by this function
        // so that I can subscribe to the Close event of the WinForms application
        // and show a fake MessageBox with the same text?
    };
};
webBrowser1.Navigate("file:///c:/index.htm");

我已经尝试了window.execScript方法,但没有可用的:

// returns null
var script = string.Format("({0})();", bu);
var result = window.execScript(script, "javascript");

我也尝试过以下操作,但也返回了null:

var result = window.execScript("(function() { return 'foo'; })();", "javascript");

作为最后的手段,我可以使用第三方javascript解析器,我可以向它提供这个函数的主体,它会执行它并给我返回值,但这确实是最后的手段。如果有一种更原生的方式来使用微软的MSHTML库,我会很高兴。


更新:

由于@Hans提供了出色的答案,这个问题现在得到了解决。由于某种原因,我无法使他的解决方案在我的测试机上运行(Win7 x64,.NET 4.0 Client Profile,IE9,en-US locale),并且我总是在IDispatch.Invoke调用后得到hr = -2147024809。因此,我已将IDispatch P/Invoke签名修改为以下内容(此签名不需要在项目中添加对c:'windows'system32'stdole2.tlb的引用):

using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("00020400-0000-0000-C000-000000000046")]
public interface IDispatch
{
    [PreserveSig]
    int GetTypeInfoCount(out int Count);
    [PreserveSig]
    int GetTypeInfo(
        [MarshalAs(UnmanagedType.U4)] int iTInfo,
        [MarshalAs(UnmanagedType.U4)] int lcid, 
        out ITypeInfo typeInfo
    );
    [PreserveSig]
    int GetIDsOfNames(
        ref Guid riid,
        [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr)] string[] rgsNames, 
        int cNames, 
        int lcid, 
        [MarshalAs(UnmanagedType.LPArray)] int[] rgDispId
    );
    [PreserveSig]
    int Invoke(
        int dispIdMember, 
        ref Guid riid, 
        uint lcid, 
        ushort wFlags,
        ref System.Runtime.InteropServices.ComTypes.DISPPARAMS pDispParams, 
        out object pVarResult,
        ref System.Runtime.InteropServices.ComTypes.EXCEPINFO pExcepInfo, 
        IntPtr[] pArgErr
    );
}

然后我订阅了表单的关闭事件,能够获取onbeforeunload事件返回的消息并提示用户:

protected override void OnFormClosing(FormClosingEventArgs e)
{
    var window = (IHTMLWindow2)webBrowser1.Document.Window.DomWindow;
    var args = new System.Runtime.InteropServices.ComTypes.DISPPARAMS();
    var result = new object();
    var except = new System.Runtime.InteropServices.ComTypes.EXCEPINFO();
    var idisp = window.onbeforeunload as IDispatch;
    if (idisp != null)
    {
        var iid = Guid.Empty;
        var lcid = (uint)CultureInfo.CurrentCulture.LCID;
        int hr = idisp.Invoke(0, ref iid, lcid, 1, ref args, out result, ref except, null);
        if (hr == 0)
        {
            var msgBox = MessageBox.Show(
                this,
                (string)result,
                "Confirm",
                MessageBoxButtons.OKCancel
            );
            e.Cancel = msgBox == DialogResult.Cancel;
        }
    }
    base.OnFormClosing(e);
}

如何在WebBrowser控件中拦截onbeforenload事件

IHtmlWindow2.onbeforeunload本身不显示对话框。它只返回一个字符串,然后主机必须在消息框中使用该字符串。由于Winforms应用程序是主机,它必须使用MessageBox.Show()。调用onbeforenload很困难,它是一个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);                                                       
    }

我刚刚处理了一个类似的问题。这是一个迟来的答案,但希望它能帮助到一些人。该解决方案基于这篇MSKB文章。它也适用于网页通过attachEventaddEventListener处理onbeforenload事件的情况。

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;
    }
}

以上代码适用于WebBrowser控件的WinForms版本。对于WPF版本,应首先通过反射获得ActiveXInstance

 var activeX = this.WB.GetType().InvokeMember("ActiveXInstance",
                    BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
                    null, this.WB, new object[] { }) as SHDocVw.WebBrowser;

如果您愿意过早地执行事件代码(可以是任何东西),下面的代码会在您的Window.Load中为我捕获字符串;

Object[] args = { @"(" + bu + ")();" };
string result = webBrowser1.Document.InvokeScript("eval", args).ToString();