c#中,当BorderStyle=None时,界面是没有框的,因此无法拖动,因此可以借助于Win32的系统API可以使用,首先导入包:using System.Runtime.InteropServices;
然后定义常量及调用的方法:
public const int Wm_ = 0x0112;
public const int Sc_ = 0xF010;
public const int Ht_ = 0x0002;
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwdn,int wMsg,int mParam,int lParam);
然后在属性里面,为界面设置mouseDown事件:
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, Wm_, Sc_ + Ht_, 0);
}