private void Form1_MouseDown(object sender, MouseEventArgs e)
{
int x, y;
if (e.Button == MouseButtons.Left)
{
// e.X, e.Y 是鼠標在窗口的相對座標, 而們要改變窗口的屏幕座標 <pre name="code" class="csharp"> // SystemInformation.FrameBorderSize 爲窗體邊框大小
x = -e.X - SystemInformation.FrameBorderSize.Width; y = -e.Y - SystemInformation.FrameBorderSize.Height; MyPoint = new Point(x, y); isMouseDown = true; } } private void Form1_MouseMove(object sender, MouseEventArgs e) { if (isMouseDown) {
Point MousePoint = Control.MousePosition; MousePoint.Offset(MyPoint.X, MyPoint.Y);
// Location是窗口左上角的屏幕座標
Location = MousePoint; } } private void Form1_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { isMouseDown = false; } }