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

时间:2013-07-04 09:52:48

标签: c# events

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

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


            }
        }
    }
}

非常感谢这个问题的任何和所有帮助=]

2 个答案:

答案 0 :(得分:0)

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

public static HookProc MouseHookProcedure;

答案 1 :(得分:0)

让它静止:

public static HookProc MouseHookProcedure;