Code Sharing for drag and zoom implementations of WinForm controls in C #

Source: Internet
Author: User
This article mainly introduces the implementation code of drag and scale of WinForm control in C #, and the drag and zoom of C # WinForm control is a useful function, which is interesting to understand.

The drag and scale of the C # WinForm control is a useful feature. Implementation is actually very simple, mainly the design of the control MouseDown, MouseLeave, MouseMove events, the following steps will gradually implement the C # WinForm control of the drag and zoom function.

1. Define an enumeration type that describes the cursor state

Private enum enummousepointposition    {    Mousesizenone  = 0,//' no    mousesizeright  = 1,//' Extrude right    border Mousesizeleft  = 2,//' Extrude left border    mousesizebottom  = 3,//' Extrude bottom frame    mousesizetop  = 4,//' Extrude top Border    Mousesizetopleft = 5,//' Extrude upper left corner    mousesizetopright = 6,//' Extrude upper right corner    Mousesizebottomleft = 7,//' extrude lower left corner    Mousesizebottomright= 8,//' stretched lower right corner    mousedrag  = 9  //' Mouse drag    }

2. Define several variables

const int Band = 5;    const int minwidth=10;    const int minheight=10;    Private Enummousepointposition m_mousepointposition;    Private point P,p1;

3. Define your own Mymousedown events

private void Mymousedown (Object Sender,system.windows.forms.mouseeventargs e)  {   p.x=e.x;   P.y=e.y;   P1. x=e.x;   P1. y=e.y;    }

4. Define your own Mymouseleave events

private void Mymouseleave (object sender, System.EventArgs e)  {   m_mousepointposition = Enummousepointposition.mousesizenone;   This. Cursor=cursors.arrow;  }

5. Design a function to determine the style of the cursor at different positions in the control

Private Enummousepointposition mousepointposition (Size Size,system.windows.forms.mouseeventargs e) {if (E.X >=- 1 * Band) | (e.x <= size.) Width) | (E.y >=-1 * Band) | (e.y <= size.)    Height) {if (E.x < Band) {if (E.y < Band) {return enummousepointposition.mousesizetopleft;} else {if (E.y >-1 * Band + size.    Height) {return enummousepointposition.mousesizebottomleft;}    else {return enummousepointposition.mousesizeleft;} }} else {if (e.x >-1 * Band + size.    Width) {if (E.y < Band) {return enummousepointposition.mousesizetopright;} else {if (E.y >-1 * Band + size.     Height) {return enummousepointposition.mousesizebottomright;}    else {return enummousepointposition.mousesizeright;}    }} else {if (E.y < Band) {return enummousepointposition.mousesizetop;} else {if (E.y >-1 * Band + size. Height) {return enummousepointposition.mousesizebottom;} else {return enummousepointposition.mousedrag;}  }}}} else {return enummousepointposition.mousesizenone;} }

6. Define your own Mymousemove event, in which the function designed above will be used

  private void Mymousemove (Object Sender,system.windows.forms.mouseeventargs e) {Control Lctrl = (sender as Control)      ;         if (e.button==mousebuttons.left) {switch (m_mousepointposition) {case Enummousepointposition.mousedrag:      Lctrl.left =lctrl.left+ e.x-p.x;      Lctrl.top =lctrl.top+ e.y-p.y;     Break Case EnumMousePointPosition.MouseSizeBottom:lCtrl.Height = Lctrl.height + e.y-p1.      Y P1.      x=e.x; P1. Y=e.y;     ' Record cursor to drag the current point to break; Case EnumMousePointPosition.MouseSizeBottomRight:lCtrl.Width = Lctrl.width + e.x-p1.      X Lctrl.height = Lctrl.height + e.y-p1.      Y P1.      x=e.x; P1. Y=e.y;     ' Record cursor to drag the current point to break; Case EnumMousePointPosition.MouseSizeRight:lCtrl.Width = Lctrl.width + e.x-p1.     X Lctrl.height = Lctrl.height + e.y-p1.      Y P1.      x=e.x; P1. Y=e.y;     ' Record cursor to drag the current point to break;      Case EnumMousePointPosition.MouseSizeTop:lCtrl.Top = lctrl.top + (E.Y-P.Y); Lctrl.Height = Lctrl.height-(E.Y-P.Y);     Break      Case EnumMousePointPosition.MouseSizeLeft:lCtrl.Left = Lctrl.left + e.x-p.x;      Lctrl.width = Lctrl.width-(e.x-p.x);     Break      Case EnumMousePointPosition.MouseSizeBottomLeft:lCtrl.Left = Lctrl.left + e.x-p.x;      Lctrl.width = Lctrl.width-(e.x-p.x); Lctrl.height = lctrl.height+ E.y-p1.      Y P1.      x=e.x; P1. Y=e.y;     ' Record cursor to drag the current point to break;      Case EnumMousePointPosition.MouseSizeTopRight:lCtrl.Top = lctrl.top + (E.Y-P.Y); Lctrl.width = Lctrl.width + (e.x-p1.      X);      Lctrl.height = Lctrl.height-(E.Y-P.Y); P1.      x=e.x; P1. Y=e.y;     ' Record cursor to drag the current point to break;      Case EnumMousePointPosition.MouseSizeTopLeft:lCtrl.Left = Lctrl.left + e.x-p.x;      Lctrl.top = Lctrl.top + (E.Y-P.Y);      Lctrl.width = Lctrl.width-(e.x-p.x);      Lctrl.height = Lctrl.height-(E.Y-P.Y);     Break     Default:break; } if (Lctrl.width<minwidth) lctrL.width=minwidth;         if (lctrl.height<minheight) lctrl.height=minheight;  } else {m_mousepointposition = Mousepointposition (Lctrl.size, E); ' Determine the cursor position status switch (m_mousepointposition)//' Change cursor {case EnumMousePointPosition.MouseSizeNone:this.    Cursor = Cursors.arrow;     ' Arrow break; Case EnumMousePointPosition.MouseDrag:this.   Cursor = Cursors.sizeall;     ' Quartet to break; Case EnumMousePointPosition.MouseSizeBottom:this.    Cursor = Cursors.sizens;     ' North-South break; Case EnumMousePointPosition.MouseSizeTop:this.    Cursor = Cursors.sizens;     ' North-South break; Case EnumMousePointPosition.MouseSizeLeft:this.    Cursor = Cursors.sizewe;     ' Something break; Case EnumMousePointPosition.MouseSizeRight:this.    Cursor = Cursors.sizewe;     ' Something break; Case EnumMousePointPosition.MouseSizeBottomLeft:this.   Cursor = CURSORS.SIZENESW;     ' Northeast to south West break; Case Enummousepointposition.mousesizebottoMright:this.   Cursor = Cursors.sizenwse;     ' Southeast to Northwest break; Case EnumMousePointPosition.MouseSizeTopLeft:this.   Cursor = Cursors.sizenwse;     ' Southeast to Northwest break; Case EnumMousePointPosition.MouseSizeTopRight:this.   Cursor = CURSORS.SIZENESW;     ' Northeast to south West break;     Default:break; }    }      }

7. Make an initialization process, bind all the controls on the interface Panel1 to Mymousedown, Mymouseleave, Mymousemove events, and remember to execute it first when the form is initialized or Form_Load.

private void Initproperty ()  {for   (int i = 0; i < This.panel1.Controls.Count; i++)    {    This.panel1.controls[i]. mousedown+= new System.Windows.Forms.MouseEventHandler (Mymousedown);   This.panel1.controls[i]. mouseleave+= new System.EventHandler (mymouseleave);   This.panel1.controls[i]. MouseMove + = new System.Windows.Forms.MouseEventHandler (mymousemove);   }    }

8, OK, before running you put a few controls on the Panel1, execute the program, should be able to casually move, change its size

The implementation of drag and zoom for the C # WinForm control is complete.

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.