GeckoFX在保留滚动条的同时禁用表单输入

时间:2018-09-10 22:31:26

标签: c# geckofx window-handles

GeckoFX浏览器的属性Enabled确定整个浏览器是否可以获取输入。

但是,如果将其放置为false,则滚动条将根本无法单击或拖动。

我正在寻找一种禁用整个浏览器而不禁用滚动条的方法,简单来说,就是禁用所有内容,防止它们从表单获取输入。

1 个答案:

答案 0 :(得分:1)

我看到许多路线:而不是geckowebbrowser.Enabled = false;

  1. disable所有inputselecttextareabutton以及DOM上的链接,例如

    GeckoElementCollection byTag = _browser.Document.GetElementsByTagName("input");
    foreach (var ele in byTag)
    {
        var input = ele as GeckoInputElement;
        input.Disabled = true;
    }
    

    等。

  2. 从可点击元素中删除指针事件,例如

    var byTag = _browser.Document.GetElementsByTagName("a");
    foreach (var ele in byTag)
    {    
         var a = ele as GeckoHtmlElement;
        //a.SetAttribute("disabled", @"true");
        a.SetAttribute("style", "pointer-events: none;cursor: default;");
    }
    
  3. 使用不可见的CSS阻止程序覆盖图(jsfiddle),例如使用JavaScript

//UI block
window.onload = function() {        
    var blockUI = document.createElement("div");
    blockUI.setAttribute("id", "blocker");
    blockUI.innerHTML = '<div></div>'
    document.body.appendChild(blockUI);
    
    //unblock it
    //var cover = document.getElementById("blocker").style.display = "none";
}
#blocker
{
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0.0;
    background-color: #111;
    z-index: 9000;
    overflow: auto;
}
<button id="bloc">Blocked UI</button>

在我的WPF demo应用背后的代码中,在页面完成DocumentCompleted事件加载后,我添加了叠加层:

using Gecko;
using Gecko.DOM;
using System.Windows;
using System.Windows.Forms.Integration;
using System.Linq;    
namespace GeckoWpf {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
            Gecko.Xpcom.Initialize("Firefox");
        }    
        void browser_DocumentCompleted(object sender, System.EventArgs e) {
             //unsubscribe
            _browser.DocumentCompleted -= browser_DocumentCompleted;

            GeckoElement rt = _browser.Document.CreateElement("div");
            rt.SetAttribute("id", "blocker");
            rt.SetAttribute
            (
            "style",
            "position: fixed;"
            + "top: 0px;"
            + "left: 0px;"
            + "width: 100%;"
            + "height: 100%;"
            + "opacity: 0.0;"
            + "background-color: #111;"
            + "z-index: 9000;"
            + "overflow: auto;"
            );
            _browser.Document.Body.AppendChild(rt);
        }    
        WindowsFormsHost _host = new WindowsFormsHost();
        GeckoWebBrowser _browser = new GeckoWebBrowser();    
        private void Window_Loaded(object sender, RoutedEventArgs e) {
            _browser.DocumentCompleted += browser_DocumentCompleted;
            _host.Child = _browser;
            GridWeb.Children.Add(_host);    
            _browser.Navigate("https://www.google.com/");
        }
    }
}
  1. 在主应用程序窗口或Gecko Dom事件中覆盖OnClick事件,并将事件设置为e.Handled = true;

当然还有其他选择。

相关问题