C#窗体越界时鼠标还能回到初始坐标位置

C#窗体越界时鼠标还能回到初始坐标位置

对窗体加越界限制后,鼠标拖动窗体越界时,窗体不能动,鼠标位置可动,但窗体不再越界时,鼠标位置还能回到鼠标按下时相对窗体的坐标:
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#窗体越界时鼠标还能回到初始坐标位置

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

转载于:https://www.cnblogs.com/qiantao/p/9531532.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/101470.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • jenkins 邮件_邮件发送服务

    jenkins 邮件_邮件发送服务前言前面已经实现在jenkins上展示html的测试报告,接下来只差最后一步,把报告发给你的领导,展示你的劳动成果了。安装EmailExtensionPlugin插件jenkins首页-

  • 域名、服务器、IP之间的关系[通俗易懂]

    域名、服务器、IP之间的关系[通俗易懂]文章转载自:[https://www.jianshu.com/p/c3d3f6629c13](https://www.jianshu.com/p/c3d3f6629c13)本文主要针对域名、服务器和IP之间的关系进行介绍,并通过实际案例讲解域名解析过程本文目录基础概念案例分析阿里云域名解析过程基础概念服务器:服务器其实就像我们的家用电脑一样,也有主板、CPU、内存、硬盘、电源等。根据功能来说服务器可分为web服务器、ftp服务器、数据库服务器、邮件服务器等等,做什么用途就可以叫做什么服务器

  • CAN 接口测试[通俗易懂]

    CAN 接口测试[通俗易懂]CAN测试收发程序can发送测试#include<stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<net/if.h>#include<sys/ioctl.h>#include<sys/socket…

  • android 混淆不起作用,Android代码混淆的写法总结

    android 混淆不起作用,Android代码混淆的写法总结Apk文件被反编译出来能被获取到里面的代码。对于这种情况,我们可以对项目代码进行混淆,随机生成难理解的类名,方法名,让代码难以阅读,加大功能被盗取的难度。混淆可以起到压缩Apk,混淆文件,预检,优化的作用。1.使用方式,在gradle文件中设置minifyEnabled为true即可开启混淆buildTypes{release{minifyEnabledture//是否开启代码混淆pro…

  • html——form表单提交方法及表单提交注意点

    html——form表单提交方法及表单提交注意点1、type=”submit”&amp;amp;amp;amp;amp;amp;lt;formname=”form”method=”post”action=”#&amp;amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;lt;inputtype=”submit”name=”submit”value=”提交&amp;amp;amp;amp;amp;quo

  • 电脑很大,电脑内存很大为什么还是很卡「建议收藏」

    电脑很大,电脑内存很大为什么还是很卡「建议收藏」2019-10-09阅读(121)”电脑反应慢原因:1.电脑反应太慢,第一我们打开任务管理器,检查CPU资源的使用,若某个程序的CPU资源占用一直比较高(比如占到50%以上),系统速度就会明显变慢。因一般情况下,除大型游戏程序之外,应用程序在运行过程中只是瞬间占用较高,而不会一直持续2019-10-09阅读(132)一.清理垃圾在Windows在安装和使用过程中都会产生相当多的垃圾文件,包括临时文…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号