WebBrowser Control 禁用鼠标点击 [英] WebBrowser Control disable mouse clicks

查看:44
本文介绍了WebBrowser Control 禁用鼠标点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 WebBrowser 控件中显示 YouTube 视频,但我想禁用所有用户交互(没有鼠标点击,没有键盘事件......).

我正在捕获所有控件的预览、鼠标和键盘事件,此外我还为加载的 HTML 文档添加了一些处理程序,但没有任何成功:

 void webBrowser1_DocumentCompleted( object sender, WebBrowserDocumentCompletedEventArgs e ){if(webBrowser1.Document != null) {var doc = webBrowser1.Document;doc.Body.Style =溢出:隐藏";doc.Click += htmlDoc_Click;doc.MouseDown += htmlDoc_MouseDown;doc.MouseMove += htmlDoc_MouseMove;webBrowser1.Document.Body.Click += new HtmlElementEventHandler(htmlDoc_Click);webBrowser1.Document.Body.MouseDown += new HtmlElementEventHandler(Document_MouseDown);webBrowser1.Document.Body.MouseUp += new HtmlElementEventHandler(Document_MouseMove);webBrowser1.Document.Body.MouseUp += new HtmlElementEventHandler(Document_MouseUp);HtmlElement head = doc.GetElementsByTagName("head")[0];HtmlElement mscript = doc.CreateElement("script");IHTMLScriptElement 元素 = (IHTMLScriptElement)mscript.DomElement;element.text = "function handleMouseEvent(e) { ";+ "var evt = (e==null ? event:e);"+ "返回真;} "+ "document.onmousedown = handleMouseEvent;"+ "document.onmouseup = handleMouseEvent;"+ "document.onclick = handleMouseEvent;";head.AppendChild(mscript);}}

也可以在前面"覆盖一个透明的控件.WebBrowser 控件.

解决方案

这是一个从标准 WinForms 面板派生的自定义控件,修改为完全透明但实体"(接收鼠标事件).

使用 CreateParams 添加 ExStyle = WS_EX_TRANSPARENT;

另外,Control.SetStyle() 方法用于修改控件行为,添加这些 ControlStyles:

ControlStyles.Opaque 阻止绘制控件 BackGround,因此它不受系统管理.
ControlStyles.SupportsTransparentBackColor 允许控件接受其背景颜色的 Alpha 值.
ControlStyles.ResizeRedraw 导致控件在调整大小时重绘.

自定义控件被初始化并传递它必须覆盖的控件的引用.然后它将自身调整为该引用控件的大小,从该度量中排除 ScrollBar,以便可以使用它们.
<小时>要使其正常工作,请创建对 OverlayPanel 类的引用并调用辅助方法 CreateOverlay(Control control):

private OverlayPanel overlayPanel;private void CreateOverlay(控制控件){overlayPanel = 新的 OverlayPanel(this.webBrowser1);Controls.Add(overlayPanel);覆盖面板.BringToFront();}

OverlayPanel 类代码可以插入到一个 Form 中,也可以插入到它自己的类文件中.
当一个 Form 中的所有控件都已经设置了它们的尺寸时,应该创建它:在Form.Shown() 事件或父窗体可见的任何其他时间.无论如何,Form.Load() 事件在大多数情况下也可能正常工作.

请注意,此 OverlayPanel 目前没有 Resize 方法,如果在某个时候调整覆盖控件的大小,则需要该方法.但这是一个非常简单的实现,如果需要的话.

私有类 OverlayPanel : Panel{内部 int WS_EX_TRANSPARENT = 0x00000020;公共覆盖面板(控制 RefControl){初始化组件();this.Size = new Size(RefControl.Size.Width - SystemInformation.VerticalScrollBarWidth,RefControl.Size.Height - SystemInformation.Horizo​​ntalScrollBarHeight);this.Location = RefControl.Location;}私有无效InitializeComponent(){this.SetStyle(ControlStyles.Opaque |ControlStyles.ResizeRedraw |ControlStyles.SupportsTransparentBackColor, true);this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);this.BorderStyle = BorderStyle.None;}受保护的覆盖 CreateParams CreateParams{得到{CreateParams 参数 = base.CreateParams;参数.ExStyle |= WS_EX_TRANSPARENT;返回参数;}}}

I want to show a YouTube video inside a WebBrowser control, but I want to disable all user interactions (no mouse clicks no keyboard events...).

I am catching all control's preview, mouse and keyboard events and furthermore I put some handlers to the loaded HTML document, but without any success:

 void webBrowser1_DocumentCompleted( object sender, WebBrowserDocumentCompletedEventArgs e )
 {
     if(webBrowser1.Document != null) {
         var doc = webBrowser1.Document;
         
         doc.Body.Style = "overflow:hidden";
         doc.Click      += htmlDoc_Click;
         doc.MouseDown  += htmlDoc_MouseDown;
         doc.MouseMove  += htmlDoc_MouseMove;
             
         webBrowser1.Document.Body.Click     += new HtmlElementEventHandler(htmlDoc_Click);
         webBrowser1.Document.Body.MouseDown += new HtmlElementEventHandler(Document_MouseDown);
         webBrowser1.Document.Body.MouseUp   += new HtmlElementEventHandler(Document_MouseMove);
         webBrowser1.Document.Body.MouseUp   += new HtmlElementEventHandler(Document_MouseUp);
         
         
         HtmlElement head = doc.GetElementsByTagName("head")[0];
         HtmlElement mscript = doc.CreateElement("script");
         IHTMLScriptElement element = (IHTMLScriptElement)mscript.DomElement;
         element.text = "function handleMouseEvent(e) { "
                      + "var evt = (e==null ? event:e); "
                      + "return true; } "
                      + "document.onmousedown = handleMouseEvent; "
                      + "document.onmouseup   = handleMouseEvent; "
                      + "document.onclick     = handleMouseEvent; ";
         head.AppendChild(mscript);
     }
 }

Also it would be fine to overlay a transparent Control "in front of" the WebBrowser control.

解决方案

This is a custom control derived from a standard WinForms Panel, modified to be completely transparent but "solid" (receives the Mouse events).

The transparency is achieved using CreateParams adding an ExStyle = WS_EX_TRANSPARENT;

Also, Control.SetStyle() method is used to modify the control behaviour, adding these ControlStyles:

ControlStyles.Opaque prevents the painting of the control BackGround, so it's not managed by the System.
ControlStyles.SupportsTransparentBackColor allows the control to accept Alpha values for it's background color.
ControlStyles.ResizeRedraw causes the redrawing of the control when it's resized.

The Custom Control is initialized passing a reference of the control it has to overlay. It then resizes itself to the size of this referenced control, excluding the ScrollBars from this measure, so they can be used.


To set it to work, create a reference to the OverlayPanel class and call the helper method CreateOverlay(Control control):

private OverlayPanel overlayPanel;

private void CreateOverlay(Control control)
{
    overlayPanel = new OverlayPanel(this.webBrowser1);
    Controls.Add(overlayPanel);
    overlayPanel.BringToFront();
}

The OverlayPanel class code can be inserted in a Form or in a class file of its own.
It should be created when all controls in a Form already have their dimensions set: in the Form.Shown() event or any other time when the parent form is visible. The Form.Load() event might also well work most of the time, anyway.

As a note, this OverlayPanel doesn't have a Resize method at this moment, which is required if the overlayed control is resized at some point. But it's quite a simple implementation, should it be needed.

private class OverlayPanel : Panel
{
    internal int WS_EX_TRANSPARENT = 0x00000020;
    public OverlayPanel(Control RefControl)
    {
        InitializeComponent();
        this.Size = new Size(RefControl.Size.Width - SystemInformation.VerticalScrollBarWidth,
                             RefControl.Size.Height - SystemInformation.HorizontalScrollBarHeight);
        this.Location = RefControl.Location;
    }
    private void InitializeComponent()
    {
        this.SetStyle(ControlStyles.Opaque | 
                      ControlStyles.ResizeRedraw |
                      ControlStyles.SupportsTransparentBackColor, true);
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
        this.BorderStyle = BorderStyle.None;
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams parameters = base.CreateParams;
            parameters.ExStyle |= WS_EX_TRANSPARENT;
            return parameters;
        }
    }
}

这篇关于WebBrowser Control 禁用鼠标点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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