Drag any of the controls in the. NET WinForm form with the mouse __.net

Source: Internet
Author: User
In the WinForm form, drag through the mouse to change the position of the control. During the drag process, follow the mouse to display a black box that is the size of the dragged control to simulate the drag effect. The following figure: This is the source code. A button control is dragged here. If you want, you can also change the cursor as you drag. using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.text; using system.windows.forms; Namespace windowsapplication1 {    public partial class formdrag :  form     {        //The controls that will be dragged          private Control control;         public formdrag ()          {            initializecomponent ();             this. Paint += new system.windows.forms.painteventhandler (this. Formdrag_paint);              control = new button ();             control. Mousedown += new mouseeventhandler (Control_mousedown);             control. Mousemove += new mouseeventhandler (Control_mousemove);             control. Mouseup += new mouseeventhandler (Control_mouseup);             this. Controls.Add (Control);                      }                  //mouse down coordinates (relative coordinates of control controls)         Point  mousedownpoint = point.empty;          //the rectangle that displays the drag effect         rectangle rect =  Rectangle.Empty;         //is dragging         bool  isDrag = false;         void control_mousedown (object sender,  Mouseeventargs e)         {             if  (E.button == mousebuttons.left)              {                 mouseDownPoint = e.Location;                 //the size of the record control                 rect =  cOntrol. Bounds;             }         &NBSP}         void control_mousemove (Object sender,  mouseeventargs e)         {             if  (E.button == mousebuttons.left)              {                 isDrag = true;                 //Reset the rect position, Follow the mouse to move the                 rect. Location = getpointtoform (New point) (e.location.x - mousedownpoint.x, e. LOCATION.Y - MOUSEDOWNPOINT.Y));  &nBsp;              this. Refresh ();                               }         }         void control_mouseup (object sender,  Mouseeventargs e)         {             if  (E.button == mousebuttons.left)              {                 if  (Isdrag)                  {                     isDrag = false;                      //move control to the place where the mouse is released                      control. Location = rect. Location;                      this. Refresh ();                 }                  reset ();             }         &NBSP}         //Reset Variable          private void reset ()         {             mousedownpoint = point.empty ;             rect = Rectangle.Empty;             isDrag = false;        &NBSP}         //form Redraw          private void formdrag_paint (object sender,  Painteventargs e)         {             if  (Rect != rectangle.empty)              {                 if  (Isdrag)                 &nbsp {//Draw a black box of the same size as control                      e.graphics.drawrectangle (Pens.black, rect);                 }                  else                  {                     e. Graphics.drawrectangle (this. New pen BackColor),  rect);                 }              }         }         //converts the coordinates of control controls to relative to the form's coordinates.         private point getpointtoform (point p)          {            return  This. PointToClient (Control. Pointtoscreen (p));         }              &NBSP}}

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.