Reputation: 795
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 .
Upvotes: 6
Views: 17339
Reputation: 1615
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:
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;
}
Upvotes: 9