2013-06-21 35 views
0

我在最近两天内遇到了一个问题,我想要获取他点击的用户进程。就像用户点击记事本一样,我的程序应该告诉我用户点击记事本。记事本已打开。如果用户点击计算器,我的程序也会告诉用户点击计算器。 Calcultor进程正在运行。如何使用Intptr(USER32.dll)从鼠标事件获取进程???

为此,我使用了这段代码。钩经理给我鼠标点击事件,但没有给我的过程。

我只得到鼠标intptr事件。

private static void WindowEventCallback(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) 
{ 
    Console.WriteLine("Event {0}", hwnd);// it is giving me mouse event 
    /*uint pid; 
    GetWindowThreadProcessId(hwnd, out pid);// it gives me process id 
    Process p = Process.GetProcessById((int)pid);// now here exception occured not in vs studio but when i run its exe then its gives me access violation exception 
    if (!my.ContainsKey(p.MainWindowTitle.ToString())) 
    { 
      my.Add(p.MainWindowTitle.ToString(), p.Id.ToString()); 
      Console.WriteLine("\r\n"); 
      Console.WriteLine("Status = Running"); 
      Console.WriteLine("\r\n Window Title:" + p.MainWindowTitle.ToString()); 
      Console.WriteLine("\r\n Process Name:" + p.ProcessName.ToString()); 
      Console.WriteLine("\r\n Process Starting Time:" + p.StartTime.ToString()); 
    }*/ 
} 

完整的代码是

static void Main(string[] args) 
    { 
     HookManager.SubscribeToWindowEvents(); 

     EventLoop.Run(); 
    } 


    public static class HookManager 
    { 
     [DllImport("user32.dll")] 
     public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint ProcessId); 
     public static void SubscribeToWindowEvents() 
     { 
      if (windowEventHook == IntPtr.Zero) 
      { 
       windowEventHook = SetWinEventHook(
        EVENT_SYSTEM_FOREGROUND, // eventMin 
        EVENT_SYSTEM_FOREGROUND, // eventMax 
        IntPtr.Zero,    // hmodWinEventProc 
        WindowEventCallback,  // lpfnWinEventProc 
        0,      // idProcess 
        0,      // idThread 
        WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS); 

       if (windowEventHook == IntPtr.Zero) 
       { 
        throw new Win32Exception(Marshal.GetLastWin32Error()); 
       } 
      } 
     } 

     static Dictionary<string, string> my = new Dictionary<string, string>(); 


     private static void WindowEventCallback(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) 
     { 
      Console.WriteLine("Event {0}", hwnd); 
      /*uint pid; 
      GetWindowThreadProcessId(hwnd, out pid); 
      Process p = Process.GetProcessById((int)pid); 
      if (!my.ContainsKey(p.MainWindowTitle.ToString())) 
      { 
       my.Add(p.MainWindowTitle.ToString(), p.Id.ToString()); 
       Console.WriteLine("\r\n"); 
       Console.WriteLine("Status = Running"); 
       Console.WriteLine("\r\n Window Title:" + p.MainWindowTitle.ToString()); 
       Console.WriteLine("\r\n Process Name:" + p.ProcessName.ToString()); 
       Console.WriteLine("\r\n Process Starting Time:" + p.StartTime.ToString()); 


      }*/ 
     } 
    } 

    private static IntPtr windowEventHook; 

    private delegate void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime); 

    [DllImport("user32.dll", SetLastError = true)] 
    private static extern IntPtr SetWinEventHook(int eventMin, int eventMax, IntPtr hmodWinEventProc, WinEventProc lpfnWinEventProc, int idProcess, int idThread, int dwflags); 
    [DllImport("user32.dll", SetLastError = true)] 
    private static extern int UnhookWinEvent(IntPtr hWinEventHook); 

    private const int WINEVENT_INCONTEXT = 4; 
    private const int WINEVENT_OUTOFCONTEXT = 0; 
    private const int WINEVENT_SKIPOWNPROCESS = 2; 
    private const int WINEVENT_SKIPOWNTHREAD = 1; 

    private const int EVENT_SYSTEM_FOREGROUND = 3; 


    public static class EventLoop 
    { 
     public static void Run() 
     { 
      MSG msg; 

      while (true) 
      { 

       if (PeekMessage(out msg, IntPtr.Zero, 0, 0, PM_REMOVE)) 
       { 
        if (msg.Message == WM_QUIT) 
         break; 

        TranslateMessage(ref msg); 
        DispatchMessage(ref msg); 
       } 
      } 
     } 

     [StructLayout(LayoutKind.Sequential)] 
     private struct MSG 
     { 
      public IntPtr Hwnd; 
      public uint Message; 
      public IntPtr WParam; 
      public IntPtr LParam; 
      public uint Time; 
     } 

     const uint PM_NOREMOVE = 0; 
     const uint PM_REMOVE = 1; 

     const uint WM_QUIT = 0x0012; 

     [DllImport("user32.dll")] 
     private static extern bool PeekMessage(out MSG lpMsg, IntPtr hwnd, uint wMsgFilterMin, uint wMsgFilterMax, uint wRemoveMsg); 
     [DllImport("user32.dll")] 
     private static extern bool TranslateMessage(ref MSG lpMsg); 
     [DllImport("user32.dll")] 
     private static extern IntPtr DispatchMessage(ref MSG lpMsg); 
    } 
} 
+0

我不知道你问的问题正确的问题。你确定你不是在寻找类似[UI Automation](http://msdn.microsoft.com/en-us/library/ms753388.aspx)系统的东西吗? - 它是一个框架,可以让您确定用户与其他程序进行交互的方式,如果需要,还可以代表用户执行输入。尽管它是一个.NET解决方案,但它适用于所有可访问的应用程序,无论是否管理。 –

回答

相关问题