public partial class Form1 : Form { public Form1() { InitializeComponent(); this.button1.Enter += button_Enter; this.button1.Leave += button_Leave; this.button2.Enter += button_Enter; this.button2.Leave += button_Leave; //继续写其他的 } void button_Leave(object sender, EventArgs e) { (sender as Button).BackColor = SystemColors.Control; } void button_Enter(object sender, EventArgs e) { (sender as Button).BackColor = Color.Red; } }
//给每个按钮全部订阅一样的点击事件 button1.Click += button_Click; button2.Click += button_Click; button3.Click += button_Click; private void button_Click(object sender, EventArgs e) { Button[] btnArr = { button1, button2,button3 }; Button btn = sender as Button;//当前按钮 btn.BackgroundImage = Resource1.backImg1;//将当前按钮背景设为backImg1,Resource1是资源文件类名 //剩下的除当前按钮外,全部设另一个背景 Array.ForEach(btnArr.Where(p => p != btn).ToArray(), p => p.BackgroundImage = Resource1.backImg2); }
时间: 2024-10-16 09:54:25