繁体   English   中英

为什么输入挂钩不起作用?

[英]Why won't my Input Hook work?

我正在尝试制作一个可以通过输入挂钩记录用户非活动状态的应用程序,但是由于编译器抛出错误,此刻我被卡住了

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); 


            }
        }
    }
}

对于此问题的所有帮助将不胜感激=]

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

public static HookProc MouseHookProcedure;

使其静态:

public static HookProc MouseHookProcedure;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM