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

http://blog.csdn.net/yan_hyz/article/details/7776333

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

  1. public partial class FormMain : Form  
  2.   {  
  3.       public FormMain()  
  4.       {  
  5.           InitializeComponent();  
  6.       }  
  7.   
  8.       int x = 0;//保存鼠标进入的按钮的原始位置X坐标   
  9.       int y = 0;//保存鼠标进入的按钮的原始位置Y坐标   
  10.   
  11.       System.Drawing.Size size = new Size();//保存鼠标进入的按钮的原始大小   
  12.   
  13.       private void Btn_MouseEnter(object sender, EventArgs e)  
  14.       {  
  15.           if (!(sender is Button))  
  16.               return;  
  17.   
  18.          Button btn = sender as Button;  
  19.          btn.BringToFront();  
  20.   
  21.          x = btn.Location.X;  
  22.          y = btn.Location.Y;  
  23.          size = btn.Size;  
  24.   
  25.          System.Drawing.Size newSize = new Size((int)(size.Width * 1.3), (int)(size.Height * 1.3));  
  26.          btn.Size = newSize;  
  27.   
  28.          int residualX = (newSize.Width - size.Width) / 2;  
  29.          int residualY = (newSize.Height - size.Height) / 2;  
  30.   
  31.          btn.Location = new Point(x - residualX, y - residualY);  
  32.       }  
  33.   
  34.       private void Btn_MouseLeave(object sender, EventArgs e)  
  35.       {  
  36.           if (!(sender is Button))  
  37.               return;  
  38.   
  39.           Button btn = sender as Button;  
  40.   
  41.           btn.Location = new Point(x, y);  
  42.           btn.Size = size;  
  43.       }  
  44.   
  45.       private void Btn_Click(object sender, EventArgs e)  
  46.       {  
  47.           if (!(sender is Button))  
  48.               return;  
  49.   
  50.           MessageBox.Show((sender as Button).Text);  
  51.       }  
  52.   }  
  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上的按钮的激活的时候,是动态放大和缩小的,明天把那个动态的效果加上,完善下,现在半夜,失眠,不过也没心思,去做了

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

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows.Forms;  
  6. using System.Drawing;  
  7. using System.Threading;  
  8.   
  9. namespace _TopButton  
  10. {  
  11.     public class ButtonEx : Button  
  12.     {  
  13.         private Thread magnifyThread = null;  
  14.         private Thread lessenThread = null;  
  15.         private Size _size;  
  16.         private Point _p;  
  17.   
  18.         protected override void OnMouseEnter(EventArgs e)  
  19.         {  
  20.             StartMagnifyThread();  
  21.             base.OnMouseEnter(e);  
  22.         }  
  23.   
  24.         protected override void OnMouseLeave(EventArgs e)  
  25.         {  
  26.             StartlessenThread();  
  27.             base.OnMouseLeave(e);  
  28.         }  
  29.   
  30.         private void MagnifyMethod()  
  31.         {  
  32.             Invoke(new MethodInvoker(new Action(delegate()  
  33.             {  
  34.                 _p = Location;  
  35.                 _size = Size;  
  36.                 BringToFront();  
  37.             })));  
  38.             int times = 1;  
  39.             double ratio = 0.02;//每次增长的比例   
  40.             int t = 1;  
  41.             while (times <= 10)  
  42.             {  
  43.                 {  
  44.                     System.Drawing.Size newSize;  
  45.                     if (times < 8)  
  46.                     {  
  47.                         newSize = new Size((int)(_size.Width * (1 + (times) * ratio)),  
  48.                              (int)(_size.Height * (1 + (times) * ratio)));  
  49.                     }  
  50.                     else if (times == 8)  
  51.                     {  
  52.                         newSize = new Size((int)(_size.Width * (1 + (times + 5) * ratio)),  
  53.                             (int)(_size.Height * (1 + (times + 5) * ratio)));  
  54.                     }  
  55.                     else  
  56.                     {  
  57.                         newSize = new Size((int)(_size.Width * (1 + (times-t) * ratio)),  
  58.                             (int)(_size.Height * (1 + (times-t) * ratio)));  
  59.                         t = t + 2;  
  60.                     }  
  61.                     times++;  
  62.   
  63.                     Invoke(new MethodInvoker(new Action(delegate()  
  64.                     {  
  65.                         Size = newSize;  
  66.                     })));  
  67.   
  68.                     int residualX = (newSize.Width - _size.Width) / 2;  
  69.                     int residualY = (newSize.Height - _size.Height) / 2;  
  70.   
  71.                     Invoke(new MethodInvoker(new Action(delegate()  
  72.                     {  
  73.                         Location = new Point(_p.X - residualX, _p.Y - residualY);  
  74.                     })));  
  75.   
  76.                 }  
  77.                 Thread.Sleep(5);  
  78.             }  
  79.         }  
  80.   
  81.         private void LessonMethod()  
  82.         {  
  83.             Invoke(new MethodInvoker(new Action(delegate()  
  84.             {  
  85.                 {  
  86.                     Location = _p;  
  87.                     Size = _size;  
  88.                 }  
  89.             })));  
  90.         }  
  91.   
  92.         private void StartMagnifyThread()  
  93.         {  
  94.             //if ((magnifyThread == null || !magnifyThread.IsAlive) && (lessenThread == null || !lessenThread.IsAlive))   
  95.             {//注释掉了才行,分析下   
  96.                 magnifyThread = new Thread(new ThreadStart(MagnifyMethod));  
  97.                 magnifyThread.IsBackground = true;  
  98.                 magnifyThread.Start();  
  99.             }  
  100.         }  
  101.   
  102.         private void StartlessenThread()  
  103.         {  
  104.             if (magnifyThread != null && magnifyThread.IsAlive)  
  105.             {  
  106.                 magnifyThread.Abort();  
  107.                 magnifyThread = null;  
  108.   
  109.             }  
  110.             lessenThread = new Thread(new ThreadStart(LessonMethod));  
  111.             lessenThread.IsBackground = true;  
  112.             lessenThread.Start();  
  113.         }  
  114.     }  
  115. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值