Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 . 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 . I need to remove the background completely .

I also tried the PictureBox , but I couldn't remove the background by properties again .

like image 930
Reyhaneh Sharifzadeh Avatar asked Jul 25 '14 23:07

Reyhaneh Sharifzadeh


People also ask

How do I add a button to Windows C form?

Windows. Forms namespace. Step 2: Drag the Button control from the ToolBox and drop it on the windows form. You are allowed to place a Button control anywhere on the windows form according to your need.

How do I add an image to a button in Visual Studio?

create an image. set the source of the image to the right file of your resources (e.g. a . png file that you included to your project) drag the image over the button. A text shows that asks you to press ALT to replace the text of the button with the image.


1 Answers

Use a Button instead of a PictureBox. because it also gives the ability to use keyboard and tabbing, but the PictureBox does not.

Add a Button to your form and set these properties:

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:

result

Next, if you want to change the picture of the Button on mouse over or mouse click, create these events:

//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;
}
like image 88
Matin Lotfaliee Avatar answered Sep 20 '22 01:09

Matin Lotfaliee