C# 在窗体中移动面板

C# Move Panel inside Form

我想在我的表单中制作某些面板 draggable/movable。我整合了:

    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;
    [DllImportAttribute("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    [DllImportAttribute("user32.dll")]
    public static extern bool ReleaseCapture();

根据我在这里找到的其他答案。连同:

    void pnlSettings_MouseMove(object sender, MouseEventArgs e)
    {
        Drag_Form(Handle, e);
    }
    public static void Drag_Form(IntPtr Handle, MouseEventArgs e){
        if (e.Button == MouseButtons.Left)
        {
          ReleaseCapture();
          SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        }
    }

而且似乎发生的是整个表单移动而不仅仅是面板 (pnlSettings)。我似乎无法弄清楚如何让面板单独移动。

Handle 是表单句柄。

您需要传递您要移动的控件的.Handle