[英]How can I use a .png file as a button in Windows Form ? ( in C# - visual studio 2013 )
I need to use a .png file as a button . 我需要使用.png文件作为按钮。 I just want to show a picture ( without any extra area around it ) to users .
我只想向用户展示一张图片(周围没有任何额外区域)。
My problem is in design and specify the area that should reacts clicking . 我的问题在于设计并指定应该点击的区域。 I changed some properties of my button , but I couldn't achieve to my purpose .
我改变了我的按钮的一些属性,但我达不到我的目的。 I changed the FlatStyle to Flat or the BackColor to Transparent , but the button has a colored background around itself yet .
我将FlatStyle更改为Flat或BackColor更改为Transparent,但按钮的背景周围有彩色背景。 I need to remove the background completely .
我需要完全删除背景。
I also tried the PictureBox , but I couldn't remove the background by properties again . 我也尝试了PictureBox,但我无法再通过属性删除背景。
Use a Button
instead of a PictureBox
. 使用
Button
而不是PictureBox
。 because it also gives the ability to use keyboard and tabbing, but the PictureBox
does not. 因为它还能够使用键盘和标签,但
PictureBox
却没有。
Add a Button
to your form and set these properties: 在表单中添加一个
Button
并设置以下属性:
Image = optionalPNGImage //should be 32bpp (alpha channel enabled)
BackColor = Color.Transparent;
FlatStyle = FlatStyle.Flat;
FlatAppearance.BorderSize = 0;
FlatAppearance.MouseDownBackColor = Color.Transparent;
FlatAppearance.MouseOverBackColor = Color.Transparent;
ForeColor = System.Drawing.Color.White;
Text = "Hello";
The result would be like this: 结果将是这样的:
Next, if you want to change the picture of the Button
on mouse over or mouse click, create these events: 接下来,如果要在鼠标悬停或鼠标单击时更改
Button
的图片,请创建以下事件:
//happens when your mouse enters the region of the button.
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.Image = picMouseOver;
}
//happens when your mouse leaves the region of the button.
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.Image = picRegular;
}
//happens when your mouse button is down inside the region of the button.
private void button1_MouseDown(object sender, MouseEventArgs e)
{
button1.Image = picMouseDown;
}
//happens when your mouse button goes up after it went down.
private void button1_MouseUp(object sender, MouseEventArgs e)
{
button1.Image = picRegular;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.