WinForm to drag the borderless form solution

Source: Internet
Author: User

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

Related Article

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.