2013-07-04 27 views
0

我試圖做出可以通過輸入登錄鉤用戶不活動的應用程序,但我被困在時刻,因爲編譯器會引發錯誤說爲什麼我的輸入鉤不工作?

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


      } 
     } 
    } 
} 

任何與此ISSU所有幫助Ë將不勝感激=]

回答

0

你需要讓靜態的,因爲它不是一個實例屬性

public static HookProc MouseHookProcedure; 
+0

解決的問題,非常感謝=] –

+0

你歡迎:) – keyboardP

0

讓它靜:

public static HookProc MouseHookProcedure;