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

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

这很简单。

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

这将使按钮透明。

但是,如果你想让它在PictureBox透明,这个方法就不行了。

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

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

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

参考:

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

鼠标hover在禁用控件上时显示工具提示

由tetsushmz提供的 @ CodeProject

码:

 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事件同步,您可以单击禁用的控件。

我强烈建议不要采用这种方法,并认为这是一种黑客行为。 你真的应该寻找一种替代方法,而不是必须使用带有覆盖控制的禁用控件。

也许是一个不同的用户界面或至少在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); } }