关于在canvas里面拖动控件时候鼠标位置的问题

dachuyin 2017-09-13 09:58:20


鼠标靠近那个按钮,变成小手可以拖动控件,然后鼠标向窗体外移动,空间在canvas里面,然后小手跑到窗体外,在窗体移动鼠标,里面那个按钮也会动,现在想做成只要在移动的时候,鼠标就是一直在按钮上,往右边移动,鼠标不能跑出去,拖动控件的时候鼠标一直固定在控件上..
有没有大神有比较好的解决方法,谢谢~~
...全文
1075 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
exception92 2017-09-14
  • 打赏
  • 举报
回复
在移动的时候,鼠标就是一直在按钮上,往右边移动,鼠标不能跑出去 -》移动的时候鼠标可以做到 但是不能控制鼠标不能跑出去,鼠标可以跑到任何位置。
exception92 2017-09-14
  • 打赏
  • 举报
回复
引用 5 楼 dachuyin 的回复:
[quote=引用 4 楼 duanzi_peng 的回复:] 在移动的时候,鼠标就是一直在按钮上,往右边移动,鼠标不能跑出去 -》移动的时候鼠标可以做到 但是不能控制鼠标不能跑出去,鼠标可以跑到任何位置。
就是能做到鼠标一直在控件上?[/quote] 是的,在鼠标移动的时候,根据鼠标位置 设置按钮的位置即可。
dachuyin 2017-09-14
  • 打赏
  • 举报
回复
引用 4 楼 duanzi_peng 的回复:
在移动的时候,鼠标就是一直在按钮上,往右边移动,鼠标不能跑出去 -》移动的时候鼠标可以做到 但是不能控制鼠标不能跑出去,鼠标可以跑到任何位置。
就是能做到鼠标一直在控件上?
dachuyin 2017-09-13
  • 打赏
  • 举报
回复
图片不知道为何貌似没传上去,这是代码: bool isDragDropInEffect = false; Point pos = new Point(); private int totalZIndex = 100; private void Element_MouseMove(object sender, MouseEventArgs e) { if (isDragDropInEffect) { FrameworkElement currEle = sender as FrameworkElement; double xPos = e.GetPosition(null).X - pos.X + currEle.Margin.Left; double yPos = e.GetPosition(null).Y - pos.Y + currEle.Margin.Top; double rPos = pos.X - e.GetPosition(null).X + currEle.Margin.Right; double bPos = pos.Y - e.GetPosition(null).Y + currEle.Margin.Bottom; currEle.CaptureMouse(); if (xPos >= 0 && rPos >= 0 && yPos >= 0 && bPos >= 0) { currEle.Margin = new Thickness(xPos, yPos, rPos, bPos); } pos = e.GetPosition(null); } } private void Element_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { FrameworkElement fEle = sender as FrameworkElement; totalZIndex++; Canvas.SetZIndex(fEle, totalZIndex); isDragDropInEffect = true; pos = e.GetPosition(null); fEle.CaptureMouse(); fEle.Cursor = Cursors.Hand; SetMargin(fEle); } private void SetMargin(FrameworkElement fEle) { //取得控件在窗口中的坐标 //Window wind = Window.GetWindow(fEle); Point point = fEle.TransformToAncestor(CanPan).Transform(new Point(0, 0)); //得到窗体的标题高度和窗口外边宽度 var verticalBorderWidth = CanPan.RenderSize.Width; var verticalBorderHeight = CanPan.RenderSize.Height; double x = point.X; double y = point.Y; double right = verticalBorderWidth - fEle.ActualWidth - x; double bottom = verticalBorderHeight - fEle.ActualHeight - y; fEle.Margin = new Thickness(x, y, right, bottom); } private void Element_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (isDragDropInEffect) { FrameworkElement ele = sender as FrameworkElement; isDragDropInEffect = false; ele.ReleaseMouseCapture(); } }
dachuyin 2017-09-13
  • 打赏
  • 举报
回复
dachuyin 2017-09-13
  • 打赏
  • 举报
回复

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