我有一个 WPF C# 应用程序。我有双屏,我想取消其中一个屏幕上的鼠标事件,因为它是一个触摸屏,当用户按下它时,它会将焦点从主屏幕上移开。我只想在向用户请求数据时启用触摸,然后阻止它。
我找到了一些关于使用 user32.dll 的应用程序钩子的好例子,这有助于我捕捉设备输入。此处显示了一个示例:http ://www.pinvoke.net/default.aspx/user32.setwindowshookex 。另一个更适合鼠标事件的可以在这里展示:How to Avoid mouse move on Touch
我的问题不是捕捉设备输入,而是禁用它。上面的第一个示例显示了如何捕获键盘输入并提取按键,但不禁用它。
第二个例子也不行。它同时捕获鼠标事件、常规 USB 鼠标设备和触摸屏设备。我可以分辨出哪个是哪个,但我无法取消触摸屏设备。在示例中,new IntPtr(1);
为触摸设备返回 a。这对设备输入没有任何作用。
另一方面,当我做相反的事情时,意味着返回 new IntPtr(1); 在每个鼠标事件但触摸事件上,似乎我的常规鼠标根本不移动(而触摸设备鼠标确实移动)。
这种方法可以帮助阻止普通鼠标,但不能阻止触摸鼠标,因为我返回的任何代码都不会阻止这个鼠标(我确实找到了这个鼠标并返回一个代码,我检查了很多次,但代码什么也没做)。
这是我的代码基于上面的例子:
static readonly LowLevelMouseProc hookCallback = HookCallback;
static IntPtr hookId = IntPtr.Zero;
public DisableTouchConversionToMouse()
{
hookId = SetHook(hookCallback);
}
static IntPtr SetHook(LowLevelMouseProc proc)
{
var moduleHandle = UnsafeNativeMethods.GetModuleHandle(null);
var setHookResult = UnsafeNativeMethods.SetWindowsHookEx(HookType.WH_MOUSE_LL,
proc, moduleHandle, 0);
if (setHookResult == IntPtr.Zero)
{
throw new Win32Exception();
}
return setHookResult;
}
delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
var info = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
var extraInfo = (uint)info.dwExtraInfo.ToInt32();
// if ((extraInfo & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH)
if (extraInfo>0)
{
Console.WriteLine("TOUCH SCREEN FOUND!");
return new IntPtr(1);
}
}
return UnsafeNativeMethods.CallNextHookEx(hookId, nCode, wParam, lParam);
}
public enum HookType : int
{
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}
[StructLayout(LayoutKind.Sequential)]
struct POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
struct MSLLHOOKSTRUCT
{
public POINT pt;
public uint mouseData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}
[SuppressUnmanagedCodeSecurity]
static class UnsafeNativeMethods
{
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(HookType code, LowLevelMouseProc lpfn, IntPtr hMod,
uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
}