Rey*_*deh 5 c# png button winforms visual-studio-2013
我需要使用.png文件作为按钮.我只想向用户展示一张图片(周围没有任何额外区域).
我的问题在于设计并指定应该点击的区域.我改变了我的按钮的一些属性,但我达不到我的目的.我将FlatStyle更改为Flat或BackColor更改为Transparent,但按钮的背景周围有彩色背景.我需要完全删除背景.
我也尝试了PictureBox,但我无法再通过属性删除背景.
使用Button而不是a PictureBox.因为它也提供了使用键盘和标签的能力,但PictureBox事实并非如此.
将a添加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";
Run Code Online (Sandbox Code Playgroud)
结果将是这样的:

接下来,如果要更改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;
}
Run Code Online (Sandbox Code Playgroud)