標籤:
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); //初始座標+移動的座標-滑鼠最開始點下去的座標 } } }}
滑鼠拖動 移動座標