繁体   English   中英

如何在Windows窗体中将.png文件用作按钮? (在C#中 - visual studio 2013)

[英]How can I use a .png file as a button in Windows Form ? ( in C# - visual studio 2013 )

我需要使用.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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM