Method One: By overloading the message processing implementation.
Dragging the mouse is only valid for the form itself and cannot be clicked on the control area on the form
/// <summary> ///implemented by overloading message processing. Rewrite the window procedure (WNDPROC) to process some non-client area messages (WM_NCXXXX),///C # Rewrite the window procedure no longer call the SetWindowLong API, directly overide a wndproc, do not declare API functions///dragging the mouse is only valid for the form itself and cannot be clicked on the control area on the form/// </summary> /// <param name= "M" ></param> protected Override voidWndProc (refMessage m) { Base. WndProc (refm); if(M.msg = =0x84) { Switch(M.result.toint32 ()) { Case 1: M.result=NewINTPTR (2); Break; } } }
View Code
Method Two: Call the unmanaged dynamic-link library, send a dragged message through the control's mouse-down event (MouseDown), you can add the MouseDown event to the control, and then drag the control to move the form
/// <summary> ///when you call an unmanaged dynamic-link library and send a dragged message through the control's mouse-down event (MouseDown), you can drag the control to move the form after you add the MouseDown event to the control./// </summary> /// <param name= "hWnd" ></param> /// <param name= "MSG" ></param> /// <param name= "WParam" ></param> /// <param name= "LParam" ></param> /// <returns></returns>[DllImport ("User32.DLL")] Public Static extern intSendMessage (IntPtr hWnd,UINTMSG,intWParam,intLParam); [DllImport ("User32.DLL")] Public Static extern BOOLreleasecapture (); Public Const UINTWm_syscommand =0x0112; Public Const intSc_move =61456; Public Const intHtcaption =2; Private voidForm1_mousedown (Objectsender, MouseEventArgs e) {releasecapture (); SendMessage (Handle, Wm_syscommand, Sc_move| Htcaption,0); }
View Code
Method Three: Write the event directly on the control, a friend is a PictureBox docked in the main form, and then the main form is set without a border, using this method
/// <summary> ///write an event directly on the control, a friend is a PictureBox docked in the main form, and then the main form is set without a border, using this method/// </summary>Point downpoint; Private voidPicturebox1_mousedown (Objectsender, MouseEventArgs e) {Downpoint=NewPoint (e.x, e.y); } Private voidPicturebox1_mousemove (Objectsender, MouseEventArgs e) { if(E.button = =mousebuttons.left) { This. Location =NewPoint ( This. Location.x + e.x-Downpoint.x, This. LOCATION.Y + e.y-downpoint.y); } }
View Code
. NET Rewrite url:http://www.cnblogs.com/yonsy/archive/2012/09/21/2696935.html