using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 鼠标拖动移动坐标
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private bool isok ;
private int x; //鼠标点下去的横坐标
private int y;//鼠标点下去的纵坐标
private int cx;
private int cy;
private void Form1_MouseDown(object sender, MouseEventArgs e) //点下去
{
isok = true;
x = Cursor.Position.X; //鼠标点下去时候窗体的横坐标
y = Cursor.Position.Y;//鼠标点下去时候窗体的纵坐标
cx = this.Location.X;//窗体初始横坐标
cy = this.Location.Y;//窗体初始纵坐标
}
private void Form1_MouseUp(object sender, MouseEventArgs e)//松开
{
isok = false;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)//移动
{
if(isok)
{
int movex = Cursor.Position.X; //移动过程的横坐标
int movey = Cursor.Position.Y;//移动过程的纵坐标
this.Location = new Point(cx+movex-x,cy+movey-y); //初始坐标+移动的坐标-鼠标最开始点下去的坐标
}
}
}
}