允许WPF WebBrowser中的弹出窗口



有没有办法允许嵌入式WPF WebBrowser Control中的弹出窗口?我没有设法找到某人的Elses解决方案,也没有启用允许弹出的COM接口。

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

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

已更新以解决注释。要禁用内置弹出式阻滞剂,您需要为FEATURE_WEBOC_POPUPMANAGEMENT实现WebBrowser功能控件。您确实需要访问Registry.CurrentUser Hive,但这不需要管理权。下面的代码显示了如何做。

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(@"SoftwareMicrosoftInternet ExplorerMainFeatureControl", 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禁用弹出阻滞剂。我自己没有尝试过那个场地。

相关内容

  • 没有找到相关文章

最新更新