我正在开发一个自动化程序,WaitForInputIdle
函数帮助我确定目标应用程序的窗口何时完成初始化。问题是,就我而言,它仅适用于第一个窗口 - 这就是 WaitForInputIdle
的工作方式,仅一次。
能否以不同的方式实现WaitForInputIdle
的功能,以便每次目标进程繁忙时都可以调用它,并等待它完成?
我考虑过发布一条虚拟消息,但我认为没有办法知道它何时从队列中删除。
编辑:我想出了一些东西,一个丑陋且古怪的解决方案,但似乎有效:
RECT rc;
if(!GetUpdateRect(hWnd, &rc, FALSE))
{
rc.left = rc.top = 0;
rc.right = rc.bottom = 1;
InvalidateRect(hWnd, &rc, FALSE);
}
do {
Sleep(100);
} while(GetUpdateRect(hWnd, &rc, FALSE));
我真的希望有比这更好的东西。
最佳答案
编辑:按照Raymond Chen的建议发送WM_NULL
对我有用。
看起来PrintWindow
hack没有任何优势,因为它在内部只是发送WM_PAINT
消息。
旧消息:我想出了一个解决我的问题的解决方案,仍然是一个 hack,但没有那么丑陋。
这个想法是滥用PrintWindow
函数,它基本上posts发送一条WM_PAINT消息并等待窗口处理它 - 正是我所需要的。
下面是带有一些信息性注释的代码。
它在 Windows XP 和 Windows 8 上进行了测试,并且按预期工作,即尽管 HDC 值为 NULL,也不会失败。
// BEWARE: HACK BELOW
// PrintWindow is misused here as a synchronization tool
// When calling it, the system sends WM_PAINT and waits for it to be processed
// Note: if hWnd is hung, the following call will hang as well
PrintWindow(hWnd, NULL, 0);
关于winapi - 寻找 WaitForInputIdle 函数的替代方案,随时可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17247346/