C# = 0; ; (i++) { SetupDiEnumDeviceInfo(信息、, 我 输出数据); //如果没有与筛选器匹配的项目,则抛出 if(Marshal.GetLastWin32Error()==错误\u无\u更多\u项) 检查错误(“未找到与筛选器匹配的设备。”,0xcffff); 检查错误(“SetupDiEnumDeviceInfo”); string devicepath=GetStringPropertyForDevice(信息, devdata,1);//SPDRP_硬件ID //取消对打印名称/路径的注释 //Console.WriteLine(GetStringPropertyForDevice)(信息, //devdata、DEVPKEY_设备_设备(ESC)); //Console.WriteLine(“{0}”,devicepath); 如果(devicepath!=null&&filter(devicepath))中断; } SP_CLASSINSTALL_标头=新SP_CLASSINSTALL_标头(); header.cbSize=(UInt32)Marshal.SizeOf(header); header.InstallFunction=DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams=新的SP_PROPCHANGE_PARAMS(); propchangeparams.ClassInstallHeader=标题; propchangeparams.StateChange=禁用?DICS_禁用:DICS_启用; propchangeparams.Scope=DICS_标志_全局; propchangeparams.HwProfile=0; SetupDiSetClassInstallParams(信息, 参考devdata, 参考propchangeparams, (UInt32)SizeOf元帅(propchangeparams); 检查错误(“SetupDiSetClassInstallParams”); 设置双更改状态( 信息, 参考devdata); 检查错误(“SetupDiChangeState”); } 最后 { 如果(信息!=IntPtr.Zero) SetupDiDestroyDeviceInfo列表(信息); } } 私有静态void CheckError(字符串消息,int lasterror=-1) { int code=lasterror==-1?封送。GetLastWin32Error():lasterror; 如果(代码!=0) 抛出新的ApplicationException( Format(“禁用硬件设备时出错(代码{0}):{1}”, 代码、消息); } 私有静态字符串GetStringPropertyForDevice(IntPtr信息、SP_设备信息、数据数据数据、, uint propId) { 单元式,特大型; IntPtr buffer=IntPtr.Zero; 尝试 { uint-buflen=512; buffer=Marshal.AllocHGlobal((int)buflen); 超大=0; //更改#2-使用此选项代替SetupDiGetDeviceProperty SetupDiGetDeviceRegistrPropertyW( 信息, 参考devdata, 普罗皮德, 出道具类型, 缓冲器 布弗伦, ref(特大型); 字节[]lbuffer=新字节[超大]; 封送处理副本(缓冲区,lbuffer,0,(int)超大); int errcode=Marshal.GetLastWin32Error(); if(errcode==ERROR\u INVALID\u DATA)返回null; 检查错误(“SetupDiGetDeviceProperty”,错误代码); 返回Encoding.Unicode.GetString(lbuffer); } 最后 { if(缓冲区!=IntPtr.Zero) 自由全球元帅(缓冲区); } } }

