透明形式无法解释地仅传递一些触摸输入

时间:2019-06-25 19:53:49

标签: c# winforms transparent

在我要拿起笔记本电脑的地方工作时遇到问题,由于屏幕几乎是笔记本电脑顶部的整个区域,所以我的手最终碰到了屏幕,并且经常最小化或关闭程序,我一起工作。为了解决这个问题,我制作了一个小程序来“禁用”笔记本电脑上的触摸屏,这是由Windows 10在设备管理器外部缺少禁用选项引起的(在Windows 7上,您可以使用鼠标设置-在win10上,此选项消失了)。

该程序通过本质上识别和删除被识别为触摸的鼠标输入而很好地工作,但是有一个奇怪的怪癖,因为一些窗口仍然接收触摸输入,例如Edge和Chrome。大多数程序都没有此问题,并且该程序通常可以正确删除触摸输入。没什么大不了的,但是我想知道为什么那些窗口在其他窗口不接收的情况下仍能接收输入。

我已经附上了代码(很小),有人知道为什么有些程序仍然收到这些输入吗?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Input;

namespace TouchDisable
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Width = Screen.PrimaryScreen.Bounds.Width;
            Height = Screen.PrimaryScreen.Bounds.Height;
            _hookID = SetHook(_proc);
        }
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            UnhookWindowsHookEx(_hookID);
        }


        //------------------------------------------------------------------------------------------
        private static LowLevelMouseProc _proc = HookCallback;
        private static IntPtr _hookID = IntPtr.Zero;

        private static IntPtr SetHook(LowLevelMouseProc proc)
        {
            using (Process curProcess = Process.GetCurrentProcess())
            using (ProcessModule curModule = curProcess.MainModule)
            {
                return SetWindowsHookEx(WH_MOUSE_LL, proc,
                    GetModuleHandle(curModule.ModuleName), 0);
            }
        }

        private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);

        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (IsTouch(lParam))
                return new IntPtr(1);
            else
                return CallNextHookEx(_hookID, nCode, wParam, lParam);
        }

        private const int WH_MOUSE_LL = 14;

        private enum MouseMessages
        {
            WM_LBUTTONDOWN = 0x0201,
            WM_LBUTTONUP = 0x0202,
            WM_MOUSEMOVE = 0x0200,
            WM_MOUSEWHEEL = 0x020A,
            WM_RBUTTONDOWN = 0x0204,
            WM_RBUTTONUP = 0x0205
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct POINT
        {
            public int x;
            public int y;
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct MSLLHOOKSTRUCT
        {
            public POINT pt;
            public uint mouseData;
            public uint flags;
            public uint time;
            public IntPtr dwExtraInfo;
        }

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

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
            IntPtr wParam, IntPtr lParam);

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


        //------------------------------------------------------------------------------------------

        const uint TOUCH_FLAG = 0xFF515700;
        static bool IsTouch(IntPtr lParam)
        {
            MSLLHOOKSTRUCT hookData = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam,
                typeof(MSLLHOOKSTRUCT));
            uint extraInfo = (uint)hookData.dwExtraInfo.ToInt32();
            if ((extraInfo & TOUCH_FLAG) == TOUCH_FLAG)
                return true;
            return false;
        }
    }
}

0 个答案:

没有答案