对窗体加越界限制后,鼠标拖动窗体越界时,窗体不能动,鼠标位置可动,但窗体不再越界时,鼠标位置还能回到鼠标按下时相对窗体的坐标:
1、首先创建一个窗体Form1,然后在窗体上拖一个button1按钮(主要通过这个按钮来拖动窗体)

2、然后对按钮添加鼠标按下事件、鼠标移动事件和鼠标抬起事件,事件里面是对窗体和鼠标坐标的操作,整体代码如下:
using System.Drawing;
using System.Windows.Forms;
namespace 窗体越界鼠标还能回到初始坐标
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        #region 变量定义
        public Point mainFormPosition;//主窗口左上角位置
        int workingAreaHeight = SystemInformation.WorkingArea.Height;
        int workingAreaWidth = SystemInformation.WorkingArea.Width;
        private Point relativetMousePosition;//鼠标相对窗体的相对坐标
        bool FirstLeft = false;//是否左边第一次按下时坐标
        bool FirstRight = false;//是否右边第一次按下时坐标
        bool FirstUp = false;//是否上边第一次按下时坐标
        bool FirstDown = false;//是否下边第一次按下时坐标
        bool isFirstDown = true;//是否第一次按下,鼠标越界纠正时判断是否是第一次按下,不是按下后又拖动了
        bool isMoveOut = false;//是否拖出
        //这两个变量做差,差值为拖出的距离时软件弹出侧边
        int oldMove_X = 0;//侧边停靠拖出时起始坐标
        //上一个时刻、鼠标的位置
        private Point oldMousePosition;
        //当前时刻、鼠标的位置
        private Point currentMousePosition;
        //鼠标事件
        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
        //移动鼠标
        const int MOUSEEVENTF_MOVE = 0x0001;
        //模拟鼠标左键按下
        const int MOUSEEVENTF_LEFTDOWN = 0x0002;
        //模拟鼠标左键抬起
        const int MOUSEEVENTF_LEFTUP = 0x0004;
        //模拟鼠标右键按下
        const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        //模拟鼠标右键抬起
        const int MOUSEEVENTF_RIGHTUP = 0x0010;
        //模拟鼠标中键按下
        const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
        //模拟鼠标中键抬起
        const int MOUSEEVENTF_MIDDLEUP = 0x0040;
        //标示是否采用绝对坐标
        const int MOUSEEVENTF_ABSOLUTE = 0x8000;
        #endregion

#region 窗体初始化
        public Form1()
        {
            InitializeComponent();
        }
        #endregion

//-------------------------按钮button1鼠标事件---------------------------------------------------------------
        #region 鼠标左键按下Button1按钮事件
        private void Button1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                currentMousePosition = Control.MousePosition;
                oldMousePosition = currentMousePosition;   //鼠标按下时、两个时刻的坐标变成一样,防止错误移动            
                mainFormPosition = Location;
                if (isFirstDown)//鼠标按下时,记录第一次鼠标按下相对主窗体的坐标
                {
                    relativetMousePosition.X = currentMousePosition.X - Location.X;
                    relativetMousePosition.Y = currentMousePosition.Y - Location.Y;
                    isFirstDown = false;
                }
                if (isMoveOut)//鼠标按下时,记录拖出时的初始坐标
                {
                    oldMove_X = currentMousePosition.X;
                }
            }
        }
        #endregion

