reprint:http://www.cnblogs.com/techmango/archive/2012/03/31/2427523.html
The first is to use the Windows Messaging mechanism to implement:
First, you define the message identity of the mouse button press, and secondly, in the Form1_mousedown method, let the operation system be wrong by pressing the title bar.
1. Define the message ID of the mouse button when left-click
1 Private Const int 0xa1; // . Define the mouse button and left click 2 Private Const int Htcaption 2;
2. Let the operating system be mistaken to press the title bar
1 Private voidForm1_mousedown (Objectsender, System.Windows.Forms.MouseEventArgs e)2 { 3 //for the pre-application program release mouse4 releasecapture ();5 //send a message to the system to press the mouse button on the title bar6SendMessage ((int) This. Handle,wm_nclbuttondown,htcaption,0);7}
3. Declare the API functions of Windows in the program
1[DllImport ("user32.dll", entrypoint="SendMessageA")]2 Private Static extern intSendMessage (inthwndintWmsg,intWParam,intlParam);3 4[DllImport ("user32.dll")]5 Private Static extern intReleaseCapture ();
The second, by customizing the left mouse button to press the event that occurs:
* First change the form's border style to none so that the form has no title bar
* This effect was achieved using three events: mouse down, mouse bounce, mouse movement
* Change the variable when the mouse is pressed Ismousedown marker form can move with mouse movement
* Change the Location property of the form according to the mouse movement, implement the form movement
* Change variable when mouse bounces Ismousedown mark form can not move with mouse movement
*/
1 Private BOOL false ; 2 Private Point Formlocation; // Location of form 3 Private Point Mouseoffset; // press position of mouse
1 Private voidForm1_mousedown (Objectsender, MouseEventArgs e)2 {3 if(E.button = =mousebuttons.left)4 {5Ismousedown =true;6Formlocation = This. Location;7Mouseoffset =control.mouseposition;8 }9}
1 Private void Form1_mouseup (object sender, MouseEventArgs e)2{3 false ; 4 }
1 Private voidForm1_mousemove (Objectsender, MouseEventArgs e)2 {3 int_x =0;4 int_y =0;5 if(Ismousedown)6 {7Point pt =control.mouseposition;8_x = Mouseoffset.x-Pt. X;9_y = Mouseoffset.y-Pt. Y;Ten One This. Location =NewPoint (Formlocation.x-_x, FORMLOCATION.Y-_y); A } -}
How to move a form (go) After setting no border in C #