首次单击鼠标即可获取活动应用程序的名称

时间:2013-04-24 08:48:01

标签: c#

我正在尝试获取当前活动应用程序的名称,仅在该应用程序中第一次单击鼠标时才会显示。

我的意思是:当我总是在同一个应用程序上时,每次用户点击应用程序时我都不想获取应用程序的名称。我的代码运行得很好,但它会在每次单击鼠标时写入应用程序名称。

如果我想仅在用户点击其他应用程序时获取应用程序的名称,我该如何更改?

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

1 个答案:

答案 0 :(得分: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看到的标题,向哈希集添加新标题,如果已在哈希集中找到一个标题则压制

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