使用 CreateParams 添加 ExStyle = WS_EX_TRANSPARENT ; 此外, Control.SetStyle()方法用于修改控件行为,并添加这些控件样式: ControlStyles.Opaque 可防止绘制 BackGround 控件,因此它不受系统管理. ControlStyles.SupportsTransparentBackColor 允许控件接受Alpha值作为背景色. ControlStyles.ResizeRedraw 会导致控件在调整大小时重新绘制. 初始化自定义控件,将其必须引用的控件传递给覆盖.然后,它将自身调整为此引用控件的大小,从此度量中排除ScrollBar,以便可以使用它们.要使其正常工作,请创建对 OverlayPanel 类的引用,然后调用辅助方法 CreateOverlay(Control control): private OverlayPanel overlayPanel;私人void CreateOverlay(Control control){overlayPanel =新的OverlayPanel(this.webBrowser1);Controls.Add(overlayPanel);overlayPanel.BringToFront();} OverlayPanel 类代码可以插入到Form或自己的类文件中.当Form中的所有控件均已设置其尺寸时,应创建该代码. Form.Shown()事件或可见父窗体的任何其他时间.无论如何, Form.Load()事件在大多数情况下也可以正常工作. 请注意,此 OverlayPanel 目前没有 Resize 方法,如果在某个时候调整了覆盖控件的大小,则需要此方法.但这是一个非常简单的实现,如果需要的话. 私有类OverlayPanel:面板{内部int WS_EX_TRANSPARENT = 0x00000020;公共OverlayPanel(Control RefControl){InitializeComponent();this.Size = new Size(RefControl.Size.Width-SystemInformation.VerticalScrollBarWidth,RefControl.Size.Height-SystemInformation.HorizontalScrollBarHeight);this.Location = RefControl.Location;}私有void InitializeComponent(){this.SetStyle(ControlStyles.Opaque |ControlStyles.ResizeRedraw |ControlStyles.SupportsTransparentBackColor,true);this.SetStyle(ControlStyles.OptimizedDoubleBuffer,false);this.BorderStyle = BorderStyle.None;}受保护的覆盖CreateParams CreateParams{得到{CreateParams参数= base.CreateParams;parameters.ExStyle | = WS_EX_TRANSPARENT;返回参数;}}} I want to show a YouTube video inside a c# WinForm 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 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禁用鼠标单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-18 17:22