滑屏切换Panel

1.重载Window消息

  1. internal override bool WndProc(ref Microsoft.WindowsCE.Forms.Message msg)
  2.         {
  3.             switch (msg.Msg)
  4.             {
  5.                 case API.WM_LBUTTONDOWN:
  6.                     if (culPanel != null)
  7.                         culPanel.OnMouseDownEx(new Point(msg.LParam.ToInt32() & 0xFFFF, (msg.LParam.ToInt32() >> 16) & 0xFFFF));
  8.                     break;
  9.                 case API.WM_LBUTTONUP:
  10.                     if (culPanel != null)
  11.                         culPanel.OnMouseUpEx(new Point(msg.LParam.ToInt32() & 0xFFFF, (msg.LParam.ToInt32() >> 16) & 0xFFFF));
  12.                     break;
  13.                 case API.WM_MOUSEMOVE:
  14.                     if (culPanel != null)
  15.                         culPanel.OnMouseMoveEx(new Point(msg.LParam.ToInt32() & 0xFFFF, (msg.LParam.ToInt32() >> 16) & 0xFFFF));
  16.                     break;
  17.             }
  18.             return base.WndProc(ref msg);
  19.         }

2. Panel中对鼠标事件处理

 

  1.         /// <summary>
  2.         /// 滑屏
  3.         /// </summary>
  4.         /// <param name="sender">滑屏的对象</param>
  5.         /// <param name="dir">滑屏的方向</param>
  6.         public delegate void OnTurch(PanelEx sender, Direction dir);
  7.         /// <summary>
  8.         /// 滑屏Event
  9.         /// </summary>
  10.         public static event OnTurch onTurch;
  11. #region 鼠标滑屏判断
  12.         private Point mouseDownPoint = Point.Empty;
  13.         public void OnMouseMoveEx(Point e)
  14.         {
  15.             if(onTurch == null)
  16.                 return;
  17.             if (mouseDownPoint != Point.Empty)
  18.             {
  19.                 if (Math.Abs(e.X - mouseDownPoint.X) > 60 && Math.Abs(e.Y - mouseDownPoint.Y) > 60)
  20.                 {
  21.                     //误差范围两个方向都超过 60者判断为无法识别
  22.                     mouseDownPoint = Point.Empty;
  23.                 }
  24.             }
  25.         }
  26.         public void OnMouseDownEx(Point e)
  27.         {
  28.             mouseDownPoint = new Point(e.X, e.Y);
  29.             //set mouse capt
  30.             //System.Diagnostics.Debug.WriteLine("down:" + e.X + "," + e.Y);
  31.         }
  32.         public void OnMouseUpEx(Point e)
  33.         {
  34.             //System.Diagnostics.Debug.WriteLine("up:" + e.X + "," + e.Y);
  35.             //System.Diagnostics.Debug.WriteLine("sub:" + Math.Abs(e.X - mouseDownPoint.X) + "," + Math.Abs(e.Y - mouseDownPoint.Y));
  36.             if (onTurch != null && mouseDownPoint != Point.Empty)
  37.             {
  38.                 if (Math.Abs(e.X - mouseDownPoint.X) < 60)
  39.                 {
  40.                     if (e.Y - mouseDownPoint.Y > 100)
  41.                     {
  42.                         onTurch(this, Direction.Down);
  43.                         mouseDownPoint = Point.Empty;
  44.                         return;
  45.                     }
  46.                     if (e.Y - mouseDownPoint.Y < -100)
  47.                     {
  48.                         onTurch(this, Direction.Up);
  49.                         mouseDownPoint = Point.Empty;
  50.                         return;
  51.                     }
  52.                 }
  53.                 if (Math.Abs(e.Y - mouseDownPoint.Y) < 60)
  54.                 {
  55.                     if (e.X - mouseDownPoint.X > 100)
  56.                     {
  57.                         onTurch(this, Direction.Right);
  58.                         mouseDownPoint = Point.Empty;
  59.                         return;
  60.                     }
  61.                     if (e.X - mouseDownPoint.X < -100)
  62.                     {
  63.                         onTurch(this, Direction.Left);
  64.                         mouseDownPoint = Point.Empty;
  65.                         return;
  66.                     }
  67.                 }
  68.             }
  69.             mouseDownPoint = Point.Empty;
  70.         }
  71.         protected void _onTurch(Direction dir)
  72.         {
  73.             if(onTurch != null)
  74.                 onTurch(this, dir);
  75.         }
  76.         #endregion

