1. 程式人生 > >C#點選視窗任意位置可拖拽

C#點選視窗任意位置可拖拽

直接上程式碼

private Point offset;

private void Form1_MouseDown(object sender, MouseEventArgs e)
 {
            if (MouseButtons.Left != e.Button) return;

            Point cur = this.PointToScreen(e.Location);
            offset = new Point(cur.X - this.Left, cur.Y - this.Top);
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
            if (MouseButtons.Left != e.Button) return;

            Point cur = MousePosition;
            this.Location = new Point(cur.X - offset.X, cur.Y - offset.Y);

}