当前位置:编程学习 > C#/ASP.NET >>

将拖拽进行到底

答案:     问题描述:
    想在.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();
 

上一个:asp+版本简单的留言板的制作(一)(二)(三)
下一个:4 种进程或线程同步互斥的控制方法

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,