winform 拖动无边框窗体(调用Windows API)

第一步:将窗体的FormBoderStyle属性设置为None;

第二步:添加一个新类:Win32.cs

代码如下:

1 public class Win32
2     {
3         [DllImport("user32.dll")]
4         public static extern bool ReleaseCapture();
5 
6         [DllImport("user32.dll")]
7         public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
8     }

第三步:激活窗体的MouseDown事件

代码如下:

private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Win32.ReleaseCapture();
                Win32.SendMessage(Handle, 274, 61440 + 9, 0);
            }
        }

完成!!

原文地址:https://www.cnblogs.com/LJSL/p/3476818.html