为什么获胜';t我的输入挂钩工作

本文关键字:输入 工作 我的 获胜 为什么 | 更新日期: 2023-09-27 18:21:12

我正在尝试制作一个应用程序,它可以通过输入挂钩记录用户的不活动状态,但我现在被卡住了,因为编译器抛出了一个错误,说

an object reference is required for the non-static field, method, or property 'NotifyIcon.InputHook.MouseHookProcedure'

这是我的非活动自定义事件处理程序代码

public void inactivity_Active(object sender, EventArgs e)
        {
            if (InputHook.hHook == 0)
            {
               InputHook.MouseHookProcedure = new InputHook.HookProc(InputHook.MouseHookProc);
              InputHook.hHook = InputHook.SetWindowsHookEx(InputHook.WH_MOUSE,
                  InputHook.MouseHookProcedure,
                   (IntPtr)0,
                   AppDomain.GetCurrentThreadId());
            }

对于我的InputHook类,这设置了Input Hook

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;


namespace NotifyIcon
{
    public class InputHook
    {
        public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
        public static int hHook = 0;
        public const int WH_MOUSE = 7;
        public HookProc MouseHookProcedure;
        [StructLayout(LayoutKind.Sequential)]
        public class POINT
        {
            public int x;
            public int y;
        }
        [StructLayout(LayoutKind.Sequential)]
        public class MouseHookStruct
        {
            public POINT pt;
            public int hwnd;
            public int wHitTestCode;
            public int dwExtraInfo;
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto,
 CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn,
        IntPtr hInstance, int threadId);
        [DllImport("user32.dll", CharSet = CharSet.Auto,
 CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);
        [DllImport("user32.dll", CharSet = CharSet.Auto,
 CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(int idHook, int nCode,
        IntPtr wParam, IntPtr lParam);

        public static int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            //Marshall the data from the callback.
            MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
            if (nCode < 0)
            {
                return CallNextHookEx(hHook, nCode, wParam, lParam);
            }
            else
            {
                //Create a string variable that shows the current mouse coordinates.
                String strCaption = "x = " +
                        MyMouseHookStruct.pt.x.ToString("d") +
                            "  y = " +
                MyMouseHookStruct.pt.y.ToString("d");
                return CallNextHookEx(hHook, nCode, wParam, lParam); 

            }
        }
    }
}

我们将非常感谢有关此问题的任何帮助=]

为什么获胜';t我的输入挂钩工作

您需要将其设置为静态,因为它不是实例属性

public static HookProc MouseHookProcedure;

使其静态:

public static HookProc MouseHookProcedure;