检测任务栏图标闪烁 [英] Detect a taskbar icon flashing

查看:93
本文介绍了检测任务栏图标闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个脚本,以检测任务栏图标何时闪烁并激活程序.我想使用AutoIt或Windows API.

I want to make a script that detects when taskbar icon flashes, and activates a program. I would like to use AutoIt or the Windows API.

如何检测程序的任务栏图标何时开始闪烁?

How to detect when a program's taskbar icon starts flashing?

推荐答案

要直接回答您的问题,没有简单(证明可靠的)方法可以检测到窗口的闪烁.它是由于 FlashWindow而导致的/ FlashWindowEx .一个非常麻烦且繁琐的选择是对这两个API进行全局挂钩.您可以通过向每个用户模式应用程序注入DLL并执行本地挂接/绕行操作来通知您拥有的某些中央可执行文件来实现此目的.

To answer your question directly, there is no easy (documented and reliable) way to detect the flashing of the window. It occurs as a result of FlashWindow/FlashWindowEx. A very intrusive and heavy-handed option is to perform global hooking of both APIs. You could do this by injecting a DLL to every usermode application and performing a local hook/detour which notifies some central executable you own.

但是,您的建议存在一个更大的潜在问题,这使其极为不可取.想象一个没有焦点的应用程序不断闪烁.您的应用会将其设置为前台.如果有两个这样的应用程序,会发生什么情况? ?

However, there is a greater underlying problem with what you are proposing, which makes it extremely undesirable. Imagine an application which constantly flashes when it does not have focus. Your app would set it to the foreground. What would happen if there were two such applications?

按照Raymond的建议使用WH_SHELL钩子并不是太困难,可以通过调用

Using a WH_SHELL hook as Raymond suggests is not too difficult and is done by calling SetWindowsHookEx as so:

SetWindowsHookEx(WH_SHELL, hook_proc, NULL, dwPID);

这将HOOKPROC设置为hook_proc的shell钩子,而dwPID是我们要与该钩子关联的线程.既然您提到您已经知道要定位的程序,那么我假设您已经对该窗口拥有HWND了.您需要生成dwPID,可以这样做:

This sets a shell hook with the HOOKPROC as hook_proc and dwPID is the thread which we want to associate the hook with. Since you mention that you already know which program you want to target, I'll assume you have a HWND to that window already. You need to generate the dwPID, which can be done as so:

DWORD dwID = GetWindowThreadProcessId(hwnd, NULL)

这将用HWND的关联PID填充dwPID.对于下一步,我假设挂钩过程位于当前可执行文件中,而不是DLL中.挂钩过程可能是这样的:

This will populate dwPID with the associated PID of the HWND. For the next step, I assume the hook procedure to be in the current executable as opposed to a DLL. The hook procedure might be something like this:

LRESULT CALLBACK hook_proc(int nCode, WPARAM wParam, LPARAM lParam) {
  if (nCode == HSHELL_REDRAW && lParam){
    SetForegroundWindow(hwnd); // assumed hwnd is a global
  }
  return CallNextHookEx(NULL, nCode, wParam, lParam);
}

上面的代码尚未经过测试,可能包含错误,但应该使您大致了解该怎么做.

The code above has not been tested and might contain mistakes but should give you a general idea of what to do.

与窗口挂钩有关的重要一件事是,必须从具有与目标相同的位的程序中调用SetWindowHookEx.也就是说,如果您的目标是64位,则SetWindowHookEx的调用方也必须是64位.另外,完成之后,您应该使用

One important thing to note with window hooks is that SetWindowHookEx must be called from a program with the same bitiness as the target. i.e. if your target is 64 bit, the caller of SetWindowHookEx must also be 64 bit. Also, after you are done, you should cleanup by removing your hook with UnhookWindowsHookEx.

这篇关于检测任务栏图标闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