找传奇、传世资源到传世资源站!

用鼠标拖动无边框窗体功能的实现代码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

实现无边框窗体的拖动功能。
用鼠标拖动无边框窗体功能的实现代码 桌面应用界面/GUI-第1张
[DllImport("user32.dll")]
        public static extern bool ReleaseCapture();        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);        bool _BeginMove = false;//初始化鼠标位置
        int _CurrentXPosition;
        int _CurrentYPosition;        private void frmMain_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _BeginMove = true;
                _CurrentXPosition = Control.MousePosition.X;//鼠标的x坐标为当前窗体左上角x坐标
                _CurrentYPosition = Control.MousePosition.Y;//鼠标的y坐标为当前窗体左上角y坐标
            }
        }        private void frmMain_MouseMove(object sender, MouseEventArgs e)
        {
            if (_BeginMove)
            {
                this.Left = Control.MousePosition.X - _CurrentXPosition;//根据鼠标x坐标确定窗体的左边坐标x
                this.Top = Control.MousePosition.Y - _CurrentYPosition;//根据鼠标的y坐标窗体的顶部,即Y坐标
                _CurrentXPosition = Control.MousePosition.X;
                _CurrentYPosition = Control.MousePosition.Y;
            }
        }        private void frmMain_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _CurrentXPosition = 0; //设置初始状态
                _CurrentYPosition = 0;
                _BeginMove = false;
            }
        }        public void InitMoveWindowEvent(Control ctrl)
        {
            ctrl.MouseDown = frmMain_MouseDown;
            ctrl.MouseMove = frmMain_MouseMove;
            ctrl.MouseUp = frmMain_MouseUp;
        }

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复