首页 > 开发 > 综合 > 正文

C#中势将窗体拖拽进行到底

2024-07-21 02:29:42
字体:
来源:转载
供稿:网友

  问题描述: 

  想在.net下实现对一些非规则窗体,没有caption,formborderstyle = system.windows.forms.formborderstyle.none;窗体的拖拽,最小化,最大化,特殊操作的实现(如图1所示)。在黄色的区域即区域1里实现对窗体的拖拽操作,在橙色区域即区域2里实现对窗体的最小化操作,在蓝色区域即区域3里实现对窗体的关闭操作,在绿色区域即区域4里实现对窗体的特殊操作(如双倍窗体)。
  

(图1) 

  问题实现:

  第一种方法:添加label为label添加click事件。(如图2所示) 如果要用这种方法实现,就要为每一个颜色区域进行切图,并要保证有正确的切图长和宽,然后设置label的背景为这个图片。

                    (图2) 

   处理他们的click事件,拖拽处理mousedown mouseup事件。 

  第二种方法:不添加label只处理鼠标事件,判断鼠标的位置然后决定执行什么操作,这种方法很耗费资源,每次鼠标点击就要判断,鼠标是否在某个区域然后决定是否要处理。不过这个处理用多态包装了。程序看起来比较整齐。

 //定义常量
   private point point;
   private const int dragmove=172;
   private const int dragmin=72;
   private const int dragclose=72;
   private const int dragdouble=78;
   private const int dragheight=29;
   private mousehandleenum dragenum;
  
  //定义mousedown事件
  private void dragmain_mousedown(object sender, system.windows.forms.mouseeventargs e)
   {
   point.x=e.x;
   point.y=e.y;
   if(e.y<dragheight)
   {
   if(e.x<dragmove)
   {
   dragenum = mousehandleenum.move;
   return;
   }
   if(e.x<dragmove+dragmin)
   {
   dragenum = mousehandleenum.min;
   return;
   }
   if(e.x<dragmove+dragmin+dragclose)
   {
   dragenum = mousehandleenum.close;
   return;
   }
   if(e.x<dragmove+dragmin+dragclose+dragdouble)
   {
   dragenum = mousehandleenum.double;
   return;
   }
   }
  
   }
  
  
  //定义mouseup事件
  private void dragmain_mouseup(object sender, system.windows.forms.mouseeventargs e)
   {
   point.x=e.x-point.x;
   point.y=e.y-point.y;
   idragmouse idragmouse;
   switch(dragenum)
   {
   case mousehandleenum.move :
   idragmouse = new mousemove(point,this);
   break;
   case mousehandleenum.min :
   idragmouse = new mousemin(point,this);
   break;
   case mousehandleenum.close :
   idragmouse = new mouseclose(point,this);
   break;
   case mousehandleenum.double :
   idragmouse = new mousedouble(point,this);
   break;
   default:
   idragmouse = null;
   break;
   }
   if(idragmouse!=null)
   idragmouse.mousedo();
   }
   }
 
 //定义基类
  namespace dragmouse
  {
  public enum mousehandleenum
  {
   none=0,
   move=1,
   min=2,
   close=3,
   double=4,
  }
  public class dragmousebase
   {
   protected point point;
   public form form;
   public dragmousebase(point point, form form)
   {
   this.point = point;
   this.form = form;
   }
   }
  }
 

 //定义接口
  namespace dragmouse
  {
   /// <summary>
   ///
   /// </summary>
   public interface idragmouse
   {
   void mousedo();
   }
  }
 
 //拖拽操作
  namespace dragmouse
  {
   /// <summary>
   ///
   /// </summary>
   public class mouseclose : dragmousebase,idragmouse
   {
   public mouseclose(point point,form form):base(point,form)
   {
   //
   // todo: add constructor logic here
   //
   }
   #region idragmouse members
  
   public void mousedo()
   {
   application.exit();
   // todo: add mouseclose.mousedo implementation
   }
  
   #endregion
   }
  }
 
 //其他操作类似。 

  第三种方法:是用责任链这个设计模式来包装鼠标的点击操作,把操作分配到各个责任链的节点上,是程序更加面向对象,有更好的扩展性。

  //两个鼠标事件
  private void dragmain_mousedown(object sender, system.windows.forms.mouseeventargs e)
   {
   request.getinformation(e.x,e.y);
  
   }
  
  
  private void dragmain_mouseup(object sender, system.windows.forms.mouseeventargs e)
   {
   request.setscreenpoint(e.x,e.y);
  
   }
  
  //封装的请求类
   public class request
   {
   public int iscreenx;
   public int iscreeny;
  
   public int ex;
   public int ey;
  
   public readonly int yhigh;
   public readonly int draglength;
   public readonly int minlength;
   public readonly int closelength;
   public readonly int doublelength;
  
   private draghandler draghandler;
   private minhandler minhandler;
   private closehandler closehandler;
   private doublehandler doublehandler;
  
   public form parentform;
  
   public void setscreenpoint(int ix,int iy)
   {
   iscreenx = ix;
   iscreeny = iy;
   draghandler.handlerequest(this);
   }
  
   public void getinformation(int ex,int ey)
   {
   ex=ex;
   ey=ey;
   }
  
   public request(int yhigh,int draglength,form form)
   {
   yhigh = yhigh;
   draglength = draglength;
   parentform = form;
   draghandler = new draghandler();
   minhandler =new minhandler();
   closehandler = new closehandler();
   doublehandler = new doublehandler();
  
   draghandler.setsuccessor(minhandler);
   minhandler.setsuccessor(closehandler);
   closehandler.setsuccessor(doublehandler);
   }
  
  
  public request(int yhigh,int draglength,int minlength,form form):this(yhigh,draglength,form)
   {
   minlength = minlength;
   }
  
  
  public request(int yhigh,int draglength,int minlength,int closelength,form form):this(yhigh,draglength,minlength,form)
   {
   closelength = closelength;
   }
  
  
  public request(int yhigh,int draglength,int minlength,int closelength, int doublelength , form form):this(yhigh,draglength,minlength,closelength,form)
   {
   doublelength = doublelength;
   }
  
   }
 
 //拖拽操作
   public class draghandler : handler
   {
   override public void handlerequest(request request)
   {
   // determine if we can handle the request
   if ((request.ey<request.yhigh)&&(request.ex<request.draglength)) // some complex decision making!
   {
   request.parentform.left += request.iscreenx-request.ex;
   request.parentform.top += request.iscreeny-request.ey;
  
   // request handling code goes here
   }
   else
   {
   // not handled here - pass on to next in the chain
   if (successorhandler != null)
   successorhandler.handlerequest(request);
   }
   }
   } 
 
 //其他操作类似 

  第四种方法:(只是有想法还没有找到成功的实现办法) 

  在mfc中可以用postmessage或者sendmessag发消息,当鼠标单击,但不在窗体的captiontitle上时,发一个消息告诉系统鼠标在captiontitle(每个窗口自己titlebar)上,这样窗口的拖拽就可以由系统托管了。现在实现了在窗口中任意位置单击鼠标拖拽窗体。但是没有实现上面要求的那些多样化操作。

   if(point.y<this->m_height)
   {
  //发消息给系统伪装鼠标在caption bar 上。
   if(point.x<this->m_drag)
   {
   postmessage(wm_nclbuttondown,
   htcaption,
   makelparam(point.x,point.y));
   return;
   }
   if(point.x<this->m_drag+this->m_min&&point.x>this->m_drag)
   {
   postmessage(wm_nclbuttondown,
   htminbutton,
   makelparam(point.x,point.y));
   return;
   }
   if(point.x<this->m_drag+this->m_min+this->m_close&&point.x>this->m_drag+this->m_min)
   {
   postmessage(wm_nclbuttondown,
   htclose,
   makelparam(point.x,point.y));
   return;
   }
   if(point.x<this->m_drag+this->m_min+this->m_close+this->m_double&&point.x>this->m_drag+this->m_min+this->m_close)
   {
   crgn *rgn = new crgn();
   crect *rect =new crect();
   this->getwindowrect(*rect);
   this->setwindowrgn(*rgn,true);
   return;
   }
  
   }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表