C# 为什么赢了';我的输入钩子不工作吗?
我正在尝试创建一个应用程序,它可以通过一个输入钩子记录用户的不活动,但是我现在陷入了困境,因为编译器抛出了一个错误C# 为什么赢了';我的输入钩子不工作吗?,c#,events,C#,Events,我正在尝试创建一个应用程序,它可以通过一个输入钩子记录用户的不活动,但是我现在陷入了困境,因为编译器抛出了一个错误 an object reference is required for the non-static field, method, or property 'NotifyIcon.InputHook.MouseHookProcedure' 这是我的非活动自定义事件处理程序代码 public void inactivity_Active(object sender, EventAr
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类,这设置了输入钩子
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);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Runtime.InteropServices;
名称空间通知图标
{
公共类InputHook
{
公共委托int-HookProc(int-nCode、IntPtr-wParam、IntPtr-lParam);
公共静态int hHook=0;
公共常数int WH_鼠标=7;
公共挂钩程序鼠标挂钩程序;
[StructLayout(LayoutKind.Sequential)]
公共课点
{
公共int x;
公共智力;
}
[StructLayout(LayoutKind.Sequential)]
公营鼠屋货车
{
公共点;
公共卫生署;
公共测试代码;
公共信息;
}
[DllImport(“user32.dll”,CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
公共静态外部int SetWindowsHookEx(int idHook、HookProc lpfn、,
IntPtr hInstance,int threadId);
[DllImport(“user32.dll”,CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
公共静态外部bool unhookwindowshookx(intidhook);
[DllImport(“user32.dll”,CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
公共静态外部int CALLNEXTHOKEX(int idHook,int nCode,
IntPtr wParam、IntPtr lParam);
公共静态int-MouseHookProc(int-nCode、IntPtr-wParam、IntPtr-lParam)
{
//整理回调中的数据。
MouseHookStruct MyMouseHookStruct=(MouseHookStruct)Marshal.PtrToStructure(lParam,typeof(MouseHookStruct));
if(nCode<0)
{
返回CallNextHookEx(hHook、nCode、wParam、lParam);
}
其他的
{
//创建一个显示当前鼠标坐标的字符串变量。
字符串stroption=“x=”+
MyMouseHookStruct.pt.x.ToString(“d”)+
“y=”+
MyMouseHookStruct.pt.y.ToString(“d”);
返回CallNextHookEx(hHook、nCode、wParam、lParam);
}
}
}
}
对此问题的任何和所有帮助都将不胜感激=]您需要将其设置为静态,因为它不是实例属性
public static HookProc MouseHookProcedure;
public static HookProc MouseHookProcedure;
将其设置为静态: