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

前端 未结 3 925
悲&欢浪女
悲&欢浪女 2020-11-29 06:47

I recently started exploring 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.

3条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 07:41

    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.

    To mimic semi-transparency, you can paint the form onto the panel, and then draw something with 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.

提交回复
热议问题