C# = 0; ; (i++) { SetupDiEnumDeviceInfo(信息、, 我 输出数据); //如果没有与筛选器匹配的项目,则抛出 if(Marshal.GetLastWin32Error()==错误\u无\u更多\u项) 检查错误(“未找到与筛选器匹配的设备。”,0xcffff); 检查错误(“SetupDiEnumDeviceInfo”); string devicepath=GetStringPropertyForDevice(信息, devdata,1);//SPDRP_硬件ID //取消对打印名称/路径的注释 //Console.WriteLine(GetStringPropertyForDevice)(信息, //devdata、DEVPKEY_设备_设备(ESC)); //Console.WriteLine(“{0}”,devicepath); 如果(devicepath!=null&&filter(devicepath))中断; } SP_CLASSINSTALL_标头=新SP_CLASSINSTALL_标头(); header.cbSize=(UInt32)Marshal.SizeOf(header); header.InstallFunction=DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams=新的SP_PROPCHANGE_PARAMS(); propchangeparams.ClassInstallHeader=标题; propchangeparams.StateChange=禁用?DICS_禁用:DICS_启用; propchangeparams.Scope=DICS_标志_全局; propchangeparams.HwProfile=0; SetupDiSetClassInstallParams(信息, 参考devdata, 参考propchangeparams, (UInt32)SizeOf元帅(propchangeparams); 检查错误(“SetupDiSetClassInstallParams”); 设置双更改状态( 信息, 参考devdata); 检查错误(“SetupDiChangeState”); } 最后 { 如果(信息!=IntPtr.Zero) SetupDiDestroyDeviceInfo列表(信息); } } 私有静态void CheckError(字符串消息,int lasterror=-1) { int code=lasterror==-1?封送。GetLastWin32Error():lasterror; 如果(代码!=0) 抛出新的ApplicationException( Format(“禁用硬件设备时出错(代码{0}):{1}”, 代码、消息); } 私有静态字符串GetStringPropertyForDevice(IntPtr信息、SP_设备信息、数据数据数据、, uint propId) { 单元式,特大型; IntPtr buffer=IntPtr.Zero; 尝试 { uint-buflen=512; buffer=Marshal.AllocHGlobal((int)buflen); 超大=0; //更改#2-使用此选项代替SetupDiGetDeviceProperty SetupDiGetDeviceRegistrPropertyW( 信息, 参考devdata, 普罗皮德, 出道具类型, 缓冲器 布弗伦, ref(特大型); 字节[]lbuffer=新字节[超大]; 封送处理副本(缓冲区,lbuffer,0,(int)超大); int errcode=Marshal.GetLastWin32Error(); if(errcode==ERROR\u INVALID\u DATA)返回null; 检查错误(“SetupDiGetDeviceProperty”,错误代码); 返回Encoding.Unicode.GetString(lbuffer); } 最后 { if(缓冲区!=IntPtr.Zero) 自由全球元帅(缓冲区); } } },c#,mouse,C#,Mouse,正如其他人所说,这在网络中至少是最麻烦的。我强烈建议您切换到C++/Win32来实现这一点。我想从长远来看,你会省去很多头疼的事。这是我所做的最主要的参考资料。除了在其他应用程序中限制输入外,它“起作用”。不过谢谢。从文章来看,我认为不可能对输入进行全局筛选。让我这么想的一句话是:“请注意,鼠标和键盘也是HID,因此来自它们的数据可以通过HID消息WM_输入和传统消息。应用程序可以通过在RAWINPUTDEVICE中正确选择标志来选择任何一种方法。”我会给你答案,因为这是“不可能的”在C#或.n


正如其他人所说,这在网络中至少是最麻烦的。我强烈建议您切换到C++/Win32来实现这一点。我想从长远来看,你会省去很多头疼的事。

这是我所做的最主要的参考资料。除了在其他应用程序中限制输入外,它“起作用”。不过谢谢。从文章来看,我认为不可能对输入进行全局筛选。让我这么想的一句话是:“请注意,鼠标和键盘也是HID,因此来自它们的数据可以通过HID消息WM_输入和传统消息。应用程序可以通过在RAWINPUTDEVICE中正确选择标志来选择任何一种方法。”我会给你答案,因为这是“不可能的”在C#或.net中。虽然这是可能的-但你必须使用非.net DLL,这基本上意味着用C/C++编程我一直在尝试使用全局鼠标钩子-这可能是我需要使用的。。。但到目前为止,我只能为Whu MOUSE实现这一点,它只为我的应用程序捕获消息,Whu MOUSE应该捕获所有消息,但我无法获取启动捕获的代码(它出错)。(看来我不在人间天堂,而且在C#的一些不受支持的功能中)。谢谢,我不会说也不会读俄语,所以论坛链接对我没有帮助。没有必要读俄语,这是一个鼠标钩子上的链接,评论都是英文的。它包含一些您需要的东西,例如:
private const int WH\u KEYBOARD\u LL=13
hMouseHook=setWindowshookx(WH_MOUSE_LL,MouseHookProcedure,Marshal.GetHINSTANCE(Assembly.getexecutinggassembly().GetModules()[0]),0)只需查看一下。我添加了这个链接,因为我在我的“收藏夹”lol中有它。没有必要阅读Russianheq的答案似乎是最正确的。重写base.WndProc仅控制应用程序接收的Windows消息,而与控制其他应用程序的Windows消息无关。一个全局鼠标钩子是一个不错的选择。它确实很受欢迎。一个鼠标挂钩只是成功的一半。实际上我两者都需要。这个钩子是用来防止点击表单转到其他应用程序的,但是我需要原始鼠标,这样我就可以知道是哪个设备进行了点击。真正的答案是C#(.net)不是正确的工具——很遗憾。你说你需要原始鼠标以便你[知道]点击的实际设备是什么意思?钩住鼠标单击时,可以确定活动窗口(GetActiveWindow())和位置
public static class DisableHardware
{
    const uint DIF_PROPERTYCHANGE = 0x12;
    const uint DICS_ENABLE = 1;
    const uint DICS_DISABLE = 2;  // disable device
    const uint DICS_FLAG_GLOBAL = 1; // not profile-specific
    const uint DIGCF_ALLCLASSES = 4;
    const uint DIGCF_PRESENT = 2;
    const uint ERROR_INVALID_DATA = 13;
    const uint ERROR_NO_MORE_ITEMS = 259;
    const uint ERROR_ELEMENT_NOT_FOUND = 1168;

