C# Windows 窗体应用程序透明按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10763640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Windows Form Application Transparent button
提问by Asmo
I'm new to C#. I'd like to create an invisible button, but they are click-able in C# windows form application. Is there a way? I tried BackColor to Transparent, but that does not change the fact that it is transparent
我是 C# 的新手。我想创建一个不可见的按钮,但它们在 C# windows 窗体应用程序中是可点击的。有办法吗?我尝试将 BackColor 设置为透明,但这并没有改变它是透明的事实
采纳答案by Pritam Routh
Its simple try this.
它的简单试试这个。
Click the button that you want to make transparent.
Select FlatStylefrom Properties and set it to popupNow change the BackColorproperty to Transparent.
单击要使其透明的按钮。FlatStyle从 Properties 中选择并将其设置为popup现在将BackColor属性更改为Transparent。
This will make the button transparent.
这将使按钮透明。
However if you want to make it transparent over a PictureBoxthis method wont work..
但是,如果您想使其透明,则PictureBox此方法将不起作用..
It works only on normal backgrounds and background images. Hope it works....
它仅适用于普通背景和背景图像。希望它有效......
回答by nunespascal
Did you try button.Visible = false?
If all you want is to hide it, this will do the job.
你试了button.Visible = false吗?如果您只想隐藏它,这将完成工作。
回答by decyclone
Reference:
参考:
Original article and code can be found at:
可以在以下位置找到原始文章和代码:
Displaying a ToolTip when the Mouse Hovers Over a Disabled Control
当鼠标悬停在禁用的控件上时显示工具提示
@ CodeProject by tetsushmz
@CodeProject由tetsushmz
Code:
代码:
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;
}
}
}
Explanation:
解释:
What you need to do is use the given control as an overlay on your disabled TextBox(that you mentioned in one of your comments). Sibscribe to the overlay control's Clickevent and you have yourself a click on a disabled control.
您需要做的是使用给定的控件作为您禁用的TextBox(您在您的评论中提到的)上的叠加层。Sibscribe 到覆盖控件的Click事件,您就可以点击一个禁用的控件。
I strongly recommend against this approach and feel it is kind of a hack. You really should look for an alternative approach instead of having to use a disabled control with an overlay control on top of it.
我强烈建议不要使用这种方法,并觉得它有点像黑客。您确实应该寻找一种替代方法,而不必使用禁用的控件和在其上叠加控件。
Maybe a different UI or atleast wrap it up in a UserControlto isolate this messy logic.
也许不同的 UI 或至少将它包装在UserControl 中以隔离这种混乱的逻辑。
回答by user2230410
btnLink.FlatStyle = FlatStyle.Flat;
btnLink.BackColor = Color.Transparent;
btnLink.FlatAppearance.MouseDownBackColor = Color.Transparent;
btnLink.FlatAppearance.MouseOverBackColor = Color.Transparent;
回答by Michael Kuczmarski
Setting the background property of the button to transparent will still leave a border. If you want a completely transparent button, do one of 2 things:
将按钮的背景属性设置为透明仍然会留下边框。如果您想要一个完全透明的按钮,请执行以下两项操作之一:
Create a transparent panel and assign a method to the Click event
创建一个透明面板并为 Click 事件分配一个方法
or preferably
或者最好
Create a custom UserControl that is filled with only BackColor (set to transparent) and assign method to Click event.
创建一个仅填充 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);
}
}

