問(wèn)題描述
我正在實(shí)現(xiàn)一個(gè)任務(wù)欄替換、類(lèi)似停靠欄的應(yīng)用程序切換器風(fēng)格的程序.它使用 OpenGL 和鍵盤(pán)快捷鍵做了一些獨(dú)特的事情,所以它的設(shè)置方式,窗口并不總是有焦點(diǎn).我想實(shí)現(xiàn)它,以便我可以將任意窗口帶到前臺(tái),就像任務(wù)欄或 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.
但是,我的代碼只是使應(yīng)用程序圖標(biāo)在任務(wù)欄中閃爍.Windows API 文檔說(shuō)這是應(yīng)該發(fā)生的事情,但我正在尋找一種方法來(lái)解決這個(gè)問(wèn)題.
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.
我從以下示例中改編了我的代碼,其中說(shuō)明附加到前臺(tái)線程應(yīng)該允許您設(shè)置前臺(tái)窗口.以下是網(wǎng)站:
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
我的代碼看起來(lái)像這樣.請(qǐng)注意,它使用的是 python 的 win32 包裝器(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)
然而,除非我的窗口是前臺(tái)窗口(通常不是),否則這只會(huì)導(dǎo)致程序的圖標(biāo)閃爍.
However, unless my window is the foreground window (which it isn't usually), this just causes the program's icon to flash.
我是不是把線貼錯(cuò)了?有沒(méi)有另一種方法可以解決這個(gè)問(wèn)題?我想一定有,因?yàn)橛泻芏鄳?yīng)用程序切換器似乎能夠很好地做到這一點(diǎn).
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 寫(xiě)的,但如果有其他語(yǔ)言的解決方案,我將使用包裝器或做任何必要的事情來(lái)啟動(dòng)和運(yùn)行.
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.
提前致謝!
我愿意接受一種使其僅在我的特定計(jì)算機(jī)上工作的方法,即一種在我的機(jī)器上啟用任何應(yīng)用程序獲得焦點(diǎn)的方法.
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.
推薦答案
我有一些已經(jīng)運(yùn)行多年的代碼,一直回??到 Windows 95.雙擊應(yīng)用程序系統(tǒng)托盤(pán)圖標(biāo)時(shí),我總是使用 Win32諸如BringWindowToTop 和SetForegroundWindow 之類(lèi)的API 函數(shù)將我的應(yīng)用程序窗口帶到前臺(tái).這一切在 Windows 7 上都停止了,我的輸入窗口最終會(huì)落后于其他窗口,并且窗口圖標(biāo)會(huì)在狀態(tài)欄上閃爍.我想出的解決方法"是這樣的;它似乎適用于所有版本的 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);
這篇關(guān)于Windows 7:無(wú)論其他窗口有焦點(diǎn),如何將窗口置于最前面?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!