Drag IT


"I really would like to be able to move (drag) that custom control which I have just implemeneted on another custom control (say Panel )" said to myself this weekend... and started to implement MouseMove events... but it should not be that hard and actually it was not. If you just mess with Win32 a little bit, here is a quick solution for that. Here it is:


The idea is: just get the windows message (the message when you need to begin dragging your control) and tell Windows that the mouse is down on the caption (title) of the form (jut like you do when dragging the windows)


const int HTCAPTION = 0x2;


const int WM_NCLBUTTONDOWN = 0xA1;


[DllImportAttribute("user32.dll")]


public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);


[DllImportAttribute("user32.dll")]


public static extern bool ReleaseCapture();


 


private void Control_MouseDown(object sender, MouseEventArgs e) {


    ReleaseCapture();


    SendMessage(this.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);


}


Skip to main content