允许WPF WebBrowser中的弹出窗口

时间:2014-01-31 00:09:34

标签: .net wpf webbrowser-control iwebbrowser2

有没有办法在嵌入式WPF WebBrowser控件中允许弹出窗口?我没有设法找到别人的解决方案,也没有找到允许弹出窗口的COM接口。

我不想更改用户注册表设置或使用类似的入侵方法,因为应用程序旨在通过ClickOnce分发。

1 个答案:

答案 0 :(得分:4)

您可以通过处理由底层WebBrowser ActiveX控件提供的NewWindow2NewWindow3事件来实现自定义弹出窗口。以下是如何执行此操作的一个非常基本的示例。它可以通过可重用的WebBrowser控件进一步改进,以支持弹出窗口中的弹出窗口。

已更新以解决评论。要禁用内置弹出窗口阻止程序,您需要为FEATURE_WEBOC_POPUPMANAGEMENT实施WebBrowser功能控件。您确实需要访问Registry.CurrentUser配置单元,但这并不需要管理员权限。下面的代码显示了如何执行此操作。

using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Microsoft.Win32;


namespace WpfWbApp
{
    public partial class MainWindow : Window
    {
        WebBrowser webBrowser;

        public MainWindow()
        {
            SetBrowserFeatureControl();

            InitializeComponent();

            this.webBrowser = new WebBrowser();
            this.Content = this.webBrowser;

            this.Loaded += MainWindow_Loaded;
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var axWebBrowser = (SHDocVw.WebBrowser)GetActiveXInstance(this.webBrowser);
            axWebBrowser.NewWindow2 += axWebBrowser_NewWindow2;

            this.webBrowser.Navigate("http://example.com");
        }

        void axWebBrowser_NewWindow2(ref object ppDisp, ref bool Cancel)
        {
            var window = new Window { Width = 400, Height = 300 };
            var newWebBrowser = new WebBrowser();
            window.Content = newWebBrowser;
            window.Show();
            ppDisp = GetActiveXInstance(newWebBrowser);
        }

        /// <summary>
        /// Get the underlying WebBrowser ActiveX object;
        /// this code depends on SHDocVw.dll COM interop assembly,
        /// generate SHDocVw.dll: "tlbimp.exe ieframe.dll",
        /// and add as a reference to the project
        /// </summary>
        static object GetActiveXInstance(WebBrowser browser)
        {
            var document = browser.Document;

            return browser.GetType().InvokeMember("ActiveXInstance",
                BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
                null, browser, new object[] { }) as SHDocVw.WebBrowser;
        }

        /// <summary>
        /// SetBrowserFeatureControlKey
        /// </summary>
        static void SetBrowserFeatureControlKey(string feature, string appName, uint value)
        {
            using (var key = Registry.CurrentUser.CreateSubKey(
                string.Concat(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\", feature),
                RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                key.SetValue(appName, (uint)value, RegistryValueKind.DWord);
            }
        }

        /// <summary>
        /// SetBrowserFeatureControl
        /// </summary>
        static void SetBrowserFeatureControl()
        {
            // http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx

            // FeatureControl settings are per-process
            var fileName = System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);

            // make the control is not running inside Visual Studio Designer
            if (string.Compare(fileName, "devenv.exe", true) == 0 || string.Compare(fileName, "XDesProc.exe", true) == 0)
                return;

            // Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode.
            SetBrowserFeatureControlKey("FEATURE_BROWSER_EMULATION", fileName, 10000);

            // Web Browser Control Popup Management
            SetBrowserFeatureControlKey("FEATURE_WEBOC_POPUPMANAGEMENT", fileName, 0);
        }
    }
}

根据文档,也可以通过p / invoke使用CoInternetSetFeatureEnabledFEATURE_WEBOC_POPUPMANAGEMENT禁用弹出窗口阻止程序。我自己没有尝试过那个场地。