如何向前显示不属于该程序的另一个窗口

发布于 2024-09-26 18:06:40 字数 220 浏览 3 评论 0原文

我真的很难找到一种方法来打开另一个程序窗口。

例如,我使用FindWindow来查找记事本的句柄。然后,我尝试使用 SetWindowPos(hWnd, 0,0, 0, 0, 0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE); 将窗口向前移动。

但它就是行不通! ShowWindow 也没有!

您能帮忙并给我看一段代码吗?

谢谢

I'm really struggling to find a way of bringing forward another programs window.

For example, I use FindWindow to find the handle of Notepad. I then try to bring the window forward using SetWindowPos(hWnd, 0,0, 0, 0, 0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE);

But it just doesnt work!! ShowWindow doesnt either!

Can you please help and maybe show me a snippet of code?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

む无字情书 2024-10-03 18:06:40

不知道这是否是同一件事,但在 Windows 开发的某个时刻,微软添加了一些过于聪明的“反弹出”代码,可以防止没有焦点的程序取消最小化其窗口...相反,程序栏中的窗口条目只会闪烁。也许存在类似的逻辑阻止非前台程序将其窗口向前移动?

无论如何,这里有一些可以尝试的代码,可能有帮助,也可能没有帮助:

 // Check to see if we are the foreground thread
 DWORD foregroundThreadID = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
 DWORD ourThreadID = GetCurrentThreadId();

 // If not, attach our thread's 'input' to the foreground thread's
 if (foregroundThreadID != ourThreadID) AttachThreadInput(foregroundThreadID, ourThreadID, TRUE);

 // Bring our window to the foreground
 SetForegroundWindow(hWnd);

 // If we attached our thread, detach it now
 if (foregroundThreadID != ourThreadID) AttachThreadInput(foregroundThreadID, ourThreadID, FALSE);

 // Force our window to redraw
 InvalidateRect(hWnd, NULL, TRUE);

Dunno if this is the same thing or not, but at some point in Windows development Microsoft added some too-clever-by-half "anti-popup" code that would prevent a program that does not have the focus from un-minimizing its windows... instead, the window's entry in the programs-bar would just blink. Perhaps there is similar logic preventing a non-foreground program from bringing its window forward?

In any case, here is some code to try, that might or might not help:

 // Check to see if we are the foreground thread
 DWORD foregroundThreadID = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
 DWORD ourThreadID = GetCurrentThreadId();

 // If not, attach our thread's 'input' to the foreground thread's
 if (foregroundThreadID != ourThreadID) AttachThreadInput(foregroundThreadID, ourThreadID, TRUE);

 // Bring our window to the foreground
 SetForegroundWindow(hWnd);

 // If we attached our thread, detach it now
 if (foregroundThreadID != ourThreadID) AttachThreadInput(foregroundThreadID, ourThreadID, FALSE);

 // Force our window to redraw
 InvalidateRect(hWnd, NULL, TRUE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文