#region 鼠标左键按住Button1按钮移动事件
        private void Button1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                currentMousePosition = Control.MousePosition;
                if (currentMousePosition.X < relativetMousePosition.X && currentMousePosition.Y > relativetMousePosition.Y && currentMousePosition.Y < workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y)//左边窗体越界坐标纠正
                {
                    mainFormPosition.X = 0;
                    mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                    //保存上一个时刻的鼠标位置
                    oldMousePosition.Y = currentMousePosition.Y;
                    FirstLeft = true;
                }
                else if (currentMousePosition.X > workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X && currentMousePosition.Y > relativetMousePosition.Y && currentMousePosition.Y < workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y)//右边窗体越界坐标纠正
                {
                    mainFormPosition.X = workingAreaWidth - this.ClientSize.Width;
                    mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                    //保存上一个时刻的鼠标位置
                    oldMousePosition.Y = currentMousePosition.Y;
                    FirstRight = true;
                }
                else if (currentMousePosition.Y < relativetMousePosition.Y && currentMousePosition.X > relativetMousePosition.X && currentMousePosition.X < workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X)//上边窗体越界坐标纠正
                {
                    mainFormPosition.Y = 0;
                    mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                    //保存上一个时刻的鼠标位置
                    oldMousePosition.X = currentMousePosition.X;
                    FirstUp = true;
                }
                else if (currentMousePosition.Y > workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y && currentMousePosition.X > relativetMousePosition.X && currentMousePosition.X < workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X)//下边窗体越界坐标纠正
                {
                    mainFormPosition.Y = workingAreaHeight - this.ClientSize.Height;
                    mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                    //保存上一个时刻的鼠标位置
                    oldMousePosition.X = currentMousePosition.X;
                    FirstDown = true;
                }
                else if (currentMousePosition.X < relativetMousePosition.X && currentMousePosition.Y < relativetMousePosition.Y)//左上角窗体越界坐标纠正
                {
                    mainFormPosition.X = 0;
                    mainFormPosition.Y = 0;
                    mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                    mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                    //保存上一个时刻的鼠标位置
                    oldMousePosition.X = currentMousePosition.X;
                    oldMousePosition.Y = currentMousePosition.Y;
                    FirstLeft = true;
                    FirstUp = true;
                }
                else if (currentMousePosition.X > workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X && currentMousePosition.Y < relativetMousePosition.Y)//右上角窗体越界坐标纠正
                {
                    mainFormPosition.X = workingAreaWidth - this.ClientSize.Width;
                    mainFormPosition.Y = 0;
                    mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                    mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                    //保存上一个时刻的鼠标位置
                    oldMousePosition.X = currentMousePosition.X;
                    oldMousePosition.Y = currentMousePosition.Y;
                    FirstRight = true;
                    FirstUp = true;
                }
                else if (currentMousePosition.X < relativetMousePosition.X && currentMousePosition.Y > workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y)//左下角窗体越界坐标纠正
                {
                    mainFormPosition.X = 0;
                    mainFormPosition.Y = workingAreaHeight - this.ClientSize.Height;
                    mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                    mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                    //保存上一个时刻的鼠标位置
                    oldMousePosition.X = currentMousePosition.X;
                    oldMousePosition.Y = currentMousePosition.Y;
                    FirstLeft = true;
                    FirstDown = true;
                }
                else if (currentMousePosition.X > workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X && currentMousePosition.Y > workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y)//右下角窗体越界坐标纠正
                {
                    mainFormPosition.Y = workingAreaHeight - this.ClientSize.Height;
                    mainFormPosition.X = workingAreaWidth - this.ClientSize.Width;
                    mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                    mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                    //保存上一个时刻的鼠标位置
                    oldMousePosition.X = currentMousePosition.X;
                    oldMousePosition.Y = currentMousePosition.Y;
                    FirstRight = true;
                    FirstDown = true;
                }
                if (FirstLeft)
                {
                    currentMousePosition.X = relativetMousePosition.X;
                    FirstLeft = false;
                }
                if (FirstRight)
                {
                    currentMousePosition.X = workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X;
                    FirstRight = false;
                }
                if (FirstUp)
                {
                    currentMousePosition.Y = relativetMousePosition.Y;
                    FirstUp = false;
                }
                if (FirstDown)
                {
                    currentMousePosition.Y = workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y;
                    FirstDown = false;
                }
                if (FirstLeft && FirstUp)
                {
                    currentMousePosition.X = relativetMousePosition.X;
                    currentMousePosition.Y = relativetMousePosition.Y;
                    FirstLeft = false;
                    FirstUp = false;

}
                if (FirstRight && FirstUp)
                {
                    currentMousePosition.X = workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X;
                    currentMousePosition.Y = relativetMousePosition.Y;
                    FirstUp = false;
                    FirstRight = false;
                }
                if (FirstLeft && FirstDown)
                {
                    currentMousePosition.X = relativetMousePosition.X;
                    currentMousePosition.Y = workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y;
                    FirstLeft = false;
                    FirstDown = false;
                }
                if (FirstRight && FirstDown)
                {
                    currentMousePosition.X = workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X;
                    currentMousePosition.Y = workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y;
                    FirstRight = false;
                    FirstDown = false;
                }
                //对窗体进行移动
                mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                //保存上一个时刻的鼠标位置
                oldMousePosition = currentMousePosition;

#region 越界处理
                if (mainFormPosition.X < 0)//左边越界
                {
                    mainFormPosition.X = 0;
                }
                else if (mainFormPosition.Y < 0)//上边越界
                {
                    mainFormPosition.Y = 0;
                }
                else if (mainFormPosition.X > workingAreaWidth - this.ClientSize.Width)//右边越界
                {
                    mainFormPosition.X = workingAreaWidth - this.ClientSize.Width;
                }
                else if (mainFormPosition.Y > workingAreaHeight - this.ClientSize.Height)//下边越界
                {
                    mainFormPosition.Y = workingAreaHeight - this.ClientSize.Height;
                }
                //更新主窗口位置
                Location = mainFormPosition;
                #endregion
            }
        }
        #endregion

#region 鼠标在按钮Button1抬起事件
        private void Button1_MouseUp(object sender, MouseEventArgs e)
        {
            isFirstDown = true;
            //如果主窗体在这个位置,就判断允许主窗体在鼠标抬起后执行移动事件
            if (mainFormPosition.X == workingAreaWidth - this.ClientSize.Width)
            {
                isMoveOut = false;
            }
            if (mainFormPosition.X == 0)
            {
                isMoveOut = false;
            }
        }
        #endregion
    }
}

动画效果如下:

这是本人在一个项目中遇到的问题,所以特意总结出来分享给大家。

