简体   繁体   English

在 Visual Studio Window Form 的面板上使用不透明度的任何技巧?

[英]Any trick to use opacity on a panel in Visual Studio Window Form?

I recently started exploring Visual Studio.我最近开始探索 Visual Studio。 I was trying to create a slide menu.我试图创建一个幻灯片菜单。 More specifically, when the user would press the button a submenu would pop up to the right.更具体地说,当用户按下按钮时,会在右侧弹出一个子菜单。 To achieve that i have placed a Panel to resize itself.为了实现这一点,我放置了一个Panel来调整自己的大小。 Apart from functionality i wanted to add a bit more design and make the Panel appear a bit faded.除了功能之外,我想添加更多设计并使Panel看起来有点褪色。

I know that Panels in Visual studio do not have opacity, but i was thinking if anyone knows a way-trick-idea about how it can be achieved.我知道 Visual Studio 中的Panels没有不透明度,但我在想是否有人知道如何实现它的方法技巧。 I tried a Picture Box but that too didn't have Opacity as a property.我尝试了一个Picture Box但它也没有不透明度作为属性。 I avoided to use the regular Menu object that visual studio offers because i wanted to add more design.我避免使用 Visual Studio 提供的常规Menu对象,因为我想添加更多设计。 Any ideas?有任何想法吗?

  1. Create a class that inherits from Panel .创建一个继承自Panel的类。
  2. Set the ControlStyle.Opaque for control in constructor using SetStyle .使用SetStyle为构造函数中的ControlStyle.Opaque设置ControlStyle.Opaque

If true, the control is drawn opaque and the background is not painted.如果为 true,则控件绘制为不透明且不绘制背景。

  1. Override CreateParams and set WS_EX_TRANSPARENT style for it.覆盖CreateParamsWS_EX_TRANSPARENT设置WS_EX_TRANSPARENT样式。

Specifies that a window created with this style is to be transparent.指定使用此样式创建的窗口是透明的。 That is, any windows that are beneath the window are not obscured by the window.也就是说,窗口下方的任何窗口都不会被窗口遮挡。 A window created with this style receives WM_PAINT messages only after all sibling windows beneath it have been updated.使用此样式创建的窗口仅在其下方的所有同级窗口都已更新后才会接收 WM_PAINT 消息。

  1. Create an Opacity property that accepts values from 0 to 100 that will be used as alpha channel of background.创建一个Opacity属性,该属性接受 0 到 100 之间的值,这些值将用作背景的 Alpha 通道。
  2. Override OnPaint and fill the background using an alpha enabled Brush that is created from BackGroundColor and Opacity .覆盖OnPaint并使用从BackGroundColorOpacity创建的启用 alpha 的Brush填充背景。

Complete 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截屏

在此处输入图片说明

To make a control "transparent", you should paint the right area of its parent onto the control.要使控件“透明”,您应该将其父级的正确区域绘制到控件上。 That's what the Button does before it draws its content so the rounded corners will be transparent.这就是Button在绘制其内容之前所做的事情,因此圆角将是透明的。

To mimic semi-transparency, you can paint the form onto the panel, and then draw something with Alpha:要模仿半透明,您可以将表单绘制到面板上,然后使用 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);
}

半透明面板

Please note that the ButtonRenderer.DrawParentBackground does not paint the controls of the form, which overlap with the panel, but only the background of the form.请注意, ButtonRenderer.DrawParentBackground不会绘制与面板重叠的表单控件,而只会绘制表单的背景。

Set any color at the panel.在面板上设置任何颜色。 For example, black, after which you just need to register the TransparencyKey shape and select the color you want to make transparent:例如黑色,之后你只需要注册 TransparencyKey 形状并选择你想要透明的颜色:

 public Form1()
        {
            InitializeComponent();
            panel1.BackColor = Color.Black; 
            this.TransparencyKey = Color.Black;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM