Implementation code of drag and scaling of WinForm control in C

  • 2021-11-30 01:14:37
  • OfStack

The dragging and scaling of the C # WinForm control is a useful feature. In fact, it is very simple to implement, mainly designing MouseDown, MouseLeave and MouseMove events of the control. The following steps will gradually realize the dragging and scaling functions of C # WinForm controls.

1. Define an enumeration type to describe the cursor state


private enum EnumMousePointPosition  
  {  
  MouseSizeNone  = 0, //' None   
  MouseSizeRight  = 1, //' Stretch the right border   
  MouseSizeLeft  = 2, //' Stretch the left border   
  MouseSizeBottom  = 3, //' Stretch the lower border   
  MouseSizeTop  = 4, //' Stretch the top border   
  MouseSizeTopLeft = 5, //' Stretch the upper left corner   
  MouseSizeTopRight = 6, //' Stretch the upper right corner   
  MouseSizeBottomLeft = 7, //' Stretch the lower left corner   
  MouseSizeBottomRight= 8, //' Stretch the 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 event


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 event


 private void MyMouseLeave(object sender, System.EventArgs e)  
{  
 m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;  
 this.Cursor=Cursors.Arrow;  
}  

5. Design a function to determine the cursor style in different positions of 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 this event, you will use the functions designed above


  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 the current point of cursor drag   
    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 the current point of cursor drag   
    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 the current point of cursor drag   
    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 the current point of cursor drag   
    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 the current point of cursor drag   
    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);  //' Judge the position state of the cursor   
   switch (m_MousePointPosition)  //' Change the cursor   
   {  
   case EnumMousePointPosition.MouseSizeNone:  
    this.Cursor = Cursors.Arrow;    //' Arrow   
    break;  
   case EnumMousePointPosition.MouseDrag:  
    this.Cursor = Cursors.SizeAll;   //'4 Direction   
    break;  
   case EnumMousePointPosition.MouseSizeBottom:  
    this.Cursor = Cursors.SizeNS;    //' North and South   
    break;  
   case EnumMousePointPosition.MouseSizeTop:  
    this.Cursor = Cursors.SizeNS;    //' North and 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, and bind all controls on the interface panel1 to MyMouseDown, MyMouseLeave and MyMouseMove events. Remember to execute it first when 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, you put a few controls on panel1 before running, and execute the program. You should be able to move and change its size at will

The implementation of the drag and scaling of the C # WinForm control is completed.


Related articles: