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


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