C#winform No Border form Move method, imitate mouse click title bar move form Position

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.