zoukankan      html  css  js  c++  java
  • c++hook全局触控事件

    https://gist.github.com/vbfox/1339671

    namespace BlackFox
    {
        using System;
        using System.ComponentModel;
        using System.Runtime.InteropServices;
        using System.Security;
    
        /// <summary>
        /// As long as this object exists all mouse events created from a touch event for legacy support will be disabled.
        /// </summary>
        class DisableTouchConversionToMouse : IDisposable
        {
            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(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)
                    {
                        return new IntPtr(1);
                    }
                }
    
                return UnsafeNativeMethods.CallNextHookEx(hookId, nCode, wParam, lParam);
            }
            
            bool disposed;
    
            public void Dispose()
            {
                if (disposed) return;
    
                UnsafeNativeMethods.UnhookWindowsHookEx(hookId);
                disposed = true;
                GC.SuppressFinalize(this);
            }
    
            ~DisableTouchConversionToMouse()
            {
                Dispose();
            }
    
            #region Interop
    
            // ReSharper disable InconsistentNaming
            // ReSharper disable MemberCanBePrivate.Local
            // ReSharper disable FieldCanBeMadeReadOnly.Local
    
            const uint MOUSEEVENTF_FROMTOUCH = 0xFF515700;
            const int 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(int idHook, 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);
            }
    
            // ReSharper restore InconsistentNaming
            // ReSharper restore FieldCanBeMadeReadOnly.Local
            // ReSharper restore MemberCanBePrivate.Local
    
            #endregion
        }
    }
  • 相关阅读:
    Junit单元测试
    win7的6个网络命令
    WOJ1024 (POJ1985+POJ2631) Exploration 树/BFS
    WOJ1022 Competition of Programming 贪心 WOJ1023 Division dp
    woj1019 Curriculum Schedule 输入输出 woj1020 Adjacent Difference 排序
    woj1018(HDU4384)KING KONG 循环群
    woj1016 cherry blossom woj1017 Billiard ball 几何
    woj1013 Barcelet 字符串 woj1014 Doraemon's Flashlight 几何
    woj1012 Thingk and Count DP好题
    woj1010 alternate sum 数学 woj1011 Finding Teamates 数学
  • 原文地址:https://www.cnblogs.com/wangjixianyun/p/8988416.html
Copyright © 2011-2022 走看看