對窗體加越界限制後,滑鼠拖動窗體越界時,窗體不能動,滑鼠位置可動,但窗體不再越界時,滑鼠位置還能回到滑鼠按下時相對窗體的坐標:1、首先創建一個窗體Form1,然後在窗體上拖一個button1按鈕(主要通過這個按鈕來拖動窗體)2、然後對按鈕添加滑鼠按下事件、滑鼠移動事件和滑鼠抬起事件,事件裡面是對窗體 ...
對窗體加越界限制後,滑鼠拖動窗體越界時,窗體不能動,滑鼠位置可動,但窗體不再越界時,滑鼠位置還能回到滑鼠按下時相對窗體的坐標:
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
}
}
動畫效果如下:
這是本人在一個項目中遇到的問題,所以特意總結出來分享給大家。