简体   繁体   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 )

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.

相关问题 如何使用C#中另一个命名空间的对象/类(Visual Studio 2013) - How can I use objects/classes from another namespace in C# (visual studio 2013) 在Visual Studio 2013 C#中旋转按钮 - Rotate a button in visual studio 2013 c# 如何使用 C# 在 Visual Studio 中创建切换按钮? - How can I create a toggle button in Visual Studio with C#? 如何共享在Visual Studio 2013(C#)中创建的.exe文件? - How to share .exe file created in Visual Studio 2013 (C#)? 初始化 C# Visual Studio Windows 窗体应用程序后,如何更改窗体的视觉效果? - How do I change the visual of a form after it has Initialized C# Visual Studio Windows Form App? 如何才能逐渐且平滑地更改PictureBox的大小? (在Winforms中,C#,Visual Studio 2013) - How can I change PictureBox size incrementally and smoothly ? ( in winforms , c# , visual studio 2013 ) 如何在Visual Studio 2013中编辑ASP.Net MVC C#项目模板文件? - How can I edit ASP.Net MVC C# project template files in Visual Studio 2013? 如何使用C#和XAML使标题栏看起来像在Visual Studio 2013中显示的一样 - How can I make the titlebar look like what shows in Visual Studio 2013 Using C# & XAML 如何从Visual Studio 2013 C#项目的安装程序中获取变量? - How can I obtain a variable from the installer of a visual studio 2013 c# project? 如何从Visual Studio 2013中的C#解决方案生成序列图? - How can I generate Sequence Diagram from a C# solution in Visual Studio 2013?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM