Button的BringToFront()方法,使按钮置顶.

今天看到QQ上有一个当鼠标划过按钮时使选中的按钮放大并置顶,自己写个代码试验下,代码如下:

  public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        int x = 0;//保存鼠标进入的按钮的原始位置X坐标
        int y = 0;//保存鼠标进入的按钮的原始位置Y坐标

        System.Drawing.Size size = new Size();//保存鼠标进入的按钮的原始大小

        private void Btn_MouseEnter(object sender, EventArgs e)
        {
            if (!(sender is Button))
                return;

           Button btn = sender as Button;
           btn.BringToFront();

           x = btn.Location.X;
           y = btn.Location.Y;
           size = btn.Size;

           System.Drawing.Size newSize = new Size((int)(size.Width * 1.3), (int)(size.Height * 1.3));
           btn.Size = newSize;

           int residualX = (newSize.Width - size.Width) / 2;
           int residualY = (newSize.Height - size.Height) / 2;

           btn.Location = new Point(x - residualX, y - residualY);
        }

        private void Btn_MouseLeave(object sender, EventArgs e)
        {
            if (!(sender is Button))
                return;

            Button btn = sender as Button;

            btn.Location = new Point(x, y);
            btn.Size = size;
        }

        private void Btn_Click(object sender, EventArgs e)
        {
            if (!(sender is Button))
                return;

            MessageBox.Show((sender as Button).Text);
        }
    }

整个过程中最重要的方法是这个:BringToFront()方法。

截图:






我擦,忘了一个效果了,就是QQ上的按钮的激活的时候,是动态放大和缩小的,明天把那个动态的效果加上,完善下,现在半夜,失眠,不过也没心思,去做了

今晚重新完善了下,写成了一个控件代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;

namespace _TopButton
{
    public class ButtonEx : Button
    {
        private Thread magnifyThread = null;
        private Thread lessenThread = null;
        private Size _size;
        private Point _p;

        protected override void OnMouseEnter(EventArgs e)
        {
            StartMagnifyThread();
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            StartlessenThread();
            base.OnMouseLeave(e);
        }

        private void MagnifyMethod()
        {
            Invoke(new MethodInvoker(new Action(delegate()
            {
                _p = Location;
                _size = Size;
                BringToFront();
            })));
            int times = 1;
            double ratio = 0.02;//每次增长的比例
            int t = 1;
            while (times <= 10)
            {
                {
                    System.Drawing.Size newSize;
                    if (times < 8)
                    {
                        newSize = new Size((int)(_size.Width * (1 + (times) * ratio)),
                             (int)(_size.Height * (1 + (times) * ratio)));
                    }
                    else if (times == 8)
                    {
                        newSize = new Size((int)(_size.Width * (1 + (times + 5) * ratio)),
                            (int)(_size.Height * (1 + (times + 5) * ratio)));
                    }
                    else
                    {
                        newSize = new Size((int)(_size.Width * (1 + (times-t) * ratio)),
                            (int)(_size.Height * (1 + (times-t) * ratio)));
                        t = t + 2;
                    }
                    times++;

                    Invoke(new MethodInvoker(new Action(delegate()
                    {
                        Size = newSize;
                    })));

                    int residualX = (newSize.Width - _size.Width) / 2;
                    int residualY = (newSize.Height - _size.Height) / 2;

                    Invoke(new MethodInvoker(new Action(delegate()
                    {
                        Location = new Point(_p.X - residualX, _p.Y - residualY);
                    })));

                }
                Thread.Sleep(5);
            }
        }

        private void LessonMethod()
        {
            Invoke(new MethodInvoker(new Action(delegate()
            {
                {
                    Location = _p;
                    Size = _size;
                }
            })));
        }

        private void StartMagnifyThread()
        {
            //if ((magnifyThread == null || !magnifyThread.IsAlive) && (lessenThread == null || !lessenThread.IsAlive))
            {//注释掉了才行,分析下
                magnifyThread = new Thread(new ThreadStart(MagnifyMethod));
                magnifyThread.IsBackground = true;
                magnifyThread.Start();
            }
        }

        private void StartlessenThread()
        {
            if (magnifyThread != null && magnifyThread.IsAlive)
            {
                magnifyThread.Abort();
                magnifyThread = null;

            }
            lessenThread = new Thread(new ThreadStart(LessonMethod));
            lessenThread.IsBackground = true;
            lessenThread.Start();
        }
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值