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 catc
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.
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;
}
}
}