WinForm不让窗体移动到屏幕外

不让Form移动到屏幕外,先给窗体添加Move事件,然后判断并重新设置位置。

代码:

private void FormMain_Move(object sender, EventArgs e)
        {
            Rectangle r = Screen.GetWorkingArea(this);
            if(this.Top <=0)
            {
                this.Top = r.Top;
            }
            if (this.Left <=0)
            {
                this.Left = 0;
            }
            if (this.Right > r.Right)
            {
                this.Left = r.Right - this.Width;
            }
            if (this.Bottom > r.Bottom)
            {
                this.Top = r.Bottom - this.Height;
            }
        }

 

posted on 2016-01-07 09:23  田夫  阅读(457)  评论(0编辑  收藏  举报

导航