visual-studio 我可以使用 C# 在 Visual Studio 2005 中创建透明按钮吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/201778/
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
Can I create transparent buttons in Visual Studio 2005 with C#
提问by Sean
I have a user interface that requires placing some round buttons in a C# project with some data behind them. The buttons are System.Windows.Forms.buttons and I have used a GIF image with transparency to create them. However, the transparent areas aren't transparent. I've looked for references online but haven't found any suggestions for how to do this properly. There's some mention of doing it in Visual Studio 2008 but I need to keep this project in 2005. Any help or suggestion is appreciated.
我有一个用户界面,需要在 C# 项目中放置一些圆形按钮,并在它们后面放置一些数据。这些按钮是 System.Windows.Forms.buttons,我使用了一个透明的 GIF 图像来创建它们。但是,透明区域并不透明。我在网上寻找参考资料,但没有找到任何关于如何正确执行此操作的建议。有人提到在 Visual Studio 2008 中执行此操作,但我需要在 2005 年保留此项目。感谢任何帮助或建议。
回答by ine
I'm pretty sure you need to use PNGs with WinForms to get image transparency. I know I've used them successfully.
我很确定您需要在 WinForms 中使用 PNG 来获得图像透明度。我知道我已经成功地使用了它们。
EDIT: When I used the PNGs, I was overlaying them with the Image control onto the Form1.BackgroundImage; I wasn't using them in buttons.
编辑:当我使用 PNG 时,我将它们与 Image 控件叠加到 Form1.BackgroundImage 上;我没有在按钮中使用它们。
I think your best bet is to switch from using a button control to using an image control. You might also try changing the button style to flat (I think it was flat, maybe one of the other styles) and seeing if you can get the effect you want that way.
我认为最好的办法是从使用按钮控制切换到使用图像控制。您也可以尝试将按钮样式更改为扁平(我认为它是扁平的,也许是其他样式之一),然后看看是否可以通过这种方式获得想要的效果。
回答by ine
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
public class TransparentControl : Control
{
public TransparentControl()
{
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.Opaque, true);
SetStyle(ControlStyles.ResizeRedraw, true);
this.BackColor = Color.Transparent;
}
protected override void OnPaint(PaintEventArgs pevent)
{
Graphics g = pevent.Graphics;
g.DrawRectangle(Pens.Black, this.ClientRectangle);
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
// don't call the base class
//base.OnPaintBackground(pevent);
}
protected override CreateParams CreateParams
{
get
{
const int WS_EX_TRANSPARENT = 0x20;
CreateParams cp = base.CreateParams;
cp.ExStyle |= WS_EX_TRANSPARENT;
return cp;
}
}
// rest of class here...
}
回答by Bert
Try this:
试试这个:
Bitmap temp = new Bitmap(button1.Image);
temp.MakeTransparent(Color.Black); //your transparent color, in this case black
button1.Image = (Image) Logo;
temp.Dispose();
temp = null;
回答by Wade Price
Just set the Button's FlatStyle propertyto "Flat"…and voilà! I know this works with PNGs, but I haven't tested it with GIFs.
只需将 Button 的FlatStyle 属性设置为“Flat”……瞧!我知道这适用于 PNG,但我还没有用 GIF 对其进行测试。
回答by Razib Ali
The following answer is verified in Visual Studio 2012 and works with backgroundImage format BMP, GIF, PNG as well as JPG; created by MS Paint.
以下答案在 Visual Studio 2012 中得到验证,并且适用于背景图像格式 BMP、GIF、PNG 和 JPG;由 MS Paint 创建。
To make your background visible through your button:
要通过按钮使您的背景可见:
Go to the properties window of the button. Then change the FlatStyle and BackColor as shown in the image below:
转到按钮的属性窗口。然后更改 FlatStyle 和 BackColor,如下图所示:




Note: The color is to be chosen from Web Tab and The FlatStyle is to be chosen as Flat.
注意:颜色要从Web Tab 中选择,FlatStyle 选择为Flat。
But after this, the button will be transparent till mouse do not hover or it is selected or pressed. On that condition, it will be in some non-transparent color. If you want to make it always transparent, follow the following images and change the corresponding colors to transparent as before. The colors to be changed are: 'MouseOverBackColor' & 'MouseDownBackColor'.
但在此之后,按钮将是透明的,直到鼠标不悬停或被选中或按下。在这种情况下,它将是一些不透明的颜色。如果你想让它始终透明,请按照以下图像将相应的颜色更改为透明。要更改的颜色是:'MouseOverBackColor' & 'MouseDownBackColor'。




Note: If you choose always transparent, as shown, there will be no change in the appearance of the button while it will be in action!
注意:如果您选择始终透明,如图所示,按钮在活动时的外观不会发生变化!
Hope it will help you.
希望它会帮助你。
Best of luck!
祝你好运!
回答by Razib Ali
You need to set BackColor property of a button to "Transparent".
您需要将按钮的 BackColor 属性设置为“透明”。
Button1.BackColor = System.Drawing.Color.Transparent;
Button1.BackColor = System.Drawing.Color.Transparent;
回答by Razib Ali
create a class derived from button and put this code.
创建一个派生自按钮的类并放置此代码。
protected override CreateParams CreateParams
{
get
{
const int WS_EX_TRANSPARENT = 0x20;
CreateParams cp = base.CreateParams;
cp.ExStyle |= WS_EX_TRANSPARENT;
return cp;
}
}
and in create method
并在创建方法中
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.Opaque, true);
this.BackColor = Color.Transparent;
回答by Razib Ali
I am using Visual Studio 2005 and tried following your example above but it did not work (the snippet in create method). I have a background for my form and am trying to create bevelled transpared buttons.
我正在使用 Visual Studio 2005 并尝试按照上面的示例进行操作,但它不起作用(create 方法中的代码段)。我的表单有一个背景,并且正在尝试创建斜面的透明按钮。
currently inserting the above code makes my background transparent and my buttons all white in color.
当前插入上面的代码使我的背景透明,我的按钮都是白色的。
here is a snippet of my code
这是我的代码片段
Bitmap mainFormBackground = (Bitmap)Image.FromFile("background.jpg");
this.BackgroundImage = mainFormBackground;
this.BackColor = Color.Black;
this.ForeColor = Color.White;
Add some buttons
添加一些按钮
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.Opaque, true);
this.BackColor = Color.Transparent;
I even tried
我什至试过
btnName.BackColor = Color.Transpare;
this too did not work
这也不起作用