C#窗体越界时鼠标还能回到初始坐标位置的更多相关文章

  1. Javascript获取当前鼠标在元素内的坐标定位

    代码如下: <!doctype html> <html> <head> <meta charset="utf-8"> <tit ...

  2. Qt无边框窗体-最大化时支持拖拽还原

    目录 一.概述 二.效果展示 三.demo制作 1.设计窗体 2.双击放大 四.拖拽 五.相关文章 原文链接:Markdown模板 一.概述 用Qt进行开发界面时,既想要实现友好的用户交互又想界面漂亮 ...

  3. 黑马程序员 关于c# windows窗体关闭时线程未能完全退出问题(专题一)

    <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IO开发S</a> ...

  4. WPF 子窗体关闭时显示父窗体

    这个问题纠结了两天,今天在一个朋友的帮助下,解决了,其实很简单,但是可能作为新手,接触WPF时间还是短,因此作为一个问题困扰了我. 父窗体部分代码 private void EditInformati ...

  5. Qt窗体关闭时,如何自动销毁窗体类对象

    Qt窗体关闭时,如何自动销毁窗体类对象     要对你的窗口设置WA_DeleteOnClose属性,默认的情况下关闭窗口仅仅意味着隐藏它 ImgWindow1->setAttribute(Qt ...

  6. DirectUI中模态对话框和菜单的原理(自己控制整个Windows消息循环。或者,用菜单模拟窗体打开时用SetCapture取得控制权,一旦窗体收到WM_CAPTURECHANGED消息就把窗体退出)

    经常有人问关于模态对话框和系统菜单内部实现原理方面的问题, 因为系统通过API隐藏了太多细节,这2个问题确实令初学者甚至是有经验的开发者困扰, 下面是我个人的一些经验总结. 先说模态对话框,外部看模态 ...

  7. 启动weblogic服务时,还需要输入用户名和密码的解决方法

    当启动weblogic服务时,还需要输入用户名和密码,相当繁琐,如下: 而在生产环境中,一般会要求不要在每次启动时都输入用户名密码, 因此可以通过一些简单的配置达到此目的,通常的做法有两种: 1.修改 ...

  8. C# WinForm 关于窗体最大化时的是否全屏效果与是否遮盖任务栏

    0.新建窗体 及添加按钮 1.  执行如下按钮事件  private void btnFormMax_Click(object sender, EventArgs e)  {     if (this ...

  9. sublime打开文本时会记忆上次关闭时鼠标停留的位置

    sublime打开文本时会记忆上次关闭时鼠标停留的位置

随机推荐

  1. java基础0615

    1. 1)2)   1)输出:Base  2)编译成功,但没有输出. 2. 编译成功,但没有输出. 3. 只有12行的话,不会新建文件.需要create~~ 4. public static void ...

  2. 不适合使用hadoop来解决的问题

    1.Hadoop能解决的问题必须是可以mapreduce的.一是问题可以拆分,二是子问题必须独立.比如斐波那契数列就不适合. 2.数据结构不满足key-value形式的.比如结构化的数据查询. 3.不 ...

  3. HTB Linux queuing discipline manual - user guide笔记

    1. Introduction HTB is meant as a more understandable, intuitive and faster replacement for the CBQ ...

  4. 与其他相似软件对比,win10中个人助理conrtana具备哪些独特的功能

    目前,Cortana 可以回答各种口头问题,直接设置提醒,或者提供位置导航,并支持语音命令处理各项事务,而且随时间的推移学习更多内容,从而变得更加个性化和实用.简单而言,集成在 Edge 浏览器中的 ...

  5. 每天学点SpringCloud(十三):SpringCloud-Stream整合RabbitMQ

    我们知道,当微服务越来越来多的时候,仅仅是feign的http调用方式已经满足不了我们的使用场景了.这个时候系统就需要接入消息中间件了.相比较于传统的Spring项目.SpringBoot项目使用消息 ...

  6. java实操之使用jcraft进行sftp上传下载文件

    sftp作为临时的文件存储位置,在某些场合还是有其应景的,比如对账文件存放.需要提供一个上传的工具类.实现方法参考下: pom.xml中引入类库: <dependency> <gro ...

  7. RISC-V指令集介绍 - 整数基本指令集

    1. 寄存器 32个x寄存器,RV32下x reg是32位宽 x0:硬连线 常数0 专门的零寄存器 x1-x31:31个通用reg 返回地址:没有强制要求那一个x作为lr,但是一般用x1 pc:额外的 ...

  8. shell脚本中if的“-e,-d,-f”

    文件表达式-e filename 如果 filename存在,则为真-d filename 如果 filename为目录,则为真 -f filename 如果 filename为常规文件,则为真-L ...

  9. Python档案袋( Sys 与 Import 模块)

    Sys模块: 获取Python有关的环境变量: import sys #得到Python的一些相关路径,环境变量 #其中site-packages目录存放的是一些第三方库 #其中lib目录存放的是一些 ...

  10. JavaScript是如何工作的:深入类和继承内部原理 + Babel和TypeScript之间转换

    现在构建任何类型的软件项目最流行的方法这是使用类.在这篇文章中,探讨用 JavaScript 实现类的不同方法,以及如何构建类的结构.首先从深入研究原型工作原理,并分析在流行库中模拟基于类的继承的方法 ...