2010-03-31 111 views
3

我有IMG1和IMG2在我的資源。我很容易在btn屬性中將btn.backgroundImage設置爲img1。圖像路徑是:c:\ Project \ Resources ...如何更改mouseOver上的按鈕背景圖像?

現在我不知道如何設置btn.backgroundImage是img2,我想在事件「MouseEnter」上做到這一點。所以,我會apreciate完整的代碼,因爲我很綠色這個...

我apreciate任何給定的想法...

+0

對於Web UI還是一個雙贏的形式UI? – Sunny 2010-03-31 15:30:32

+0

爲雙贏形式UI – Slavisa 2010-03-31 15:40:50

回答

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(上)鼠標懸停AFAIK,有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 /資源/添加/現有文件

+0

@賈斯汀的答案應該因爲'MouseMove'會觸發使用,只要鼠標移動 - 包括當它仍徘徊在小部件,從而導致行爲怪異 – ecoe 2014-11-10 16:29:04

3

我覺得是這樣的:

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

*Image_Identifier*是資源中圖像的標識符。

2

我做在Visual Studio 2008的快速項目的.NET 3.5 C#窗體窗體應用程序,並能夠創建下面的代碼。我發現了進入和離開方法的事件。

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

你將它設置爲相同的圖像在鼠標離開.. – 2010-03-31 18:04:01

+0

好的,應該是img1,代碼已經更新了! – Justin 2010-04-07 16:37:22

0

您可以根據與MouseHover和的MouseDown特定圖像這樣的按鈕,創建一個類:

公共類AdvancedImageButton:按鈕 {

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屬性也是。