我需要将.png文件用作按钮。我只想向用户展示图片(周围没有多余的区域)。
我的问题是在设计中并指定应单击的区域。我更改了按钮的一些属性,但无法实现目的。我将FlatStyle更改为Flat或将BackColor更改为Transparent,但该按钮周围仍带有彩色背景。我需要完全删除背景。
我也尝试了PictureBox,但是我不能通过属性再次删除背景。
使用Button
而不是PictureBox
。因为它还提供了使用键盘和制表符的功能,但PictureBox
事实并非如此。
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";
结果将是这样的:
接下来,如果要更改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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句