我刚接触 C#,想创建一个不可见的按钮,但在 C# 窗体应用程序中它们仍然可以被点击。有没有办法?我尝试将 BackColor 设为 Transparent,但这并不能改变它是透明的这一事实。
我刚接触 C#,想创建一个不可见的按钮,但在 C# 窗体应用程序中它们仍然可以被点击。有没有办法?我尝试将 BackColor 设为 Transparent,但这并不能改变它是透明的这一事实。
很简单,试试这个。
点击想要使透明的按钮。
从属性中选择FlatStyle
并将其设置为popup
现在将BackColor
属性更改为Transparent
。
这将使按钮透明。
但是如果您想使它透明覆盖在PictureBox
上,此方法将不起作用。
它只适用于普通背景和背景图像。希望它好用....
buttonLink.FlatStyle = FlatStyle.Flat;
buttonLink.BackColor = Color.Transparent;
buttonLink.FlatAppearance.MouseDownBackColor = Color.Transparent;
buttonLink.FlatAppearance.MouseOverBackColor = Color.Transparent;
Windows Forms控件不支持真正的透明度。透明Windows Forms控件的背景由其父级绘制。
原始文章和代码可在以下链接找到:
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事件,这样您就可以在禁用的控件上单击了。
我强烈反对这种方法,感觉有点像黑客行为。您真的应该寻找替代方案,而不是必须在禁用的控件上使用覆盖控件。
也许可以考虑使用不同的UI,或者至少将其包装在一个UserControl中以隔离这个混乱的逻辑。
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
吗?如果你只是想隐藏它,这样做就可以了。