namespace Ex009
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool beginMove = false;//初始化
int currentXPosition ;
int currentYPosition ;
//鼠标移动事件
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (beginMove)
{
this.Left += MousePosition.X - currentXPosition;//根据鼠标x坐标确定窗体的左边坐标x
this.Top += MousePosition.Y - currentYPosition;//根据鼠标的y坐标窗体的顶部,即Y坐标
currentXPosition = MousePosition.X;
currentYPosition = MousePosition.Y;
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
beginMove = true;//当鼠标点击时开始移动
currentXPosition = MousePosition.X;//鼠标的x坐标为当前窗体左上角x坐标
currentYPosition = MousePosition.Y;//鼠标的y坐标为当前窗体左上角y坐标
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
beginMove = false;//当鼠标松开时停止移动
}
private void Form1_MouseLeave(object sender, EventArgs e)
{
currentXPosition = 0; //设置初始状态
currentYPosition = 0;//鼠标一移开时回复原始状态并停止移动
beginMove = false;
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool beginMove = false;//初始化
int currentXPosition ;
int currentYPosition ;
//鼠标移动事件
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (beginMove)
{
this.Left += MousePosition.X - currentXPosition;//根据鼠标x坐标确定窗体的左边坐标x
this.Top += MousePosition.Y - currentYPosition;//根据鼠标的y坐标窗体的顶部,即Y坐标
currentXPosition = MousePosition.X;
currentYPosition = MousePosition.Y;
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
beginMove = true;//当鼠标点击时开始移动
currentXPosition = MousePosition.X;//鼠标的x坐标为当前窗体左上角x坐标
currentYPosition = MousePosition.Y;//鼠标的y坐标为当前窗体左上角y坐标
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
beginMove = false;//当鼠标松开时停止移动
}
private void Form1_MouseLeave(object sender, EventArgs e)
{
currentXPosition = 0; //设置初始状态
currentYPosition = 0;//鼠标一移开时回复原始状态并停止移动
beginMove = false;
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}