【问题标题】:How to change button background image on mouseOver?如何更改 mouseOver 上的按钮背景图像?
【发布时间】:2011-02-02 23:57:17
【问题描述】:

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

现在我不知道如何将 btn.backgroundImage 设置为 img2,我想在事件“MouseEnter”上进行设置。所以我会欣赏完整的代码,因为我对此很满意......

我很欣赏任何给定的想法...

【问题讨论】:

  • 对于 Web UI 还是 win-form UI?

标签: c# image button


【解决方案1】:

在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 就是你要找的。无论如何,在 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 文件

【讨论】:

  • @Justin 的答案应该被使用,因为MouseMove 会在鼠标移动时触发 - 包括当它仍然悬停在小部件上时,会导致奇怪的行为
【解决方案2】:

我认为是这样的:

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

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

【讨论】:

    【解决方案3】:

    我在 Visual Studio 2008 中为 .net 3.5 C# Windows 窗体应用程序创建了一个快速项目,并且能够创建以下代码。我找到了 enter 和 leave 方法的事件。

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

    【讨论】:

      【解决方案4】:

      您可以像这样为 MouseHover 和 MouseDown 创建一个基于具有特定图像的 Button 的类:

      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 属性。

      【讨论】:

        猜你喜欢
        • 2012-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-07
        • 2016-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多