c# - 全局设置最大鼠标位置,而不仅仅是在 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);
}
解决方案
尝试使用 ClipCursor,将矩形作为参数并将鼠标限制在矩形中:
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-clipcursor
推荐阅读
- vue.js - Vue 3 组合 API,如何在 setup() 函数中获取上下文父属性?
- powershell - 使用 powershell 在现有 CSV 中添加列 CSV
- laravel - 如何根据类星体中的响应值更新文本?
- sql - 查询地点和分组方式
- java - 数组列表
不会添加超过一项 - c++ - strncpy 后 char 数组的错误行为
- php - 如何在 php 中运行 powershell 命令列表?
- roslyn - 如何使用 Roslyn 将函数插入到 VB.NET 中的类中
- visual-studio-code - 禁用无标题编辑器改进 - 标题中的第一行名称
- java - 如何打印字符串的每三个字符?爪哇