欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中WinForm控件的拖动和缩放的实现代码

程序员文章站 2022-05-09 08:11:05
c# winform控件的拖动和缩放是个很有用的功能。实现起来其实很简单的,主要是设计控件的mousedown、mouseleave、mousemove事件,下面的几个步骤...

c# winform控件的拖动和缩放是个很有用的功能。实现起来其实很简单的,主要是设计控件的mousedown、mouseleave、mousemove事件,下面的几个步骤将逐步实现c# winform控件的拖动和缩放的功能。

1、定义一个枚举类型,描述光标状态  

private enum enummousepointposition  
  {  
  mousesizenone  = 0, //'无  
  mousesizeright  = 1, //'拉伸右边框  
  mousesizeleft  = 2, //'拉伸左边框  
  mousesizebottom  = 3, //'拉伸下边框  
  mousesizetop  = 4, //'拉伸上边框  
  mousesizetopleft = 5, //'拉伸左上角  
  mousesizetopright = 6, //'拉伸右上角  
  mousesizebottomleft = 7, //'拉伸左下角  
  mousesizebottomright= 8, //'拉伸右下角  
  mousedrag  = 9  // '鼠标拖动  
  }  

2、定义几个变量  

 const int band = 5;  
  const int minwidth=10;  
  const int minheight=10;  
  private enummousepointposition m_mousepointposition;  
  private point p,p1;  

3、定义自己的mymousedown事件   

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、定义自己的mymouseleave事件  

 private void mymouseleave(object sender, system.eventargs e)  
{  
 m_mousepointposition = enummousepointposition.mousesizenone;  
 this.cursor=cursors.arrow;  
}  

5、设计一个函数,确定光标在控件不同位置的样式   

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、定义自己的mymousemove事件,在这个事件里,会使用上面设计的函数    

  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; //'记录光标拖动的当前点  
    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; //'记录光标拖动的当前点  
    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; //'记录光标拖动的当前点  
    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; //'记录光标拖动的当前点  
    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; //'记录光标拖动的当前点  
    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);  //'判断光标的位置状态  
   switch (m_mousepointposition)  //'改变光标  
   {  
   case enummousepointposition.mousesizenone:  
    this.cursor = cursors.arrow;    //'箭头  
    break;  
   case enummousepointposition.mousedrag:  
    this.cursor = cursors.sizeall;   //'四方向  
    break;  
   case enummousepointposition.mousesizebottom:  
    this.cursor = cursors.sizens;    //'南北  
    break;  
   case enummousepointposition.mousesizetop:  
    this.cursor = cursors.sizens;    //'南北  
    break;  
   case enummousepointposition.mousesizeleft:  
    this.cursor = cursors.sizewe;    //'东西  
    break;  
   case enummousepointposition.mousesizeright:  
    this.cursor = cursors.sizewe;    //'东西  
    break;  
   case enummousepointposition.mousesizebottomleft:  
    this.cursor = cursors.sizenesw;   //'东北到南西  
    break;  
   case enummousepointposition.mousesizebottomright:  
    this.cursor = cursors.sizenwse;   //'东南到西北  
    break;  
   case enummousepointposition.mousesizetopleft:  
    this.cursor = cursors.sizenwse;   //'东南到西北  
    break;  
   case enummousepointposition.mousesizetopright:  
    this.cursor = cursors.sizenesw;   //'东北到南西  
    break;  
   default:  
    break;  
   }  
  }  
  
  }  

7、制作一个初始化过程,将界面panel1上的所有控件都绑定mymousedown、mymouseleave、mymousemove事件,记得在form初始化或者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,在运行之前你在panel1上放几个控件,执行程序,应该可以随便移动、改变其大小了 

c# winform控件的拖动和缩放的实现就此完成。 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。