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
}
}
动画效果如下:
这是本人在一个项目中遇到的问题,所以特意总结出来分享给大家。