通过在控件上单击并拖动来移动窗口

时间:2012-11-20 16:06:30

标签: c# windows winforms

我有一个WinForms项目。我的窗口顶部有一个面板。我希望该面板能够移动窗口,当用户点击它然后拖动时。

我该怎么做?

1 个答案:

答案 0 :(得分:15)

将以下声明添加到您的班级:

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);
    }
}