首页 > 解决方案 > 全局设置最大鼠标位置,而不仅仅是在 wpf 窗口上

问题描述

我正在制作一个可以在其他应用程序上打开的菜单。菜单非常简单。它看起来像这样:

在此处输入图像描述

最终它看起来像英雄联盟中的 ping:

在此处输入图像描述

带有 4 个按钮的窗口出现在当前鼠标位置,只要按下绑定的按钮就可见。通过在适当的按钮上移动鼠标,我们进行选择。

我希望鼠标在按住按钮时不要离开按钮。我尝试了 MouseMove 和 MouseLeave 事件。但是,当您更快地移动鼠标时,不幸的是它设法离开了按钮区域。我正在寻找一些最佳解决方案,或者例如降低鼠标的速度,这使得离开区域变得困难,如果您抓住MouseLeave 事件,鼠标返回中间。

但是有什么方法可以限制 X 和 Y 的最大值。

我想补充一点,该应用程序可以在玩游戏时使用,因此鼠标的这种瞬移可以被识别为作弊。

窗户是透明的。

WindowStyle="None" KeyUp="Window_KeyUp"
<Window.Background>
    <SolidColorBrush Opacity="0" Color="White"/>
</Window.Background>

我当前的代码:

    int lastX = 140;
    int lastY = 140;
    private void Window_MouseMove(object sender, MouseEventArgs e)
    {
        var a = e.GetPosition(MiniMenuWindow);
        int newX = (int)a.X;
        int newY = (int)a.Y;
        if (a.X < 70)
            newX = 70;
        if (a.X > 210)
            newX = 210;
        if (a.Y < 70)
            newY = 70;
        if (a.Y > 210)
            newY = 210;
        if(newX<140)
            lastX = 100;
        else
            lastX = 180;
        if (newY < 140)
            lastY = 100;
        else
            lastY = 180;

        if (newX != (int)a.X || newY != (int)a.Y)
            NativeMethods.SetCursorPos((int)MiniMenuWindow.Left + newX, (int)MiniMenuWindow.Top + newY);
        
    }

    private void MiniMenuWindow_MouseLeave(object sender, MouseEventArgs e)
    {
        NativeMethods.SetCursorPos((int)MiniMenuWindow.Left + lastX, (int)MiniMenuWindow.Top + lastY);
    }

标签: c#wpf

解决方案


尝试使用 ClipCursor,将矩形作为参数并将鼠标限制在矩形中:

https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-clipcursor


推荐阅读