C# 设计当得到鼠标焦点后自动放大的button按钮

来源:互联网 发布:滨州行知中学 听风 编辑:程序博客网 时间:2024/06/16 12:09

设计原理:当得到鼠标的焦点后,按钮的长和宽会增加,当鼠标经过后,将按钮的长和宽修改过来。

还要用到button按钮的Location属性。

步骤如下:

1.在窗体的Designer.cs文件中写入以下两行程序(button1即要操作的按钮):

 this.button1.MouseEnter += new System.EventHandler(button1_MouseEnter); this.button1.MouseLeave += new System.EventHandler(button1_MouseLeave);

2.在代码窗口写入以下两个函数:

 private void button1_MouseEnter(object sender,EventArgs e)        {            button1.Width += 6;//当得到焦点时宽度增加值            button1.Height += 6;            Point pot = new Point( button1.Location.X,button1.Location.Y);            button1.Location = new Point(pot.X-3,pot.Y-3);//设计按钮的坐标中心不变        }        private void button1_MouseLeave(object sender, EventArgs e)        {            button1.Width -= 6;//失去焦点后宽度值还原            button1.Height -= 6;            Point pot = new Point(button1.Location.X, button1.Location.Y);            button1.Location = new Point(pot.X +3, pot.Y +3);//设计按钮坐标中心不变        }


ok,运行正常!

原创粉丝点击