C# 如何更改鼠标上方的按钮背景图像?
我的资源中有img1和img2。我很容易在btn属性中将btn.backgroundImage设置为img1。图像路径为:c:\Project\Resources 现在我不知道如何将btn.backgroundImage设置为img2,我想在“MouseEnter”事件上进行设置。所以我很欣赏完整的代码,因为我对这个非常熟悉C# 如何更改鼠标上方的按钮背景图像?,c#,image,button,C#,Image,Button,我的资源中有img1和img2。我很容易在btn属性中将btn.backgroundImage设置为img1。图像路径为:c:\Project\Resources 现在我不知道如何将btn.backgroundImage设置为img2,我想在“MouseEnter”事件上进行设置。所以我很欣赏完整的代码,因为我对这个非常熟悉 我很欣赏任何给定的想法…我想是这样的: btn.BackgroundImage = Properties.Resources.*Image_Identifier*; 其中
我很欣赏任何给定的想法…我想是这样的:
btn.BackgroundImage = Properties.Resources.*Image_Identifier*;
其中,
*Image\u Identifier*
是资源中图像的标识符。对于winforms:
如果您将图像包含到资源中,您可以这样做,非常简单和直接:
public Form1()
{
InitializeComponent();
button1.MouseEnter += new EventHandler(button1_MouseEnter);
button1.MouseLeave += new EventHandler(button1_MouseLeave);
}
void button1_MouseLeave(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img1));
}
void button1_MouseEnter(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img2));
}
我不建议对图像路径进行硬编码
因为你改变了你的问题
winforms afaik中没有(on)MouseOver,有MouseHover和MouseMove事件,但是如果您更改这些事件上的图像,它将不会更改回来,所以我认为MouseEnter+MouseLeave就是您要寻找的。无论如何,在悬停或移动时更改图像:
in the constructor:
button1.MouseHover += new EventHandler(button1_MouseHover);
button1.MouseMove += new MouseEventHandler(button1_MouseMove);
void button1_MouseMove(object sender, MouseEventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img2));
}
void button1_MouseHover(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img2));
}
向资源中添加图像:Projectproperties/resources/add/existing file我在visual studio 2008中为.net 3.5 C#windows窗体应用程序制作了一个快速项目,并能够创建以下代码。我找到了enter和leave方法的事件 在InitializeComponent()函数中。我使用VisualStudio设计器添加了事件处理程序
this.button1.MouseLeave += new System.EventHandler( this.button1_MouseLeave );
this.button1.MouseEnter += new System.EventHandler( this.button1_MouseEnter );
在按钮事件处理程序方法中,设置背景图像
/// <summary>
/// Handles the MouseEnter event of the button1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void button1_MouseEnter( object sender, EventArgs e )
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img2));
}
/// <summary>
/// Handles the MouseLeave event of the button1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void button1_MouseLeave( object sender, EventArgs e )
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img1));
}
//
///处理button1控件的MouseEnter事件。
///
///事件的来源。
///包含事件数据的实例。
私有无效按钮1\u鼠标指针(对象发送方,事件参数e)
{
this.button1.BackgroundImage=((System.Drawing.Image)(Properties.Resources.img2));
}
///
///处理button1控件的MouseLeave事件。
///
///事件的来源。
///包含事件数据的实例。
私有无效按钮1\u MouseLeave(对象发送方,事件参数e)
{
this.button1.BackgroundImage=((System.Drawing.Image)(Properties.Resources.img1));
}
您可以基于按钮创建一个类,其中包含鼠标悬停和鼠标向下的特定图像,如下所示:
btn.BackgroundImage = Properties.Resources.*Image_Identifier*;
公共类AdvancedImageButton:按钮
{
}
这个解决方案有一个小缺点,我相信可以解决:当您出于某种原因需要更改图像属性时,您还必须更改PlainImage属性 对于web UI或win-form UI?@Justin的回答应该使用,因为每当鼠标移动时,
MouseMove
都会触发,包括当它仍然悬停在小部件上时,会导致奇怪的行为