窃取焦点,因为 SetForegroundWindow 无法做到 [英] Steal focus as SetForegroundWindow can't do it
问题描述
我知道这听起来很邪恶,但我的本意根本不是这个.
用户单击延迟拍摄"并开始倒计时,在此期间他们将焦点放在另一个应用程序上,然后在倒计时后用户希望我的应用程序重新获得焦点.
SetForegroundWindow
当它从具有 PID X 的应用程序运行而具有 PID Y 的应用程序被聚焦时失败.
这使我的窗口向前移动,但它没有获得焦点:
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
它将窗口设置为始终在顶部.但是焦点没有放在我的窗口中.然后将其设置回 HWND_NOTOPMOST
将其留在那里,即使它不是最重要的,但它仍然没有焦点.在对 SetWindowPos
的这些调用之后,运行 SetForegroundWindow
不会成功
我尝试了以下组合:
SwitchToThisWindow
SetWindowPos
ShowWindow
SetForegroundWindow
但似乎无法弄清楚.
我认为这必须成为可能的另一个原因是 Mac OSX 允许使用 NSApplication activateIgnoringOtherApps:
并且 Linux 也允许它.我想要这个跨平台.
作为最后的手段,我将使用 HWND_TOPMOST
方法,然后使用 GetWindowRect
然后 SetCursorPos
并在 pos 处发送单击到我的窗口0,0,我真的不想,因为可能在 0,0 处有一些东西,它不应该被自动点击.
这总是有效的,无论什么应用程序在前台(如任务管理器等):
AllocConsole();自动 hWndConsole = GetConsoleWindow();SetWindowPos(hWndConsole, 0, 0, 0, 0, 0, SWP_NOZORDER);自由控制台();SetForegroundWindow(hWnd);
I know this sounds evil but my intention is not this at all.
A user has clicked "delay shot" and a countdown starts, during that time they go focus another application, then after countdown user expects my app to take back focus.
SetForegroundWindow
fails when it runs from appliction with PID X while application with PID Y is focused.
This worked to bring my window forward, but it doesn't get focus:
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
It sets the window to be always on top. However focus is not put into my window. And then setting it back to HWND_NOTOPMOST
leaves it there, even though its not top most, but it still doesnt have focus. Running SetForegroundWindow
won't do the trick after these calls to SetWindowPos
Is there anyway to steal focus?
I have tried a combination of:
SwitchToThisWindow
SetWindowPos
ShowWindow
SetForegroundWindow
But can't seem to figure it out.
Another reason I think this has to be possible is because Mac OSX allows it with NSApplication activateIgnoringOtherApps:
and Linux's allow it too. I'd like to get this cross platform.
As a last resort I was going to use the HWND_TOPMOST
method, then GetWindowRect
then SetCursorPos
and send single click to my window at pos 0,0, I really don't want to though as there might be something at 0,0 and it shouldn't be auto clicked on.
This always works, no matter what app is in foreground (like Task Manager, etc.):
AllocConsole();
auto hWndConsole = GetConsoleWindow();
SetWindowPos(hWndConsole, 0, 0, 0, 0, 0, SWP_NOZORDER);
FreeConsole();
SetForegroundWindow(hWnd);
这篇关于窃取焦点,因为 SetForegroundWindow 无法做到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!