在WinForm中实现收缩功能

1.使用WINDOWS API和TIMER控件
 class Win32API
    {
        [DllImport("user32")]
        public static extern bool PtInRect(ref Rectangle r, Point p);
	//方法名称固定
    }


向上方收缩代码:
  private void timer1_Tick(object sender, EventArgs e)
        {
            Point pp = new Point(Cursor.Position.X, Cursor.Position.Y);
            Rectangle rects = new Rectangle(this.Left, this.Top, this.Left + this.Width, this.Top + this.Height);

            if ((this.Top < 0) && Win32API.PtInRect(ref rects, pp))
            {
                this.Top = 0;
            }
            else
            {
                if (this.Top > -5 && this.Top < 5 && !(Win32API.PtInRect(ref rects, pp)))
                {
                    this.Top = 5 - this.Height;
                }
            }
        }


向左边收缩代码:
private void timer2_Tick(object sender, EventArgs e)
        {
            Point pp = new Point(Cursor.Position.X, Cursor.Position.Y);
            Rectangle rects = new Rectangle(this.Left, this.Top, this.Left + this.Width, this.Top + this.Height);

            if ((this.Left < 0) && Win32API.PtInRect(ref rects, pp))
            {
                this.Left = 0;
            }
            else
            {
                if (this.Left > -5 && this.Left < 5 && !(Win32API.PtInRect(ref rects, pp)))
                {
                    this.Left = 5 - this.Right;
                }
            }
        }


启动两个timer控件:
 	this.timer1.Start();
        this.timer2.Start();	


2.使用事件

        double width;
        double height;
        private void win_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();

            if (this.Top <= 0)
            {
                this.Top = 0;
                this.Height = 10;
            }

            if (this.Left <= 0)
            {
                this.Left = 0;
                this.Width = 10;
            }
        }


        private void win_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.Top == 0)
            {
                this.Height = height;
            }

            if (this.Left == 0)
            {
                this.Width = width;
            }
        }

        private void win_MouseLeave(object sender, MouseEventArgs e)
        {
            if (this.Top == 0 && this.Height == height)
            {
                this.Height = 10;
            }

            if (this.Left == 0 && this.Width == width)
            {
                this.Width = 10;
            }
        }

        private void win_Loaded(object sender, RoutedEventArgs e)
        {
            height = win.Height;
            width = win.Width;

            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值