[Form] 設定 FormBorderStyle.None 時, 讓 Form 移動

[Form] 設定 FormBorderStyle.None 時, 讓 Form 移動


using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WinFormMove
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.MouseDown += new MouseEventHandler(this.MouseDown1);
            this.MouseMove += new MouseEventHandler(this.MouseMove1);
        }

        private void Form1_Load(object sender, EventArgs e){}

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();

        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        public const int WM_SYSCOMMAND = 0X0112;
        public const int SC_MOVE = 0XF010;
        public const int HTCAPTION = 0X0002;

        private void MouseDown1(object sender, MouseEventArgs e)
        {
            if (e.Y > 65)
                return;

            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

        private void MouseMove1(object sender, MouseEventArgs e)
        {
            if (e.Y < 65)
                this.Cursor = Cursors.SizeAll;

            else
                this.Cursor = Cursors.Default;
        }
    }
}