How to change button background image on mouseOver?

I have img1 and img2 in my resources. I easily set btn.backgroundImage as img1 in the btn properties. Image Paths: c: \ Project \ Resources ...

Now I do not know how to set btn.backgroundImage as img2, I want to do this on the "MouseEnter" event. So I would appreciate the full code, because I'm pretty green about it ...

I appreciate any idea ...

+3
source share
4 answers

In case of winforms:

If you include images in your resources, you can do it this way, very simple and straightforward:

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

I would not recommend hard coding coding paths.

How did you change your question ...

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 file

+17

, - :

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

*Image_Identifier* - .

+3

visual studio 2008 .net 3.5 # windows form . , .

InitializeComponent(). Visual Studio.

this.button1.MouseLeave += new System.EventHandler( this.button1_MouseLeave );
this.button1.MouseEnter += new System.EventHandler( this.button1_MouseEnter );

Background events are set in button event handler methods.

/// <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));
}
+2
source

You can create a button-based class with special images for MouseHover and MouseDown as follows:

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

}

This solution has a small flaw, which I am sure can be fixed: when for some reason you need to change the Image property, you will also have to change the PlainImage property.

0
source

Source: https://habr.com/ru/post/1739330/