作用

程序设计过程中,我们经常需要增加一些动态效果,以此改善用户的使用体验。本文将介绍一种方法,动态显示按钮状态,使其得到鼠标焦点后自动放大,失去鼠标焦点后自动缩小。


 

效果图

先放一张原图(鼠标还未移动到按钮上):

 

获得鼠标焦点的Button按钮:(这里因为是图片,放大不明显,所以笔者将按钮字体也一起改变,以做“放大前和放大后”区分)


 

程序主要代码:

 

private void button11_MouseEnter(object sender, EventArgs e)//鼠标移动到按钮上发生的事件
        {
            button11.Font = new Font("隶书", 9);                                                                        //设置按钮字体样式
            button11.Width = 77;                                                                                         //设置按钮宽度
            button11.Height = 25;                                                                                          //设置按钮高度
            button11.Location = new Point(button11.Location.X-2,button11.Location.Y-2);
//控件放大后,位置也要稍微变换一下,不然效果会很违和
        }

        private void button11_MouseLeave(object sender, EventArgs e)//鼠标从按钮上移走时发生的事件
        {
            button11.Font = new Font("宋体", 9);                                                                        //设置按钮字体样式
            button11.Width = 75;                                                                                         //设置按钮宽度
            button11.Height = 23;                                                                                          //设置按钮高度
            button11.Location = new Point(button11.Location.X + 2, button11.Location.Y + 2);
//控件恢复原样后,位置也要恢复原样,不然效果会很违和
        }

 

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/qianjindelaowu/p/17553763.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!