繁体   English   中英

C# Windows 窗体应用程序透明按钮

[英]C# Windows Form Application Transparent button

我是 C# 的新手。 我想创建一个不可见的按钮,但它们在 C# windows 窗体应用程序中是可点击的。 有办法吗? 我尝试将 BackColor 设置为透明,但这并没有改变它是透明的事实

试试这个很简单。

单击要使其透明的按钮。 从 Properties 中选择FlatStyle并将其设置为popup现在将BackColor属性更改为Transparent

这将使按钮透明。

但是,如果您想让它在PictureBox上透明,则此方法将不起作用..

它仅适用于普通背景和背景图像。 希望它有效....

buttonLink.FlatStyle = FlatStyle.Flat; 
buttonLink.BackColor = Color.Transparent;
buttonLink.FlatAppearance.MouseDownBackColor = Color.Transparent;
buttonLink.FlatAppearance.MouseOverBackColor = Color.Transparent;

给出的答案只是使您想要使其透明的控件的背景颜色与其父控件的背景颜色相同。 这不是真正的透明度,Windows 窗体不支持真正的透明度。

Windows 窗体控件不支持真正的透明度。 透明 Windows 窗体控件的背景由其父级绘制。

https://docs.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-give-your-control-a-transparent-background?view=netframeworkdesktop-4.8&redirectedfrom=MSDN

参考:

原始文章和代码可以在以下位置找到:

当鼠标悬停在禁用的控件上时显示工具提示

@ CodeProject by tetsushmz

代码:

public class TransparentSheet : ContainerControl
{
    public TransparentSheet()
    {
        // Disable painting the background.
        this.SetStyle(ControlStyles.Opaque, true);
        this.UpdateStyles();

        // Make sure to set the AutoScaleMode property to None
        // so that the location and size property don't automatically change
        // when placed in a form that has different font than this.
        this.AutoScaleMode = AutoScaleMode.None;

        // Tab stop on a transparent sheet makes no sense.
        this.TabStop = false;
    }

    private const short WS_EX_TRANSPARENT = 0x20;

    protected override CreateParams CreateParams
    {
        [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
        get
        {
            CreateParams l_cp;
            l_cp = base.CreateParams;
            l_cp.ExStyle = (l_cp.ExStyle | WS_EX_TRANSPARENT);
            return l_cp;
        }
    }
}

解释:

您需要做的是使用给定的控件作为您禁用的TextBox (您在评论之一中提到的)的覆盖。 订阅覆盖控件的Click事件,您可以单击禁用的控件。

我强烈建议不要使用这种方法,并认为它是一种 hack。 你真的应该寻找一种替代方法,而不是必须使用一个禁用的控件和一个覆盖控件。

也许是一个不同的 UI 或者至少将它包装在一个UserControl中以隔离这个混乱的逻辑。

将按钮的背景属性设置为透明仍然会留下边框。 如果您想要一个完全透明的按钮,请执行以下两项操作之一:

创建一个透明面板并为 Click 事件分配一个方法

或者最好

创建一个仅填充 BackColor(设置为透明)的自定义 UserControl,并将方法分配给 Click 事件。

public class Invisible_Button : UserControl
{
    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);
        this.Cursor = Cursors.Hand;
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        e.Graphics.FillRectangle(new SolidBrush(this.BackColor), 0, 0, this.Width, this.Height);
    }
}

你试过button.Visible = false吗? 如果您只想隐藏它,这将完成这项工作。

暂无
暂无

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

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