buaa21 / summer2020

软件学院《程序设计实践》2020小学期仓库
40 stars 8 forks source link

WPF (c#) 关于点击按钮不放时获取鼠标的全局位置 #194

Closed Ocean326 closed 3 years ago

Ocean326 commented 3 years ago
            Point point0 = base.PointToScreen(e.GetPosition(this));
            double dx = point0.X;
            double dx0 = dx;

            while ((Mouse.LeftButton == MouseButtonState.Pressed))
            {
                  if (dx - dx0 < 0 && Sticker.Opacity >= 0.3)
                {//向左移动
                }
                else if (dx - dx0 > 0 )
                {//向右移动

                }
                dx0 = dx;
                Point point1 = PointToScreen(Mouse.GetPosition(this));
                dx = point1.X;
                Delay(50);
            }
     问题是这样鼠标坐标只有在窗体内的,当鼠标移除窗体外时获得的坐标就会停留在鼠标移出窗体时的位置上
Ocean326 commented 3 years ago

以及一个bug就是触发条件我是写在PreviewMouseDown 里的 不知为何在鼠标持续摁下时移动经过窗体边缘时(如果移动的慢,触发了窗体的缩放边缘,鼠标变成拖动窗体边缘的图标),Mouse.LeftButton ==MouseButtonState.Pressed这个判定条件就会失效 从而退出

crclz commented 3 years ago

WpfMouse.zip 看看这个demo

Ocean326 commented 3 years ago

感谢!!已解决