-1

I'm trying to make my panel control movable using c#, but my main form moves along with the panel control when dragging. Is there a better code for this?

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern bool ReleaseCaptre();

public static void _MOVE_PANEL(IntPtr Handle, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
       ReleaseCaptre();
       SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
    } 
}

private void sub_inv_pcount_edit_MouseMove(object sender, MouseEventArgs e) //panel move
{
    _MOVE_PANEL(Handle, e);
}

see sample: see screenshot

Stefan
  • 652
  • 5
  • 19
J.szareyo
  • 25
  • 8
  • 2
    That's what WM_NCLBUTTONDOWN does, it pretends that you clicked the title bar of a main window. Which moves the whole shebang. Google "c# move panel with mouse" to find better code, top hits on the MSDN forums as well as Stackoverflow look good. – Hans Passant Sep 27 '19 at 10:28
  • [This SO](https://stackoverflow.com/questions/3868941/how-to-allow-user-to-drag-a-dynamically-created-control-at-the-location-of-his-c) and [MSDN](https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/walkthrough-performing-a-drag-and-drop-operation-in-windows-forms) may help you!? – Siva Gopal Sep 27 '19 at 10:44

1 Answers1

0

I'm afraid this is not an approach you can tweak to get the result you want. WM_NCLBUTTONDOWN is essentially how the window manager decides how to handle mouse events on a top-level window. It allows you to make windows that have custom chrome, but it doesn't allow you to do anything about non-top-level windows (such as your panel).

You can get a reasonably nice-working dragging doing the obvious thing - in MouseMove, handle moving the control yourself. It's not going to be as well behaved as moving the entire window, though.

Luaan
  • 62,244
  • 7
  • 97
  • 116