Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 在一个监视器上捕获鼠标_.net_Mouse_Multiple Monitors - Fatal编程技术网

.net 在一个监视器上捕获鼠标

.net 在一个监视器上捕获鼠标,.net,mouse,multiple-monitors,.net,Mouse,Multiple Monitors,我有一个Windows系统,多个显示器作为扩展桌面连接。只有主监视器在物理上对用户可见,因此我想在该监视器上捕获鼠标 似乎有一个使用ClipMouse API函数的简单解决方案,如中所述: 但是,当使用alt tab更改程序或触摸其中一个辅助触摸屏时,鼠标很容易松开 有没有办法在一台显示器上可靠地永久捕获鼠标?以下是一些简单的Windows窗体代码,您可以尝试一下。它使用SetWindowsHookEx函数设置全局鼠标挂钩。在hook方法中,它检查鼠标坐标是否在主屏幕的边界内,并根据需要调整坐标

我有一个Windows系统,多个显示器作为扩展桌面连接。只有主监视器在物理上对用户可见,因此我想在该监视器上捕获鼠标

似乎有一个使用ClipMouse API函数的简单解决方案,如中所述:

但是,当使用alt tab更改程序或触摸其中一个辅助触摸屏时,鼠标很容易松开


有没有办法在一台显示器上可靠地永久捕获鼠标?

以下是一些简单的Windows窗体代码,您可以尝试一下。它使用SetWindowsHookEx函数设置全局鼠标挂钩。在hook方法中,它检查鼠标坐标是否在主屏幕的边界内,并根据需要调整坐标。当您运行此代码时,只要您只是移动鼠标,鼠标光标仍然能够离开主屏幕区域,但一旦发生单击事件,它就会跳回。您可能希望将我的代码与ClipCursor技术结合起来,以防止这种情况发生

public partial class Form1 : Form
{
    private const int WH_MOUSE_LL = 14;

    private delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll", EntryPoint = "SetWindowsHookEx", SetLastError = true)]
    private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string lpModuleName);

    Rectangle _ScreenBounds;

    HookProc _HookProc;

    public Form1()
    {
        InitializeComponent();

        _ScreenBounds = Screen.PrimaryScreen.Bounds;
        _HookProc = HookMethod;
        IntPtr hook = SetWindowsHookEx(WH_MOUSE_LL, _HookProc, GetModuleHandle("user32"), 0);
        if (hook == IntPtr.Zero) throw new System.ComponentModel.Win32Exception();
    }

    private int HookMethod(int code, IntPtr wParam, IntPtr lParam)
    {
        if (Cursor.Position.X < _ScreenBounds.Left)
        {
            Cursor.Position = new Point(_ScreenBounds.Left, Cursor.Position.Y);
        }
        else if (Cursor.Position.X > _ScreenBounds.Right)
        {
            Cursor.Position = new Point(_ScreenBounds.Right - 1, Cursor.Position.Y);
        }

        if (Cursor.Position.Y < _ScreenBounds.Top)
        {
            Cursor.Position = new Point(Cursor.Position.X, _ScreenBounds.Top);
        }
        else if (Cursor.Position.Y > _ScreenBounds.Bottom)
        {
            Cursor.Position = new Point(Cursor.Position.X, _ScreenBounds.Bottom - 1);
        }

        return 0;
    }
}
公共部分类表单1:表单
{
私有常量int WH_MOUSE_LL=14;
私有委托int HookProc(int代码、IntPtr wParam、IntPtr lParam);
[DllImport(“user32.dll”,EntryPoint=“SetWindowsHookEx”,SetLastError=true)]
私有静态外部IntPtr setwindowshhookx(intidhook、HookProc lpfn、IntPtr hMod、uint dwThreadId);
[DllImport(“kernel32.dll”,CharSet=CharSet.Auto,SetLastError=true)]
私有静态外部IntPtr GetModuleHandle(字符串lpModuleName);
矩形屏幕边界;
HookProc_HookProc;
公共表格1()
{
初始化组件();
_ScreenBounds=Screen.PrimaryScreen.Bounds;
_HookProc=HookMethod;
IntPtr hook=setWindowshookx(WH_MOUSE_LL,_HookProc,GetModuleHandle(“user32”),0);
如果(hook==IntPtr.Zero)抛出新的System.ComponentModel.Win32Exception();
}
私有int HookMethod(int代码、IntPtr wParam、IntPtr lParam)
{
如果(光标位置X<_屏幕边界左)
{
Cursor.Position=新点(_screenbunds.Left,Cursor.Position.Y);
}
else if(Cursor.Position.X>\u ScreenBounds.Right)
{
Cursor.Position=新点(_.Right-1,Cursor.Position.Y);
}
如果(光标位置Y<_屏幕边界顶部)
{
Cursor.Position=新点(Cursor.Position.X,_screenbunds.Top);
}
else if(Cursor.Position.Y>\u ScreenBounds.Bottom)
{
Cursor.Position=新点(Cursor.Position.X,_.screenbunds.Bottom-1);
}
返回0;
}
}

感谢您提供此片段!我们玩过它,但由于它只有在按下或释放鼠标按钮后才会启动,所以它并不能真正解决我们的问题。所以我必须继续寻找别的东西。
public partial class Form1 : Form
{
    private const int WH_MOUSE_LL = 14;

    private delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll", EntryPoint = "SetWindowsHookEx", SetLastError = true)]
    private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string lpModuleName);

    Rectangle _ScreenBounds;

    HookProc _HookProc;

    public Form1()
    {
        InitializeComponent();

        _ScreenBounds = Screen.PrimaryScreen.Bounds;
        _HookProc = HookMethod;
        IntPtr hook = SetWindowsHookEx(WH_MOUSE_LL, _HookProc, GetModuleHandle("user32"), 0);
        if (hook == IntPtr.Zero) throw new System.ComponentModel.Win32Exception();
    }

    private int HookMethod(int code, IntPtr wParam, IntPtr lParam)
    {
        if (Cursor.Position.X < _ScreenBounds.Left)
        {
            Cursor.Position = new Point(_ScreenBounds.Left, Cursor.Position.Y);
        }
        else if (Cursor.Position.X > _ScreenBounds.Right)
        {
            Cursor.Position = new Point(_ScreenBounds.Right - 1, Cursor.Position.Y);
        }

        if (Cursor.Position.Y < _ScreenBounds.Top)
        {
            Cursor.Position = new Point(Cursor.Position.X, _ScreenBounds.Top);
        }
        else if (Cursor.Position.Y > _ScreenBounds.Bottom)
        {
            Cursor.Position = new Point(Cursor.Position.X, _ScreenBounds.Bottom - 1);
        }

        return 0;
    }
}