我的目标应该是一个简单的目标... 获取在我的表单上触发点击事件的鼠标的设备句柄(我有多个鼠标)。
在我的表格上,我已经修补了
public bool PreFilterMessage(ref Message message) {
...以确保不会错过任何子控件(这工作正常)。
在这个例程中,我调用了一个类来处理所有必要的 API 来获取设备句柄:
public bool PreFilterMessage(ref Message message) {
switch (message.Msg) {
case 0x0201: //LButtonDown
int HardwareID = new clsGetInputID().GetDeviceID(message);
break;
case 0x204: //RButtonDown
int HardwareID2 = new clsGetInputID().GetDeviceID(message);
break;
}
HardwareID 总是显示不同的数字,即使我用同一个鼠标单击。
我怀疑我有一些常量设置不正确,或者我编组不正确。
class clsGetInputID {
[DllImport("User32.dll")]
extern static uint GetRawInputDeviceInfo(IntPtr hDevice, uint uiCommand, IntPtr pData, ref uint pcbSize);
[DllImport("User32.dll")]
extern static uint GetRawInputData(IntPtr hRawInput, uint uiCommand, IntPtr pData, ref uint pcbSize, uint cbSizeHeader);
private const int RID_INPUT = 0x10000003;
[StructLayout(LayoutKind.Sequential)]
internal struct BUTTONSSTR {
[MarshalAs(UnmanagedType.U2)]
public ushort usButtonFlags;
[MarshalAs(UnmanagedType.U2)]
public ushort usButtonData;
}
[StructLayout(LayoutKind.Sequential)]
internal struct RAWHID {
[MarshalAs(UnmanagedType.U4)]
public int dwSizHid;
[MarshalAs(UnmanagedType.U4)]
public int dwCount;
}
[StructLayout(LayoutKind.Explicit)]
internal struct RAWMOUSE {
[MarshalAs(UnmanagedType.U2)]
[FieldOffset(0)]
public ushort usFlags;
[MarshalAs(UnmanagedType.U4)]
[FieldOffset(4)]
public uint ulButtons;
[FieldOffset(4)]
public BUTTONSSTR buttonsStr;
[MarshalAs(UnmanagedType.U4)]
[FieldOffset(8)]
public uint ulRawButtons;
[FieldOffset(12)]
public int lLastX;
[FieldOffset(16)]
public int lLastY;
[MarshalAs(UnmanagedType.U4)]
[FieldOffset(20)]
public uint ulExtraInformation;
}
[StructLayout(LayoutKind.Sequential)]
internal struct RAWKEYBOARD {
[MarshalAs(UnmanagedType.U2)]
public ushort MakeCode;
[MarshalAs(UnmanagedType.U2)]
public ushort Flags;
[MarshalAs(UnmanagedType.U2)]
public ushort Reserved;
[MarshalAs(UnmanagedType.U2)]
public ushort VKey;
[MarshalAs(UnmanagedType.U4)]
public uint Message;
[MarshalAs(UnmanagedType.U4)]
public uint ExtraInformation;
}
[StructLayout(LayoutKind.Explicit)]
internal struct RAWINPUT {
[FieldOffset(0)]
public RAWINPUTHEADER header;
[FieldOffset(16)]
public RAWMOUSE mouse;
[FieldOffset(16)]
public RAWKEYBOARD keyboard;
[FieldOffset(16)]
public RAWHID hid;
}
[StructLayout(LayoutKind.Sequential)]
internal struct RAWINPUTHEADER {
[MarshalAs(UnmanagedType.U4)]
public int dwType;
[MarshalAs(UnmanagedType.U4)]
public int dwSize;
public IntPtr hDevice;
[MarshalAs(UnmanagedType.U4)]
public int wParam;
}
[StructLayout(LayoutKind.Sequential)]
internal struct RID_DEVICE_INFO_HID {
[MarshalAs(UnmanagedType.U4)]
public int dwVendorId;
[MarshalAs(UnmanagedType.U4)]
public int dwProductId;
[MarshalAs(UnmanagedType.U4)]
public int dwVersionNumber;
[MarshalAs(UnmanagedType.U2)]
public ushort usUsagePage;
[MarshalAs(UnmanagedType.U2)]
public ushort usUsage;
}
[StructLayout(LayoutKind.Sequential)]
internal struct RID_DEVICE_INFO_KEYBOARD {
[MarshalAs(UnmanagedType.U4)]
public int dwType;
[MarshalAs(UnmanagedType.U4)]
public int dwSubType;
[MarshalAs(UnmanagedType.U4)]
public int dwKeyboardMode;
[MarshalAs(UnmanagedType.U4)]
public int dwNumberOfFunctionKeys;
[MarshalAs(UnmanagedType.U4)]
public int dwNumberOfIndicators;
[MarshalAs(UnmanagedType.U4)]
public int dwNumberOfKeysTotal;
}
[StructLayout(LayoutKind.Sequential)]
internal struct RID_DEVICE_INFO_MOUSE {
[MarshalAs(UnmanagedType.U4)]
public int dwId;
[MarshalAs(UnmanagedType.U4)]
public int dwNumberOfButtons;
[MarshalAs(UnmanagedType.U4)]
public int dwSampleRate;
[MarshalAs(UnmanagedType.U4)]
public int fHasHorizontalWheel;
}
[StructLayout(LayoutKind.Explicit)]
internal struct RID_DEVICE_INFO {
[FieldOffset(0)]
public int cbSize;
[FieldOffset(4)]
public int dwType;
[FieldOffset(8)]
public RID_DEVICE_INFO_MOUSE mouse;
[FieldOffset(8)]
public RID_DEVICE_INFO_KEYBOARD keyboard;
[FieldOffset(8)]
public RID_DEVICE_INFO_HID hid;
}
public int GetDeviceID(Message message) {
uint dwSize = 0;
GetRawInputData(
message.LParam,
RID_INPUT,
IntPtr.Zero,
ref dwSize,
(uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))
);
IntPtr buffer = Marshal.AllocHGlobal((int)dwSize);
GetRawInputData(
message.LParam,
RID_INPUT,
buffer,
ref dwSize,
(uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))
);
RAWINPUT raw = (RAWINPUT)Marshal.PtrToStructure(buffer, typeof(RAWINPUT));
uint size = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(RID_DEVICE_INFO));
GetRawInputDeviceInfo(raw.header.hDevice, 0x2000000b, IntPtr.Zero, ref size);
IntPtr HardwareRawInfoPTR = Marshal.AllocHGlobal((int)size);
GetRawInputDeviceInfo(raw.header.hDevice, 0x2000000b, HardwareRawInfoPTR, ref size);
RID_DEVICE_INFO RawDevInfo = (RID_DEVICE_INFO)Marshal.PtrToStructure(HardwareRawInfoPTR, typeof(RID_DEVICE_INFO));
Marshal.FreeHGlobal(buffer);
System.Diagnostics.Debug.WriteLine(String.Format("hDevice: {0} HdHandle: {1}", raw.header.hDevice, RawDevInfo.mouse.dwId));
return RawDevInfo.mouse.dwId;
}
}
raw.header.hDevice & RawDevInfo.mouse.dwId 总是不同的值。
谁能发现我做错了什么?