2013-04-24 35 views
1

我試圖獲取當前活動應用程序的名稱,僅在該應用程序中的第一次鼠標單擊。在第一次鼠標單擊時獲取活動應用程序的名稱

我的意思是:當我總是在同一個應用程序中時,我不想每次用戶單擊應用程序時都獲取應用程序的名稱。我的代碼功能非常好,但是它在每次點擊鼠標時寫入應用程序名稱。

如果我想僅在用戶單擊另一個應用程序時才獲取應用程序的名稱,我該如何更改它?

class InterceptMouse 
{ 
    private static LowLevelMouseProc _proc = HookCallback; 
    private static IntPtr _hookID = IntPtr.Zero; 
    private const int WM_LBUTTONDOWN = 0x0201; 
    private const int WH_MOUSE_LL = 14; 
    public static void Main() 
    { 
     _hookID = SetMouseHook(_proc); 
     Application.Run(); 
     UnhookWindowsHookEx(_hookID); 
    } 

    private static IntPtr SetMouseHook(LowLevelMouseProc proc) 
    { 
     using (Process curProcess = Process.GetCurrentProcess()) 
     using (ProcessModule curModule = curProcess.MainModule) 
     { 
      return SetWindowsHookEx(WH_MOUSE_LL, proc, 
       GetModuleHandle(curModule.ModuleName), 0); 
     } 
    } 

    private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam); 

    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) 
    { 
     if (nCode >= 0 && wParam == (IntPtr)WM_LBUTTONDOWN) 
     { 
      IntPtr hwnd2 = GetForegroundWindow(); 
      StringBuilder windowtitle = new StringBuilder(256); 
      if (GetWindowText(hwnd2, windowtitle, windowtitle.Capacity) > 0) 
       Console.WriteLine(windowtitle); 
     } 

     return CallNextHookEx(_hookID, nCode, wParam, lParam); 
    } 


    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern IntPtr SetWindowsHookEx(int idHook, 
     LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId); 

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    private static extern bool UnhookWindowsHookEx(IntPtr hhk); 

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, 
     IntPtr wParam, IntPtr lParam); 

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern IntPtr GetModuleHandle(string lpModuleName); 

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] 
    public static extern IntPtr GetForegroundWindow(); 

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, 
     int nMaxCount); 
} 
+0

不知道的API。但我想首先你會得到窗口名稱,然後可以得到它擁有它的過程 – tgkprog 2013-04-24 08:49:30

+0

如果你只想識別活動窗體的變化,不會[[WM_SETFOCUS'](http://msdn.microsoft .com/en-us/library/windows/desktop/ms646283%28v = vs.85%29.aspx)事件更符合你的目的? – Carsten 2013-04-24 08:51:45

回答

0

您至少有兩個明顯的選項。一個是保持你最後看到的標題和抑制寫作,如果下一個標題是equal到最後看到的標題。

static string lastseen = null; 

private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) 
{ 
    if (nCode >= 0 && wParam == (IntPtr)WM_LBUTTONDOWN) 
    { 
     IntPtr hwnd2 = GetForegroundWindow(); 
     StringBuilder windowtitle = new StringBuilder(256); 
     if (GetWindowText(hwnd2, windowtitle, windowtitle.Capacity) > 0) 
     { 
      var title = windowtitle.ToString(); 
      if (!String.Equals(lastseen, title) 
      { 
       lastseen = title; 
       Console.WriteLine(lastseen); 
      } 
     } 
    } 

    return CallNextHookEx(_hookID, nCode, wParam, lParam); 
} 

第二個選擇是保持看到標題的HashSet,添加新遊戲到HashSet的,surpressing如果一個冠軍是在HashSet中已經找到

static seen = new HashSet<string>(); 

private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) 
{ 
    if (nCode >= 0 && wParam == (IntPtr)WM_LBUTTONDOWN) 
    { 
     IntPtr hwnd2 = GetForegroundWindow(); 
     StringBuilder windowtitle = new StringBuilder(256); 
     if (GetWindowText(hwnd2, windowtitle, windowtitle.Capacity) > 0) 
     { 
      var title = windowtitle.ToString(); 
      if (!seen.Contains(title)) 
      { 
       seen.Add(title); 
       Console.WriteLine(title); 
      } 
     } 
    } 

    return CallNextHookEx(_hookID, nCode, wParam, lParam); 
} 
相關問題