当鼠标光标悬停在按钮上时 BackgroundImage 移动
BackgroundImage shifts when mouse cursor is hovering over the button
对不起我的英语。这是我第一次用这种语言写问题。如果您能纠正我的语法错误,我会很高兴。
我创建了一个 changing_button
并设置了它的 BackgroundImage 属性。当光标位于此按钮上时,我希望此图像发生变化。我为此拍了两张照片。
blue_image.png: enter image description here
red_image.png: enter image description here
正常情况下changing_button
应该显示red_image。但是如果光标在这个按钮上,它应该显示 blue_image。问题是当我尝试覆盖 BackgroundImage 属性 时 blue_image 发生了变化:enter image description here
我该如何解决这个问题?
using System;
using System.Drawing;
using System.Windows.Forms;
namespace winforms_test_1
{
public partial class Form1 : Form
{
Button changing_button;
Image blue_image = Image.FromFile("D://images//blue_image.png");
Image red_image = Image.FromFile("D://images//red_image.png");
public Form1()
{
InitializeComponent();
TableLayoutPanel main_panel = new TableLayoutPanel
{
BackColor = Color.White,
Dock = DockStyle.Fill
};
changing_button = new Button
{
BackgroundImage = red_image,
BackgroundImageLayout = ImageLayout.Center,
FlatStyle = FlatStyle.Flat,
Margin = new Padding(0, 0, 0, 0),
Size = new Size(50, 50),
};
changing_button.FlatAppearance.BorderSize = 0;
changing_button.MouseEnter += new System.EventHandler(this.test_button_MouseEnter);
changing_button.MouseLeave += new System.EventHandler(this.test_button_MouseLeave);
main_panel.Controls.Add(changing_button, 0, 0);
Controls.Add(main_panel);
}
void test_button_MouseEnter(object sender, EventArgs e)
{
changing_button.Image = blue_image;
}
void test_button_MouseLeave(object sender, EventArgs e)
{
changing_button.Image = red_image;
}
}
}
不要更改背景图像,而是将初始图像设置为红色图像。 BackgroundImage 和 Image 看起来它们的位置略有不同。
changing_button = new Button
{
Image = red_image,
ImageLayout = ImageLayout.Center,
FlatStyle = FlatStyle.Flat,
Margin = new Padding(0, 0, 0, 0),
Size = new Size(50, 50),
};
使用 BackgroundImage 或 Image,但不能同时使用两者。
对不起我的英语。这是我第一次用这种语言写问题。如果您能纠正我的语法错误,我会很高兴。
我创建了一个 changing_button
并设置了它的 BackgroundImage 属性。当光标位于此按钮上时,我希望此图像发生变化。我为此拍了两张照片。
blue_image.png: enter image description here
red_image.png: enter image description here
正常情况下changing_button
应该显示red_image。但是如果光标在这个按钮上,它应该显示 blue_image。问题是当我尝试覆盖 BackgroundImage 属性 时 blue_image 发生了变化:enter image description here
我该如何解决这个问题?
using System;
using System.Drawing;
using System.Windows.Forms;
namespace winforms_test_1
{
public partial class Form1 : Form
{
Button changing_button;
Image blue_image = Image.FromFile("D://images//blue_image.png");
Image red_image = Image.FromFile("D://images//red_image.png");
public Form1()
{
InitializeComponent();
TableLayoutPanel main_panel = new TableLayoutPanel
{
BackColor = Color.White,
Dock = DockStyle.Fill
};
changing_button = new Button
{
BackgroundImage = red_image,
BackgroundImageLayout = ImageLayout.Center,
FlatStyle = FlatStyle.Flat,
Margin = new Padding(0, 0, 0, 0),
Size = new Size(50, 50),
};
changing_button.FlatAppearance.BorderSize = 0;
changing_button.MouseEnter += new System.EventHandler(this.test_button_MouseEnter);
changing_button.MouseLeave += new System.EventHandler(this.test_button_MouseLeave);
main_panel.Controls.Add(changing_button, 0, 0);
Controls.Add(main_panel);
}
void test_button_MouseEnter(object sender, EventArgs e)
{
changing_button.Image = blue_image;
}
void test_button_MouseLeave(object sender, EventArgs e)
{
changing_button.Image = red_image;
}
}
}
不要更改背景图像,而是将初始图像设置为红色图像。 BackgroundImage 和 Image 看起来它们的位置略有不同。
changing_button = new Button
{
Image = red_image,
ImageLayout = ImageLayout.Center,
FlatStyle = FlatStyle.Flat,
Margin = new Padding(0, 0, 0, 0),
Size = new Size(50, 50),
};
使用 BackgroundImage 或 Image,但不能同时使用两者。