在 Visual Studio Window Form 的面板上使用不透明度的任何技巧? [英] Any trick to use opacity on a panel in Visual Studio Window Form?

查看:30
本文介绍了在 Visual Studio Window Form 的面板上使用不透明度的任何技巧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始探索 Visual Studio.我试图创建一个幻灯片菜单.更具体地说,当用户按下按钮时,会在右侧弹出一个子菜单.为了实现这一点,我放置了一个 Panel 来调整自身大小.除了功能之外,我想添加更多设计并使 Panel 看起来有点褪色.

我知道 Visual Studio 中的 Panels 没有不透明度,但我在想是否有人知道如何实现它的方法技巧.我尝试了 Picture Box 但它也没有 Opacity 作为属性.我避免使用 Visual Studio 提供的常规 Menu 对象,因为我想添加更多设计.有什么想法吗?

解决方案

  1. 创建一个继承自

    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. To achieve that i have placed a Panel to resize itself. Apart from functionality i wanted to add a bit more design and make the Panel appear a bit faded.

    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. I tried a Picture Box but that too didn't have Opacity as a property. I avoided to use the regular Menuobject that visual studio offers because i wanted to add more design. Any ideas?

    解决方案

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

    If true, the control is drawn opaque and the background is not painted.

    1. Override CreateParams and set WS_EX_TRANSPARENT style for it.

    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.

    1. Create an Opacity property that accepts values from 0 to 100 that will be used as alpha channel of background.
    2. Override OnPaint and fill the background using an alpha enabled Brush that is created from BackGroundColor and Opacity.

    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

    这篇关于在 Visual Studio Window Form 的面板上使用不透明度的任何技巧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