c# 实现鼠标拖动form

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; } }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章