using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SixGroup_Knowledge.Model
{
    public partial class PopModel : Form
    {
        //获得屏幕的宽和高
        protected int winWidth = Screen.PrimaryScreen.Bounds.Width;
        protected int winHeight = Screen.PrimaryScreen.Bounds.Height;
        //窗体是否可被移动
        private Boolean isMove = false;
        //窗体移动标记位置
        private Point mouseOffset;
        public PopModel()
        {
            InitializeComponent();
        }
        private void moveTitle_MouseDown(object sender, MouseEventArgs e)
        {
            this.isMove = true;//鼠标按下时设置窗口为可拖动
            mouseOffset = new Point(-e.X, -e.Y);
        }
        private void moveTitle_MouseUp(object sender, MouseEventArgs e)
        {
            this.isMove = false;//鼠标抬起时设置窗口为不可拖动
        }

        //窗体移动
        private void moveTitle_MouseMove(object sender, MouseEventArgs e)
        {
            //如果为不可移动状态则直接返回
            if (!this.isMove)
                return;
            //获得鼠标在屏幕中的位置
            Point mouseSet = Control.MousePosition;
            //减去鼠标在窗体中的位置
            mouseSet.Offset(mouseOffset.X, mouseOffset.Y);

            //防止窗体被拖出屏幕
            if (mouseSet.X < 0)
                mouseSet.X = 0;
            if (mouseSet.X > this.winWidth - this.Width)
                mouseSet.X = this.winWidth - this.Width;
            if (mouseSet.Y < 0)
                mouseSet.Y = 0;
            if (mouseSet.Y > this.winHeight - this.Height)
                mouseSet.Y = this.winHeight - this.Height;
            //设置窗体位置
            Location = mouseSet;
        }
    }
}
