Any trick for using opacity on a panel in the form of a Visual Studio window?

I recently started to learn Visual Studio. I tried to create a slide menu. More specifically, when a user clicks a button, a submenu will appear on the right. To achieve this, I placed Panel to resize. In addition to functionality, I wanted to add a little more design and make Panel bit faded.

I know that Panels in Visual Studio does not have opacity, but I was wondering if anyone knew the idea of ​​a trick how to achieve it. I tried Picture Box , but this one also did not have opacity as a property. I avoided using the regular Menu object that visual studio offers because I wanted to add more design. Any ideas?

+8
source share
2 answers
  1. Create a class that inherits from Panel .
  2. Set ControlStyle.Opaque for the control in the constructor using SetStyle .

If true, the control appears opaque and the background is not colored.

  1. Override CreateParams and set the WS_EX_TRANSPARENT style for it.

Indicates that the window created with this style should be transparent. That is, any windows that are under the window are not closed. A window created in this style receives WM_PAINT messages only after updating all child windows below it.

  1. Create an Opacity property that takes values ​​from 0 to 100, which will be used as the alpha channel of the background.
  2. Override OnPaint and fill the background with the Brush alpha inclusion created from BackGroundColor and Opacity .

Full code

 public class ExtendedPanel : Panel { private const int WS_EX_TRANSPARENT = 0x20; public ExtendedPanel() { SetStyle(ControlStyles.Opaque, true); } private int opacity = 50; [DefaultValue(50)] public int Opacity { get { return this.opacity; } set { if (value < 0 || value > 100) throw new ArgumentException("value must be between 0 and 100"); this.opacity = value; } } protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle = cp.ExStyle | WS_EX_TRANSPARENT; return cp; } } protected override void OnPaint(PaintEventArgs e) { using (var brush = new SolidBrush(Color.FromArgb(this.opacity * 255 / 100, this.BackColor))) { e.Graphics.FillRectangle(brush, this.ClientRectangle); } base.OnPaint(e); } } 

Screenshot

enter image description here

+20
source

To make the control “transparent”, you must direct the correct area of ​​your parent to the control. What Button does before it draws its contents so that the rounded corners are transparent.

To mimic the translucency, you can draw a shape on the panel, and then draw something using Alpha:

 private void panel1_Paint(object sender, PaintEventArgs e) { PaintTransparentBackground(panel1, e); using (Brush b = new SolidBrush(Color.FromArgb(128, panel1.BackColor))) { e.Graphics.FillRectangle(b, e.ClipRectangle); } } private static void PaintTransparentBackground(Control c, PaintEventArgs e) { if (c.Parent == null || !Application.RenderWithVisualStyles) return; ButtonRenderer.DrawParentBackground(e.Graphics, c.ClientRectangle, c); } 

Translucent panel

Note that ButtonRenderer.DrawParentBackground does not draw form controls that overlap with the panel, but only the background of the form.

+2
source

All Articles