讓普通控制元件擁有左鍵移動窗體的功能

Old發表於2014-09-21

如題,程式碼如下:

        /// <summary>設定可以滑鼠左鍵讓窗體移動的控制元件
        /// </summary>
        /// <param name="form">待移動的窗體,通常是設定了FormBorderStyle = FormBorderStyle.None的窗體</param>
        /// <param name="control">能控制窗體左鍵移動的控制元件</param>
        public static void SetMoveFormControl(Form form, Control control)
        {
            control.MouseDown += (s, e) =>
            {
                ReleaseCapture();
                SendMessageA((int)form.Handle, 0xa1, 2, 0);
            };
        }
        [DllImport("user32.dll")]
        private static extern int ReleaseCapture();
        [DllImport("user32.dll")]
        private static extern int SendMessageA(int hwnd, int wMsg, int wParam, int lParam);

  

相關文章