如何在mouseOver上更改按钮背景图像?

时间:2010-03-31 15:29:17

标签: c# image button

我的资源中有img1和img2。我很容易在btn属性中将btn.backgroundImage设置为img1。图像路径是:c:\ Project \ Resources ...

现在我不知道如何将btn.backgroundImage设置为img2,我想在事件“MouseEnter”上执行此操作。所以我会提供完整的代码,因为我对此非常了解......

我赞成任何给定的想法......

4 个答案:

答案 0 :(得分:17)

在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 afai中没有(on)MouseOver,有MouseHover和MouseMove事件,但如果你改变那些图像,它将不会改回,所以MouseEnter + MouseLeave是你想要的。无论如何,在Hover或Move上更改图像:

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

答案 1 :(得分:3)

我认为是这样的:

btn.BackgroundImage = Properties.Resources.*Image_Identifier*;

其中*Image_Identifier*是资源中图片的标识符。

答案 2 :(得分:2)

我在visual studio 2008上为.net 3.5 C#windows表单应用程序做了一个快速项目,并且能够创建以下代码。我找到了进入和离开方法的事件。

在InitializeComponent()函数中。我使用Visual Studio设计器添加了事件处理程序。

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));
}

答案 3 :(得分:0)

您可以创建一个基于Button的类,其中包含MouseHover和MouseDown的特定图像,如下所示:

public class AdvancedImageButton:Button   {

public Image HoverImage { get; set; }
public Image PlainImage { get; set; }
public Image PressedImage { get; set; }

protected override void OnMouseEnter(System.EventArgs e)
{
  base.OnMouseEnter(e);
  if (HoverImage == null) return;
  if (PlainImage == null) PlainImage = base.Image;
  base.Image = HoverImage;
}

protected override void OnMouseLeave(System.EventArgs e)
{
  base.OnMouseLeave(e);
  if (HoverImage == null) return;
  base.Image = PlainImage;
}

protected override void OnMouseDown(MouseEventArgs e)
{
  base.OnMouseDown(e);
  if (PressedImage == null) return;
  if (PlainImage == null) PlainImage = base.Image;
  base.Image = PressedImage;
}

}

这个解决方案有一个小缺点,我确信可以修复:当你需要更改Image属性时,你还必须更改PlainImage属性。