CORRECTION:
TI-994A wrote:...Even with this simple routine,
the window pops right up to the front and gets the focus, regardless of what application window may be in the foreground; no flashing whatsoever.
Code: Select all
wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, 50, 50, 300, 100, "Set Foreground Test", wFlags)
Delay(3000)
SetForegroundWindow_(WindowID(0))
While WaitWindowEvent() ! #PB_Event_CloseWindow : CloseWindow : Wend
After further testing, I've noticed that
the above example works only with a short delay, nothing more than Delay(5000); any longer than that and we get only the flashing taskbar button. Perhaps the delay maintains the window's foreground status for a while, afterwhich it falls to the background.
Hello again, Danilo! Thank you very much for your tireless help and elaborate examples; really great codes. And thanks for pointing out the SwitchToThisWindow() function, although I was not able to get it to work; upon closing the Calculator in your example, the "main" window only flashes in the taskbar.
In any case, thanks to yours and RASHAD's wonderful examples, I have pieced together this short routine, for the simple and express purpose of making an application bring itself to the foreground:
Code: Select all
;========================================
;
; A workaround to SetForegroundWindow()
; adapted from routines by PureBasic
; experts, RASHAD & Danilo. Thank you!
;
; by TI-994A - 7th April, 2012
;
;========================================
Enumeration
#MainWindow
#sfwTimer
EndEnumeration
Global.i wSFW, tSFW, mSFW = RegisterWindowMessage_("mySetForegroundWindow")
Procedure SetMeForeground()
PostMessage_(WindowID(wSFW), mSFW, 0, 0)
EndProcedure
Procedure pSFW(hWndParent)
wSFW = OpenWindow(#PB_Any, 0, 0, 0, 0, "", #PB_Window_Invisible, hWndParent)
Repeat
Select WaitWindowEvent()
Case mSFW
BringWindowToTop_(hWndParent)
EndSelect
ForEver
EndProcedure
wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(#MainWindow, #PB_Any, #PB_Any, 400, 200, "SetForegroundWindow Workaround", wFlags)
tSFW = CreateThread(@pSFW(), WindowID(#MainWindow))
AddWindowTimer(#MainWindow, #sfwTimer, 5000)
Repeat
Select WaitWindowEvent()
Case #PB_Event_Timer
SetMeForeground()
Case #PB_Event_CloseWindow
appQuit = 1
KillThread(tSFW)
EndSelect
Until appQuit = 1
As a demonstration, this example uses a timer to bring the main window to the front every five seconds. So far, it's worked well on my machines, but it would be great to know if it works across a broader range.
Please do let me have some feedback if you test it. Thank you.