【问题标题】:is there easy way to check some process is activated?有没有简单的方法来检查某些进程是否被激活?
【发布时间】:2020-02-17 05:42:57
【问题描述】:

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

【问题讨论】:

  • 你只是想把焦点放在一个进程上吗?
  • 我认为我不需要知道这个过程是否有重点。但是....我认为这可能会有所帮助。有什么方法可以确保它的重点?
  • 我会参考的。谢谢。

标签: c# hook postmessage


【解决方案1】:

要检查进程是否获得焦点,您应该使用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);

【讨论】:

    猜你喜欢
    • 2012-04-10
    • 2021-06-27
    • 1970-01-01
    • 2014-01-05
    • 2011-08-08
    • 2016-10-03
    • 2015-07-27
    • 1970-01-01
    • 2015-06-19
    相关资源
    最近更新 更多