C#winform No Border form Move method, imitate mouse click title bar move form Position
Here are two ways to do this.
Method one: Directly by modifying the form position to achieve the effect of moving the form
Method Two: Direct spoofing send click the taskbar message to let the application mistakenly think that clicking on the taskbar moves the form
New form for testing
Method 11. Define a location information point for storing the mouse position
1 Private Point Mpoint;
2. Add MouseDown and MouseMove events to controls such as forms
1 /// <summary>2 ///mouse button down3 /// </summary>4 /// <param name= "Sender" ></param>5 /// <param name= "E" ></param>6 Private voidPanel1_mousedown (Objectsender, MouseEventArgs e)7 {8Mpoint =NewPoint (e.x, e.y);9 }Ten One /// <summary> A ///Mouse Movement - /// </summary> - /// <param name= "Sender" ></param> the /// <param name= "E" ></param> - Private voidPanel1_mousemove (Objectsender, MouseEventArgs e) - { - if(E.button = =mousebuttons.left) + { - This. Location =NewPoint ( This. Location.x + e.x-mpoint.x, This. LOCATION.Y + e.y-mpoint.y); + } A}
Method Two: 1. Introduce the following code prerequisites to introduce a namespaceusing System.Runtime.InteropServices
1[DllImport ("user32.dll")]2 Public Static extern BOOLreleasecapture ();3 4[DllImport ("user32.dll")]5 Public Static extern BOOLSendMessage (INTPTR hwnd,intWmsg,intWParam,intlParam);6 7 Private Const intVm_nclbuttondown =0xa1;//define the left mouse button press8 Private Const intHtcaption =2;2. Increase the mouse press event to send a message, let the system mistakenly think that the title bar is pressed
1 /// <summary>2 ///mouse button down3 /// </summary>4 /// <param name= "Sender" ></param>5 /// <param name= "E" ></param>6 Private voidPanel1_mousedown (Objectsender, MouseEventArgs e)7 {8 //release mouse capture for the current application9 releasecapture ();Ten //send a message to make the system mistake for pressing the mouse on the title bar OneSendMessage ((INTPTR) This. Handle, Vm_nclbuttondown, Htcaption,0); A}Test effect
Source Code Engineering File download
C#winform No Border form Move method, imitate mouse click title bar move form Position