2011-01-30 17 views
0

我有一個按鈕的圖像。我用圖像的平面按鈕。默認情況下,當您在按鈕上單擊或按住鼠標時,標準按鈕中的窗口按鈕的背景圖像會更改。但我想在鼠標保持事件中更改背景圖像。在C中的鼠標事件中更改windows按鈕的背景#

我使用的Visual Studio 2008

回答

3

使用MouseDownMouseUp事件來改變背景來回:

private void btn_MouseDown(object sender, MouseEventArgs e) 
{ 
    //Replace with the appropriate control/image/color change: 
    btn.BackColor = Color.Black; 
} 

private void btn_MouseUp(object sender, MouseEventArgs e) 
{ 
    //As mentioned above 
    btn.BackColor = SystemColors.Control; 
    //Show the MsgBox here 
    MessageBox.Show("The background is fine!"); 
} 
+0

什麼是語法:btn.BackgroundImage = ?????是它btn.BackgroundImage =「C:\\ image.png」還是什麼? – saaZ 2011-01-30 14:08:39

+0

好吧,我知道了btn.BackgroundImage = new Bitmap(source);但是,當我在按鈕單擊時顯示消息框時,它會產生問題。該按鈕不會恢復正常。它只在關閉從按鈕單擊事件派生的消息框時纔會執行 – saaZ 2011-01-30 14:33:24

-1

我想這將是非常複雜的,如果你使用的WinForms做 - 當你將它標記爲winforms時,你可能會這樣做。我認爲在winforms中,你必須定義自己的控制才能達到這個目標,這會花費你很多時間和精力。另外,你可以使用mousedown和mouseup事件,但是如果你想改變別的東西,那麼這種方法不是很靈活。然而,如果你不需要使用winforms,但你也可以使用WPF,那裏有幾種可能性,因爲WPF的設計正是爲此而設計的。您可以定義自己的樣式和模板來更改控件的外觀。要更改鼠標保持事件的顏色,可以使用觸發器。有關模板的詳細信息,請參閱以下頁面:click

希望有所幫助。

0

我是葡萄牙人,對不起,因爲「我的英語」。

用2張圖像創建ImagemList。

Private Sub Button1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave 



    'out the button 

End Sub 



Private Sub Button1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove 



    'In the button 

    Button1.BackgroundImage = ImageList2.Images(1) 
End Sub