    static DEVPROPKEY DEVPKEY_Device_DeviceDesc;
    static DEVPROPKEY DEVPKEY_Device_HardwareIds;

    [StructLayout(LayoutKind.Sequential)]
    struct SP_CLASSINSTALL_HEADER
    {
        public UInt32 cbSize;
        public UInt32 InstallFunction;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct SP_PROPCHANGE_PARAMS
    {
        public SP_CLASSINSTALL_HEADER ClassInstallHeader;
        public UInt32 StateChange;
        public UInt32 Scope;
        public UInt32 HwProfile;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct SP_DEVINFO_DATA
    {
        public UInt32 cbSize;
        public Guid classGuid;
        public UInt32 devInst;
        public IntPtr reserved;     // CHANGE #1 - was UInt32
    }

    [StructLayout(LayoutKind.Sequential)]
    struct DEVPROPKEY
    {
        public Guid fmtid;
        public UInt32 pid;
    }

    [DllImport("setupapi.dll", SetLastError = true)]
    static extern IntPtr SetupDiGetClassDevsW(
        [In] ref Guid ClassGuid,
        [MarshalAs(UnmanagedType.LPWStr)]
string Enumerator,
        IntPtr parent,
        UInt32 flags);

    [DllImport("setupapi.dll", SetLastError = true)]
    static extern bool SetupDiDestroyDeviceInfoList(IntPtr handle);

    [DllImport("setupapi.dll", SetLastError = true)]
    static extern bool SetupDiEnumDeviceInfo(IntPtr deviceInfoSet,
        UInt32 memberIndex,
        [Out] out SP_DEVINFO_DATA deviceInfoData);

    [DllImport("setupapi.dll", SetLastError = true)]
    static extern bool SetupDiSetClassInstallParams(
        IntPtr deviceInfoSet,
        [In] ref SP_DEVINFO_DATA deviceInfoData,
        [In] ref SP_PROPCHANGE_PARAMS classInstallParams,
        UInt32 ClassInstallParamsSize);

    [DllImport("setupapi.dll", SetLastError = true)]
    static extern bool SetupDiChangeState(
        IntPtr deviceInfoSet,
        [In] ref SP_DEVINFO_DATA deviceInfoData);

    [DllImport("setupapi.dll", SetLastError = true)]
    static extern bool SetupDiGetDevicePropertyW(
            IntPtr deviceInfoSet,
            [In] ref SP_DEVINFO_DATA DeviceInfoData,
            [In] ref DEVPROPKEY propertyKey,
            [Out] out UInt32 propertyType,
            IntPtr propertyBuffer,
            UInt32 propertyBufferSize,
            out UInt32 requiredSize,
            UInt32 flags);

    [DllImport("setupapi.dll", SetLastError = true)]
    static extern bool SetupDiGetDeviceRegistryPropertyW(
      IntPtr DeviceInfoSet,
      [In] ref SP_DEVINFO_DATA  DeviceInfoData,
      UInt32 Property,
      [Out] out UInt32  PropertyRegDataType,
      IntPtr PropertyBuffer,
      UInt32 PropertyBufferSize,
      [In,Out] ref UInt32 RequiredSize
    );

    static DisableHardware()
    {
        DisableHardware.DEVPKEY_Device_DeviceDesc = new DEVPROPKEY();
        DEVPKEY_Device_DeviceDesc.fmtid = new Guid(
                0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67,
                0xd1, 0x46, 0xa8, 0x50, 0xe0);
        DEVPKEY_Device_DeviceDesc.pid = 2;

        DEVPKEY_Device_HardwareIds = new DEVPROPKEY();
        DEVPKEY_Device_HardwareIds.fmtid = new Guid(
            0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67,
            0xd1, 0x46, 0xa8, 0x50, 0xe0);
        DEVPKEY_Device_HardwareIds.pid = 3;
    }




    public static void DisableDevice(Func<string, bool> filter, bool disable = true)
    {
        IntPtr info = IntPtr.Zero;
        Guid NullGuid = Guid.Empty;
        try
        {
            info = SetupDiGetClassDevsW(
                ref NullGuid,
                null,
                IntPtr.Zero,
                DIGCF_ALLCLASSES);
            CheckError("SetupDiGetClassDevs");

            SP_DEVINFO_DATA devdata = new SP_DEVINFO_DATA();
            devdata.cbSize = (UInt32)Marshal.SizeOf(devdata);

            // Get first device matching device criterion.
            for (uint i = 0; ; i++)
            {
                SetupDiEnumDeviceInfo(info,
                    i,
                    out devdata);
                // if no items match filter, throw
                if (Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS)
                    CheckError("No device found matching filter.", 0xcffff);
                CheckError("SetupDiEnumDeviceInfo");

                string devicepath = GetStringPropertyForDevice(info,
                                           devdata, 1); // SPDRP_HARDWAREID

                // Uncomment to print name/path
                //Console.WriteLine(GetStringPropertyForDevice(info,
                //                         devdata, DEVPKEY_Device_DeviceDesc));
                //Console.WriteLine("   {0}", devicepath);
                if (devicepath != null && filter(devicepath)) break;

            }

            SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER();
            header.cbSize = (UInt32)Marshal.SizeOf(header);
            header.InstallFunction = DIF_PROPERTYCHANGE;

            SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS();
            propchangeparams.ClassInstallHeader = header;
            propchangeparams.StateChange = disable ? DICS_DISABLE : DICS_ENABLE;
            propchangeparams.Scope = DICS_FLAG_GLOBAL;
            propchangeparams.HwProfile = 0;

            SetupDiSetClassInstallParams(info,
                ref devdata,
                ref propchangeparams,
                (UInt32)Marshal.SizeOf(propchangeparams));
            CheckError("SetupDiSetClassInstallParams");

            SetupDiChangeState(
                info,
                ref devdata);
            CheckError("SetupDiChangeState");
        }
        finally
        {
            if (info != IntPtr.Zero)
                SetupDiDestroyDeviceInfoList(info);
        }
    }
    private static void CheckError(string message, int lasterror = -1)
    {

        int code = lasterror == -1 ? Marshal.GetLastWin32Error() : lasterror;
        if (code != 0)
            throw new ApplicationException(
                String.Format("Error disabling hardware device (Code {0}): {1}",
                    code, message));
    }

    private static string GetStringPropertyForDevice(IntPtr info, SP_DEVINFO_DATA devdata,
        uint propId)
    {
        uint proptype, outsize;
        IntPtr buffer = IntPtr.Zero;
        try
        {
            uint buflen = 512;
            buffer = Marshal.AllocHGlobal((int)buflen);
            outsize=0;
            // CHANGE #2 - Use this instead of SetupDiGetDeviceProperty 
            SetupDiGetDeviceRegistryPropertyW(
                info,
                ref devdata,
                propId,
                out proptype,
                buffer,
                buflen,
                ref outsize);
            byte[] lbuffer = new byte[outsize];
            Marshal.Copy(buffer, lbuffer, 0, (int)outsize);
            int errcode = Marshal.GetLastWin32Error();
            if (errcode == ERROR_INVALID_DATA) return null;
            CheckError("SetupDiGetDeviceProperty", errcode);
            return Encoding.Unicode.GetString(lbuffer);
        }
        finally
        {
            if (buffer != IntPtr.Zero)
                Marshal.FreeHGlobal(buffer);
        }
    }

}