允许在WPF web浏览器弹出窗口 [英] Allow popups in WPF WebBrowser

查看:752
本文介绍了允许在WPF web浏览器弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法,让内嵌入WPF WebBrowser控件的弹出窗口?我没找别人解决方案,也不是COM接口,可以允许弹出窗口。

Is there a way to allow popups within embedded WPF WebBrowser control? I didn't manage to find someone elses solution, nor COM interface which enables allowing popups.

我不希望更改用户注册表设置或使用类似的侵入性方法,因为应用程序的目的是通过ClickOnce的分配。

I wouldn't like to change users registry settings or use similar invasive methods, as application is intended to be distributed through ClickOnce.

推荐答案

您可以通过处理的 NewWindow2 或的 NewWindow3 源为基础的WebBrowser ActiveX控件的事件。下面是如何做到这一点非常简单的例子。它可以用一个可重复使用 web浏览器为基础的控制,从弹出窗口支持弹出窗口得到进一步改善。

You can implement custom popups by handling NewWindow2 or NewWindow3 events sourced by the underlying WebBrowser ActiveX control. Below is a very basic example of how to do this. It can be further improved with a re-usable WebBrowser-based control, to support popups from popups.

更新,以解决意见。要禁用内置的弹出窗口阻止程序,则需要实施的 FEATURE_WEBOC_POPUPMANAGEMENT 。你需要访问 Registry.CurrentUser 蜂巢,但这并不需要管理员权限。在code以下显示了如何做到这一点。

Updated to address the comment. To disable the built-in pop-up blocker, you need to implement WebBrowser Feature Control for FEATURE_WEBOC_POPUPMANAGEMENT. You do need to access Registry.CurrentUser hive, but that doesn't require admin rights. The code below shows how to do it.

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

根据文档,在弹出式窗口拦截器也可以使用的 CoInternetSetFeatureEnabled 和的 FEATURE_WEBOC_POPUPMANAGEMENT ,通过P /调用。我还没有试过,移步自己。

According to the docs, the pop-up blocker can also be disabled with CoInternetSetFeatureEnabled and FEATURE_WEBOC_POPUPMANAGEMENT, via p/invoke. I haven't tried that venue myself.

这篇关于允许在WPF web浏览器弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