gpt4 book ai didi

winapi - 允许后台应用设置不同进程的前台窗口

转载 作者:行者123 更新时间:2023-12-03 17:50:09 25 4
gpt4 key购买 nike

首先,我知道这是一个有争议的讨论,但我希望我们可以保持这个技术性。

我有一个在后台启动的应用程序,我希望它能够在不同的进程中激活/聚焦窗口。但是,即使我要激活其窗口的进程调用了 AllowSetForegroundWindow(ASFW_ANY),调用 SetForegroundWindow 也总是会失败。

原因是 (IMO) 启动应用程序是一个后台进程,并且由于它尚未收到输入,因此不允许设置前台窗口。所以一切都出现在任务列表中,但没有显示。

所以我尝试创建一个虚拟窗口来接收立即关闭的输入和 事后能够成功调用 SetForegroundWindow。但即使是我显示的虚拟窗口也显示在背景中。

然而 ,如果我打电话

AttachThreadInput(
GetWindowThreadProcessId(GetForegroundWindow(), NULL),
GetCurrentThreadId(), TRUE);

在创建虚拟窗口之前,该窗口确实是在前台创建的,我可以 事后在不同的工作进程中为不同的 HWND 调用 SetForegroundWindow。

但是:如果我不创建虚拟窗口,尽管我使用了 AttachThreadInput,SetForegroundWindow 仍然返回零。

我不明白为什么如果我创建自己的窗口(然后成功创建其他窗口),AttachThreadInput hack 为什么会成功,但如果我不先创建自己的窗口,则不成功。

我的后台进程如何在不同进程的不同窗口上调用 SetForegroundWindow 没有 创建一个虚拟窗口?

[*] 后台应用程序实际上是 gpg-agent.exe,它在需要密码时调用 pinentry.exe(我的应用程序)。 pinentry.exe(作为后台进程运行)必须从另一个正在运行的应用程序请求密码,因此它必须将其窗口置于前台......

最佳答案

documentationAttachThreadInput()给你一个线索:

Windows created in different threads typically process input independently of each other. That is, they have their own input states (focus, active, capture windows, key state, queue status, and so on), and their input processing is not synchronized with the input processing of other threads. By using the AttachThreadInput function, a thread can attach its input processing mechanism to another thread. Keyboard and mouse events received by both threads are processed by the thread specified by the idAttachTo parameter until the threads are detached by calling AttachThreadInput a second time and specifying FALSE for the fAttach parameter. This also allows threads to share their input states, so they can call the SetFocus function to set the keyboard focus to a window of a different thread. This also allows threads to get key-state information.



documentationSetForegroundWindow()告诉您调用进程需要满足哪些条件才能设置前台窗口:

The system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true:

The process is the foreground process.
The process was started by the foreground process.
The process received the last input event.
There is no foreground process.
The process is being debugged.
The foreground process is not a Modern Application or the Start Screen.
The foreground is not locked (see LockSetForegroundWindow).
The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
No menus are active.



简单地自己创建一个虚拟窗口并不能保证你会成为前台进程。将窗口的线程附加到当前前台窗口的线程允许您共享其输入状态,如果前台线程有权这样做,这将使您有更好的机会获得设置前台窗口的权限。

关于winapi - 允许后台应用设置不同进程的前台窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23715026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com