C # enables a form to move with the mouse

Source: Internet
Author: User

/// <summary>    ///enables a specified control in a window to support run-time movement///TODO: Run-time scaling/// </summary>     Public classControlmoveresize {#regionPrivate membersBOOLIsmoving =false; Point Pctrllastcoordinate=NewPoint (0,0); Point Pcursoroffset=NewPoint (0,0); Point Pcursorlastcoordinate=NewPoint (0,0); PrivateControl CTRL =NULL; PrivateScrollableControl Containe =NULL; #endregion        #regionPrivate methods/// <summary>        ///record the current position of the mouse and the current position of the moved component in the left mouse button pressed state/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidMouseDown (Objectsender, MouseEventArgs e) {            if(Containe = =NULL)            {                return; }            if(E.button = =mousebuttons.left) {ismoving=true; Pctrllastcoordinate.x=Ctrl.                Left; Pctrllastcoordinate.y=Ctrl.                Top; Pcursorlastcoordinate.x=cursor.position.x; Pcursorlastcoordinate.y=CURSOR.POSITION.Y; }        }        Private voidMouseMove (Objectsender, MouseEventArgs e) {            if(Containe = =NULL)            {                return; }            if(E.button = =mousebuttons.left) {if( This. ismoving) {Point pcursor=NewPoint (cursor.position.x, CURSOR.POSITION.Y); Pcursoroffset.x= Pcursor.x-pcursorlastcoordinate.x; Pcursoroffset.y= Pcursor.y-pcursorlastcoordinate.y; Ctrl. Left= pctrllastcoordinate.x +Pcursoroffset.x; Ctrl. Top= Pctrllastcoordinate.y +Pcursoroffset.y; }            }        }        Private voidMouseUp (Objectsender, MouseEventArgs e) {            if(Containe = =NULL)            {                return; }            if( This. ismoving) {if(Pcursoroffset.x = =0&& Pcursoroffset.y = =0)                {                    return; }                if((pctrllastcoordinate.x + pcursoroffset.x + CTRL.) Width) >0) {Ctrl. Left= pctrllastcoordinate.x +Pcursoroffset.x; }                Else{Ctrl. Left=0; }                if((pctrllastcoordinate.y + Pcursoroffset.y + CTRL.) Height) >0) {Ctrl. Top= Pctrllastcoordinate.y +Pcursoroffset.y; }                Else{Ctrl. Top=0; } pcursoroffset.x=0; Pcursoroffset.y=0; }        }        #endregion        #regionconstructor function/// <summary>        ///gets the moved control object and container object/// </summary>        /// <param name= "C" >controls that are set to be moved when run</param>        /// <param name= "Parentcontain" >containers for removable controls</param>         Publiccontrolmoveresize (Control C, ScrollableControl parentcontain) {Ctrl=C;  This. Containe =Parentcontain; Ctrl. MouseDown+=NewMouseEventHandler (MouseDown); Ctrl. MouseMove+=NewMouseEventHandler (MouseMove); Ctrl. MouseUp+=NewMouseEventHandler (MouseUp); }        #endregion    }

The above is the class, and the following method is called

The parameters passed in are: control, control container (i.e. form)

New Controlmoveresize (this. Button1,this);

C # enables a form to move with the mouse

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.