3.切换Panel

  1.         /// <summary>
  2.         /// 切换屏幕的速度
  3.         /// </summary>
  4.         private const int speedPerSecond = 500;
  5.         /// <summary>
  6.         /// 请求下一个Panel
  7.         /// </summary>
  8.         /// <param name="sender">请求者</param>
  9.         /// <param name="type">请求的类型</param>
  10.         /// <param name="command">请求的命令</param>
  11.         public delegate void OnChangeNextPanel(PanelEx sender, PanelType type,string command);
  12.         /// <summary>
  13.         /// 请求下一个Panel Event
  14.         /// </summary>
  15.         public static event OnChangeNextPanel onChangeNextPanel;
  16.         /// <summary>
  17.         /// 窗口切换
  18.         /// </summary>
  19.         /// <param name="nextPanel">切换到的下一个窗口</param>
  20.         /// <param name="dir">切换的方向</param>
  21.         public void ChangePanel(PanelEx nextPanel,Direction dir)
  22.         {
  23.             if (this.Parent == null)
  24.                 return;
  25.             int iTarget = 0;
  26.             int iCur = 0;
  27.             nextPanel.Width = this.Width;
  28.             nextPanel.Height = this.Height;
  29.             switch (dir)
  30.             {
  31.                 case Direction.Up:
  32.                     nextPanel.Top = this.Top + this.Height;
  33.                     nextPanel.Left = this.Left;
  34.                     iTarget = this.Top;
  35.                     break;
  36.                 case Direction.Down:
  37.                     nextPanel.Top = this.Top - this.Height;
  38.                     nextPanel.Left = this.Left;
  39.                     iTarget = this.Top;
  40.                     break;
  41.                 case Direction.Left:
  42.                     nextPanel.Top = this.Top;
  43.                     nextPanel.Left = this.Left + this.Width;
  44.                     iTarget = this.Left;
  45.                     break;
  46.                 case Direction.Right:
  47.                     nextPanel.Top = this.Top;
  48.                     nextPanel.Left = this.Left - this.Width;
  49.                     iTarget = this.Left;
  50.                     break;
  51.             }
  52.             iCur = iTarget + 1;
  53.             //添加NextPanel
  54.             this.Parent.Controls.Add(nextPanel);
  55.             //MessageBox.Show("Start!");
  56.             //移动
  57.             long startTick = Environment.TickCount;
  58.             long lastTick =  startTick;
  59.             do{
  60.                 long curTick = Environment.TickCount;
  61.                 if (curTick - lastTick >= 10)
  62.                 {
  63.                     //MessageBox.Show((curTick - lastTick) + " Change!");
  64.                     int spTag = (int)((curTick - lastTick) * speedPerSecond / 1000);
  65.                     switch (dir)
  66.                     {
  67.                         case Direction.Up:
  68.                             this.Top -= spTag;
  69.                             nextPanel.Top -= spTag;
  70.                             iCur = nextPanel.Top;
  71.                             if (iCur < iTarget)
  72.                             {
  73.                                 iCur = iTarget;
  74.                                 nextPanel.Top = iTarget;
  75.                             }
  76.                             break;
  77.                         case Direction.Down:
  78.                             this.Top += spTag;
  79.                             nextPanel.Top += spTag;
  80.                             iCur = nextPanel.Top;
  81.                             if (iCur > iTarget)
  82.                             {
  83.                                 iCur = iTarget;
  84.                                 nextPanel.Top = iTarget;
  85.                             }
  86.                             break;
  87.                         case Direction.Left:
  88.                             this.Left -= spTag;
  89.                             nextPanel.Left -= spTag;
  90.                             iCur = nextPanel.Left;
  91.                             if (iCur < iTarget)
  92.                             {
  93.                                 iCur = iTarget;
  94.                                 nextPanel.Left = iTarget;
  95.                             }
  96.                             break;
  97.                         case Direction.Right:
  98.                             this.Left += spTag;
  99.                             nextPanel.Left += spTag;
  100.                             iCur = nextPanel.Left;
  101.                             if (iCur > iTarget)
  102.                             {
  103.                                 iCur = iTarget;
  104.                                 nextPanel.Left = iTarget;
  105.                             }
  106.                             break;
  107.                     }
  108.                     lastTick = curTick;
  109.                     //this.Parent.Refresh();
  110.                 }
  111.                 Application2.DoEvents();
  112.             } while (iCur != iTarget);
  113.             //移除LoadPanel
  114.             if(this.Parent!=null)
  115.                 this.Parent.Controls.Remove(this);
  116.             //MessageBox.Show("Change Success!");
  117.         }
  118.         protected void _OnChangeNextPanel(PanelType type, string command)
  119.         {
  120.             if (onChangeNextPanel != null)
  121.                 onChangeNextPanel(this,type, command);
  122.         }

上面有个问题就是有时子控件有事无法获取到事件!

查了很多资料都没有找到,后来问我们老师(倪明涛老师)后知道了是 不能有两个控件同时Capture鼠标

经过测试的确是这样的 所以在ChangePanel结束后 调用API.ReleaseCapture()释放鼠标 成功

修正后代码就不贴上来了 自己在ChangePanel后添加上 API.ReleaseCapture() 就OK了!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值