如何为所有按钮设置鼠标悬停?

时间:2017-05-11 19:08:25

标签: c# forms button

我的表单上有很多按钮。而且我想让它变得更好一点,所以按钮将颜色和字体改变为粗体,当鼠标悬停在它上面似乎是个好主意。我将不胜感激任何帮助            button.BackColor = Color.Cyan; button.Font = new Font(button.Font.Name, button.Font.Size, FontStyle.Bold);

编辑: 这对我有用:

private void button1_MouseEnter(object sender, EventArgs e)
    {
        ((Button)sender).BackColor = Color.PaleTurquoise;
        ((Button)sender).Font = new Font(((Button)sender).Font.Name, ((Button)sender).Font.Size, FontStyle.Bold);
    }

    private void button1_MouseLeave(object sender, EventArgs e)
    {
        ((Button)sender).BackColor = Color.WhiteSmoke;
        ((Button)sender).Font = new Font(((Button)sender).Font.Name, ((Button)sender).Font.Size, FontStyle.Regular);
    }

(将button1_mousenter(或mouseleave)设置为每个按钮的操作

1 个答案:

答案 0 :(得分:0)

您可以将MouseEnter和MouseLeave事件添加到更改按钮颜色的按钮。

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseenter(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseleave(v=vs.110).aspx

// bind handler to MouseEnter Event
this.yourButton1.MouseEnter += new System.EventHandler(this.allButtons_MouseEnter);

this.yourButton2.MouseEnter += new System.EventHandler(this.allButtons_MouseEnter);

// bind handler to MouseLeave Event
this.yourButton1.MouseLeave += new System.EventHandler(this.allButtons_MouseLeave);

this.yourButton2.MouseLeave += new System.EventHandler(this.allButtons_MouseLeave);

// enter handler
private void allButtons_MouseEnter(object sender, System.EventArgs e) 
{
    Button btn = (Button)sender;
    btn.BackColor = Color.Cyan;
    btn.Font = new Font(btn.Font.Name, btn.Font.Size, FontStyle.Bold);
}

// leave handler
private void allButtons_MouseLeave(object sender, System.EventArgs e) 
{
    Button btn = (Button)sender;
    btn.BackColor = Color.DeepPink; // whatever your original color was
    btn.Font = new Font(btn.Font.Name, btn.Font.Size, FontStyle.Regular);
}