Webbrowser禁用鼠标单击 [英] Webbrowser disable mouse clicks

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

问题描述

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

我正在捕获所有控件的预览,鼠标和键盘事件,此外,我在加载的HTML文档中放置了一些处理程序,但未成功:

  void webBrowser1_DocumentCompleted(对象发送者,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 + =新的HtmlElementEventHandler(Document_MouseDown);webBrowser1.Document.Body.MouseUp + =新的HtmlElementEventHandler(Document_MouseMove);webBrowser1.Document.Body.MouseUp + =新的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);}} 

我也可以在Web浏览器控件前面"使用透明控件.

还有其他想法吗?

解决方案

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

使用 Control.SetStyle()方法用于修改控件行为,并添加这些

Also I woulb be fine to but a transparent Control "in front of" the web browser control.

Has anyone another idea?

解决方案

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禁用鼠标单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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