在WinForms中,窗体的移动可以通过以下方法实现:
private Point mouseOffset;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouseOffset = new Point(-e.X, -e.Y);
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseOffset.X, mouseOffset.Y);
Location = mousePos;
}
}
private const int WM_NCHITTEST = 0x0084;
private const int HT_CAPTION = 0x0002;
private const int WM_NCLBUTTONDOWN = 0x00A1;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NCHITTEST:
base.WndProc(ref m);
if (m.Result.ToInt32() == HT_CAPTION)
m.Result = new IntPtr(HT_CLIENT);
return;
case WM_NCLBUTTONDOWN:
if ((int)m.WParam == HT_CAPTION)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, new IntPtr(HT_CAPTION), IntPtr.Zero);
}
break;
}
base.WndProc(ref m);
}
以上是两种常用的方法,可以根据实际需要选择适合的方法来实现窗体的移动。