我正在用C#编写Windows计算器。我想要当鼠标移至按钮上方时,它会更改按钮的颜色,而当鼠标移至“其他位置”时,按钮的颜色会恢复为原始颜色。
假设您使用的是Windows.Forms,则可以将事件处理程序添加到的MouseEnter
和MouseLeave
事件中,Button
并相应地设置Button
的BackColor
属性:
public partial class Form1 : Form { public Form1() { InitializeComponent(); button1.MouseEnter += OnMouseEnterButton1; button1.MouseLeave += OnMouseLeaveButton1; } private void OnMouseEnterButton1(object sender, EventArgs e) { button1.BackColor = SystemColors.ButtonHighlight; // or Color.Red or whatever you want } private void OnMouseLeaveButton1(object sender, EventArgs e) { button1.BackColor = SystemColors.ButtonFace; } }