c# 實現表單移動

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   os   使用   sp   for   

一般情況下:
 1 1.添加下列代碼到你的表單中: 2   #region 輕鬆移動 3  4 bool isInMove; 5   Point oldPoint; 6  7   void InitializeEasyMove() 8   { 9    isInMove = false;10 11    this.MouseDown += new MouseEventHandler(EasyMove_MouseDown);12    this.MouseUp += new MouseEventHandler(EasyMove_MouseUp);13    this.MouseMove += new MouseEventHandler(EasyMove_MouseMove);14   }15 16   void EasyMove_MouseMove(object sender, MouseEventArgs e)17   {18    if (!isInMove) return;19    Point pt = PointToScreen(e.Location);20    if (pt.X == oldPoint.X || pt.Y == oldPoint.Y) return;21    this.Location = new Point(this.Location.X + pt.X - oldPoint.X, this.Location.Y + pt.Y - oldPoint.Y);22    oldPoint = pt;23   }24 25   void EasyMove_MouseUp(object sender, MouseEventArgs e)26   {27    isInMove = false;28   }29 30   void EasyMove_MouseDown(object sender, MouseEventArgs e)31   {32    isInMove = true;33    oldPoint = PointToScreen(e.Location);34   }35 36   #endregion37 38 2.在你的表單的建構函式或Load事件中調用:39 InitializeEasyMove();

但是你會發現這樣很麻煩,運行時也容易出錯。

改進一:

增加mouseleave事件,當mouseleave的時候把isInMove 設定成false

這樣雖然改進了一點。但是還有有點彆扭

 

改進二:

使用win32api

 1 public partial class Form6 : Form 2     { 3         [DllImport("user32.dll")] 4         public static extern bool ReleaseCapture(); 5         [DllImport("user32.dll")] 6         public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); 7         public const int WM_SYSCOMMAND = 0x0112; 8         public const int SC_MOVE = 0xF010; 9         public const int HTCAPTION = 0x0002;10         11         public Form6()12         {13             InitializeComponent();14         }15 16         private void Form6_MouseDown(object sender, MouseEventArgs e)17         {18             ReleaseCapture();19             SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);20         }21     }

代碼量大大減少,直接消除滑鼠移快速移動時出現bug的現象。但是當滑鼠點擊pannel,groupbox等還是沒有反應

改進三:

 1 public partial class Form1 : Form 2     { 3         [DllImport("user32.dll")] 4         public static extern bool ReleaseCapture(); 5         [DllImport("user32.dll")] 6         public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); 7         public const int WM_SYSCOMMAND = 0x0112; 8         public const int SC_MOVE = 0xF010; 9         public const int HTCAPTION = 0x0002;10         public Form1()11         {12             InitializeComponent();13             foreach (var item in this.Controls)14             {15                 if ((item as GroupBox) != null)16                 {17                     (item as GroupBox).MouseDown += Form6_MouseDown;18                 }19                 else if ((item as Panel) != null)20                 {21                     (item as Panel).MouseDown += Form6_MouseDown;22                 }23             }24         }25 26         private void Form6_MouseDown(object sender, MouseEventArgs e)27         {28             ReleaseCapture();29             SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);30         }31     }

 

將GroupBox ,pannel等控制項添加mousedown動作

ok,大功告成

c# 實現表單移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.