有没有简单的方法来检查是否已激活某些进程?

时间:2020-02-17 05:42:57

标签: c# hook postmessage

我使用postmessage api向一些程序发送了WM_ACTIVE消息。 禁用程序后,发送消息实际上并没有激活该窗口,但是程序认为它是活动的。 (实际上成功了。) 但是,我认为定期发送后期邮件效率很低。 如果我想检查程序的WM_ACTIVE值并将其禁用,我尝试使用POSTMESSAGE API再次发送WM_ACTIVE消息,以使程序本身与活动无关,但是我想不出办法。尽管有一个挂钩很容易使用的想法,但是C#不支持除键盘和鼠标之外的其他类型的全局挂钩。 任何人都可以提出其他想法吗?请帮助我。

1 个答案:

答案 0 :(得分:0)

要检查某个流程是否为焦点,应使用GetForegroundWindow获取焦点的窗口句柄,然后使用GetWindowThreadProcessId从该窗口句柄获取该过程:

    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    static extern uint GetWindowThreadProcessId(int hWnd, out int ProcessId);

    IntPtr focusedWindow = GetForegroundWindow(); //get the focused window
    int focusedProcessID = 0;
    GetWindowThreadProcessID(focusedWindow, out focusedProcessID); //get it's process id
    Process focusedProcess = Process.GetProcessById(focusedProcessID);//get the focused process
    Console.WriteLine("Current Focused Process:" + focusedProcess.ProcessName);
相关问题