我有一个 WinForms 项目。我的窗口顶部有一个面板。我希望该面板能够在用户单击它然后拖动时移动窗口。
我怎样才能做到这一点?
将以下声明添加到您的课程中:
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HTCAPTION = 0x2;
[DllImport("User32.dll")]
public static extern bool ReleaseCapture();
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
将其放入您的小组MouseDown
活动中:
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
}