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

1 回答 1

0

你至少有两个明显的选择。一个是保留你最后一次看到的标题并在下一个标题与最后一次看到的标题相同的情况下停止写作。

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 中找到一个标题,则取消

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);
}
于 2014-06-26T20:38:31.653 回答