1.重载Window消息
- internal override bool WndProc(ref Microsoft.WindowsCE.Forms.Message msg)
- {
- switch (msg.Msg)
- {
- case API.WM_LBUTTONDOWN:
- if (culPanel != null)
- culPanel.OnMouseDownEx(new Point(msg.LParam.ToInt32() & 0xFFFF, (msg.LParam.ToInt32() >> 16) & 0xFFFF));
- break;
- case API.WM_LBUTTONUP:
- if (culPanel != null)
- culPanel.OnMouseUpEx(new Point(msg.LParam.ToInt32() & 0xFFFF, (msg.LParam.ToInt32() >> 16) & 0xFFFF));
- break;
- case API.WM_MOUSEMOVE:
- if (culPanel != null)
- culPanel.OnMouseMoveEx(new Point(msg.LParam.ToInt32() & 0xFFFF, (msg.LParam.ToInt32() >> 16) & 0xFFFF));
- break;
- }
- return base.WndProc(ref msg);
- }
2. Panel中对鼠标事件处理
- /// <summary>
- /// 滑屏
- /// </summary>
- /// <param name="sender">滑屏的对象</param>
- /// <param name="dir">滑屏的方向</param>
- public delegate void OnTurch(PanelEx sender, Direction dir);
- /// <summary>
- /// 滑屏Event
- /// </summary>
- public static event OnTurch onTurch;
- #region 鼠标滑屏判断
- private Point mouseDownPoint = Point.Empty;
- public void OnMouseMoveEx(Point e)
- {
- if(onTurch == null)
- return;
- if (mouseDownPoint != Point.Empty)
- {
- if (Math.Abs(e.X - mouseDownPoint.X) > 60 && Math.Abs(e.Y - mouseDownPoint.Y) > 60)
- {
- //误差范围两个方向都超过 60者判断为无法识别
- mouseDownPoint = Point.Empty;
- }
- }
- }
- public void OnMouseDownEx(Point e)
- {
- mouseDownPoint = new Point(e.X, e.Y);
- //set mouse capt
- //System.Diagnostics.Debug.WriteLine("down:" + e.X + "," + e.Y);
- }
- public void OnMouseUpEx(Point e)
- {
- //System.Diagnostics.Debug.WriteLine("up:" + e.X + "," + e.Y);
- //System.Diagnostics.Debug.WriteLine("sub:" + Math.Abs(e.X - mouseDownPoint.X) + "," + Math.Abs(e.Y - mouseDownPoint.Y));
- if (onTurch != null && mouseDownPoint != Point.Empty)
- {
- if (Math.Abs(e.X - mouseDownPoint.X) < 60)
- {
- if (e.Y - mouseDownPoint.Y > 100)
- {
- onTurch(this, Direction.Down);
- mouseDownPoint = Point.Empty;
- return;
- }
- if (e.Y - mouseDownPoint.Y < -100)
- {
- onTurch(this, Direction.Up);
- mouseDownPoint = Point.Empty;
- return;
- }
- }
- if (Math.Abs(e.Y - mouseDownPoint.Y) < 60)
- {
- if (e.X - mouseDownPoint.X > 100)
- {
- onTurch(this, Direction.Right);
- mouseDownPoint = Point.Empty;
- return;
- }
- if (e.X - mouseDownPoint.X < -100)
- {
- onTurch(this, Direction.Left);
- mouseDownPoint = Point.Empty;
- return;
- }
- }
- }
- mouseDownPoint = Point.Empty;
- }
- protected void _onTurch(Direction dir)
- {
- if(onTurch != null)
- onTurch(this, dir);
- }
- #endregion
3.切换Panel
- /// <summary>
- /// 切换屏幕的速度
- /// </summary>
- private const int speedPerSecond = 500;
- /// <summary>
- /// 请求下一个Panel
- /// </summary>
- /// <param name="sender">请求者</param>
- /// <param name="type">请求的类型</param>
- /// <param name="command">请求的命令</param>
- public delegate void OnChangeNextPanel(PanelEx sender, PanelType type,string command);
- /// <summary>
- /// 请求下一个Panel Event
- /// </summary>
- public static event OnChangeNextPanel onChangeNextPanel;
- /// <summary>
- /// 窗口切换
- /// </summary>
- /// <param name="nextPanel">切换到的下一个窗口</param>
- /// <param name="dir">切换的方向</param>
- public void ChangePanel(PanelEx nextPanel,Direction dir)
- {
- if (this.Parent == null)
- return;
- int iTarget = 0;
- int iCur = 0;
- nextPanel.Width = this.Width;
- nextPanel.Height = this.Height;
- switch (dir)
- {
- case Direction.Up:
- nextPanel.Top = this.Top + this.Height;
- nextPanel.Left = this.Left;
- iTarget = this.Top;
- break;
- case Direction.Down:
- nextPanel.Top = this.Top - this.Height;
- nextPanel.Left = this.Left;
- iTarget = this.Top;
- break;
- case Direction.Left:
- nextPanel.Top = this.Top;
- nextPanel.Left = this.Left + this.Width;
- iTarget = this.Left;
- break;
- case Direction.Right:
- nextPanel.Top = this.Top;
- nextPanel.Left = this.Left - this.Width;
- iTarget = this.Left;
- break;
- }
- iCur = iTarget + 1;
- //添加NextPanel
- this.Parent.Controls.Add(nextPanel);
- //MessageBox.Show("Start!");
- //移动
- long startTick = Environment.TickCount;
- long lastTick = startTick;
- do{
- long curTick = Environment.TickCount;
- if (curTick - lastTick >= 10)
- {
- //MessageBox.Show((curTick - lastTick) + " Change!");
- int spTag = (int)((curTick - lastTick) * speedPerSecond / 1000);
- switch (dir)
- {
- case Direction.Up:
- this.Top -= spTag;
- nextPanel.Top -= spTag;
- iCur = nextPanel.Top;
- if (iCur < iTarget)
- {
- iCur = iTarget;
- nextPanel.Top = iTarget;
- }
- break;
- case Direction.Down:
- this.Top += spTag;
- nextPanel.Top += spTag;
- iCur = nextPanel.Top;
- if (iCur > iTarget)
- {
- iCur = iTarget;
- nextPanel.Top = iTarget;
- }
- break;
- case Direction.Left:
- this.Left -= spTag;
- nextPanel.Left -= spTag;
- iCur = nextPanel.Left;
- if (iCur < iTarget)
- {
- iCur = iTarget;
- nextPanel.Left = iTarget;
- }
- break;
- case Direction.Right:
- this.Left += spTag;
- nextPanel.Left += spTag;
- iCur = nextPanel.Left;
- if (iCur > iTarget)
- {
- iCur = iTarget;
- nextPanel.Left = iTarget;
- }
- break;
- }
- lastTick = curTick;
- //this.Parent.Refresh();
- }
- Application2.DoEvents();
- } while (iCur != iTarget);
- //移除LoadPanel
- if(this.Parent!=null)
- this.Parent.Controls.Remove(this);
- //MessageBox.Show("Change Success!");
- }
- protected void _OnChangeNextPanel(PanelType type, string command)
- {
- if (onChangeNextPanel != null)
- onChangeNextPanel(this,type, command);
- }
上面有个问题就是有时子控件有事无法获取到事件!
查了很多资料都没有找到,后来问我们老师(倪明涛老师)后知道了是 不能有两个控件同时Capture鼠标
经过测试的确是这样的 所以在ChangePanel结束后 调用API.ReleaseCapture()释放鼠标 成功
修正后代码就不贴上来了 自己在ChangePanel后添加上 API.ReleaseCapture() 就OK了!