Windows 7:无论其他哪个窗口都具有焦点,如何将一个窗口置于最前面? [英] Windows 7: how to bring a window to the front no matter what other window has focus?

查看:361
本文介绍了Windows 7:无论其他哪个窗口都具有焦点,如何将一个窗口置于最前面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现任务栏替换,类似码头的应用程序切换器风格的程序.它使用OpenGL和键盘快捷键来做一些独特的事情,因此在设置方式上,窗口并不总是具有焦点.我想要实现它,以便可以将任意窗口带到前台,就像任务栏或ALT-TAB程序一样.

I'm implementing a task-bar replacement, dock-like application-switcher style program. It's doing some unique stuff with OpenGL, and with keyboard shortcuts, so the way it's set up, the window doesn't always have focus. I'd like to implement it such that I can bring an arbitrary window to the foreground, much like a taskbar or an ALT-TAB program would.

但是,我的代码只是使应用程序图标在任务栏中闪烁. Windows API文档说这是应该发生的,但是我正在寻找一种解决此问题的方法.

However, my code simply causes the application icon to flash in the taskbar. The Windows API documentation says that this is what is supposed to happen, but I'm looking for a way to work around this.

我已经从以下示例改编了我的代码,这些示例说附加到前台线程应允许您设置前台窗口.这些是这些网站:

I've adapted my code from the following examples, which say that attaching to the foreground thread should allow you to set the foreground window. Here are the sites:

http://www.voidnish.com/Articles/ShowArticle.aspx? code = dlgboxtricks

http://invers2008. blogspot.com/2008/10/mfc-how-to-steal-focus-on-2kxp.html

我的代码如下所示.请注意,它使用的是win32封装的python(self.hwnd是我想放在最前面的窗口的句柄):

My code looks like this. Note that it's using the win32 wrappers for python (self.hwnd is the handle of the window I want to bring to the front):

fgwin = win32gui.GetForegroundWindow()
fg = win32process.GetWindowThreadProcessId(fgwin)[0]
current = win32api.GetCurrentThreadId()
if current != fg:
    win32process.AttachThreadInput(fg, current, True)
    win32gui.SetForegroundWindow(self.hwnd)
    win32process.AttachThreadInput(fg, win32api.GetCurrentThreadId(), False)

但是,除非我的窗口是前景窗口(通常不是),否则这只会导致程序的图标闪烁.

However, unless my window is the foreground window (which it isn't usually), this just causes the program's icon to flash.

我执行的线程连接错误吗?有另一种方法可以解决此问题?我认为一定有,因为那里有许多应用程序切换器似乎可以做到这一点.

Am I doing the thread attaching wrong? Is there another way to work around this? I figure there must be, as there are lots of application switchers out there that seem to be able to do this just fine.

我正在用python编写代码,但是如果有另一种语言的解决方案,我将使用包装器或执行任何必要的操作来启动和运行它.

I'm writing this in python, but if there is a solution in another language I will use wrappers or do whatever is necessarry to get this up and running.

提前谢谢!

编辑:我愿意接受一种使其只能在我的特定计算机上运行的方法,即在我的计算机上启用任何应用程序聚焦的方法.

I'd be open to a way to make it work only on my particular computer, i.e. a way to enable, on my machine, a way for any application to take focus.

已解决:您要做的就是禁用前台锁定.事实证明,它是如此简单:

SOLVED: What you have to do is disable the foreground lock. Turns out it was as easy as this:

win32gui.SystemParametersInfo(win32con.SPI_SETFOREGROUNDLOCKTIMEOUT, 0, win32con.SPIF_SENDWININICHANGE | win32con.SPIF_UPDATEINIFILE)

推荐答案

我已经运行了多年的代码,一直回​​到Windows95.双击应用程序系统任务栏图标时,我总是使用Win32诸如BringWindowToTop和SetForegroundWindow之类的API函数将我的应用程序窗口带到前台.所有这些都停止了在Windows 7上的预期工作,在Windows 7中,我的输入窗口将最终位于其他窗口之后,并且窗口图标将在状态栏上闪烁.我想到的解决方法"是这样的;似乎可以在所有Windows版本上使用.

I've had some code that's been running for years, going all the way back to Windows 95. When double clicking the applications system tray icon I always used Win32 API functions such as BringWindowToTop and SetForegroundWindow to bring my application windows to the foreground. This all stopped working as intended on Windows 7, where my input window would end up behind other windows and the window icon would flash on the status bar. The 'work around' that I came up with was this; and it seems to work on all versions of Windows.

//-- show the window as you normally would, and bring window to foreground.
//   for example;
::ShowWindow(hWnd,SW_SHOW); 
::BringWindowToTop(hWnd);
::SetForegroundWindow(hWnd);

//-- on Windows 7, this workaround brings window to top
::SetWindowPos(hWnd,HWND_NOTOPMOST,0,0,0,0, SWP_NOMOVE | SWP_NOSIZE);
::SetWindowPos(hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);
::SetWindowPos(hWnd,HWND_NOTOPMOST,0,0,0,0,SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE);

这篇关于Windows 7:无论其他哪个窗口都具有焦点,如何将一个窗口置于最前面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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