Page 1 of 1
State: Window Visible or not (user can see the window)
Posted: Thu May 04, 2017 6:26 am
by Amnesty
Good morning,
maybe its a stupid question, but how can I found out, whether a window is visible or not.
Visible = The user can see the window its not hidden or behind another window.
I would like to open another small alert window, if the User cant see the Main Window.
Thank you in advance for your help
Amnesty
Re: State: Window Visible or not (user can see the window)
Posted: Thu May 04, 2017 9:49 am
by chi
Something like this?
Windows onlyCode: Select all
EnableExplicit
Procedure ForceWindowIntoForeground(hWnd)
Protected currentThread, activeWindow, activeProcess, activeThread, windowProcess, windowThread, oldTimeout, newTimeout
#LSFW_UNLOCK = 2 : #ASFW_ANY = -1
currentThread = GetCurrentThreadId_();
activeWindow = GetForegroundWindow_();
activeThread = GetWindowThreadProcessId_(activeWindow, @activeProcess);
windowThread = GetWindowThreadProcessId_(hWnd, @windowProcess);
If currentThread <> activeThread
AttachThreadInput_(currentThread, activeThread, #True)
EndIf
If windowThread <> currentThread
AttachThreadInput_(windowThread, currentThread, #True)
EndIf
oldTimeout = 0
newTimeout = 0
SystemParametersInfo_(#SPI_GETFOREGROUNDLOCKTIMEOUT, 0, @oldTimeout, 0);
SystemParametersInfo_(#SPI_SETFOREGROUNDLOCKTIMEOUT, 0, @newTimeout, 0);
LockSetForegroundWindow_(#LSFW_UNLOCK);
AllowSetForegroundWindow_(#ASFW_ANY);
SetForegroundWindow_(hWnd);
SystemParametersInfo_(#SPI_SETFOREGROUNDLOCKTIMEOUT, 0, @oldTimeout, 0);
If currentThread <> activeThread
AttachThreadInput_(currentThread, activeThread, #False)
EndIf
If windowThread <> currentThread
AttachThreadInput_(windowThread, currentThread, #False)
EndIf
EndProcedure
Procedure IsWindowPartiallyCovered(hWnd)
Protected.RECT wRect, tmpRect
GetWindowRect_(hWnd, wRect)
Repeat
hWnd = GetWindow_(hWnd, #GW_HWNDPREV)
If IsWindowVisible_(hWnd) And GetWindowRect_(hWnd, tmpRect) And IntersectRect_(tmpRect, wRect, tmpRect)
ProcedureReturn #True
EndIf
Until hWnd = 0
ProcedureReturn #False
EndProcedure
OpenWindow(0, 0, 0, 320, 200, "", #WS_OVERLAPPEDWINDOW|#PB_Window_ScreenCentered)
AddWindowTimer(0, 0, 3000)
Repeat
Define event = WaitWindowEvent()
Select event
Case #PB_Event_Timer
Select EventTimer()
Case 0
If IsWindowPartiallyCovered(WindowID(0))
ForceWindowIntoForeground(WindowID(0))
EndIf
EndSelect
EndSelect
Until event = #PB_Event_CloseWindow
Re: State: Window Visible or not (user can see the window)
Posted: Thu May 04, 2017 11:40 am
by Marc56us
Code: Select all
; Monitor system
; Marc56us - 2017/05/04
; Check every 5 sec if the windows is activated
OpenWindow(0, 0, 0, 500, 300, "Monitor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
AddWindowTimer(0, 2, 5000)
Procedure CheckStillOnTop()
If GetActiveWindow() = -1
; I don't know why, but SetActiveWindow(0) does not work here ?
; so i use an alternate solution:
SetWindowState(0, #PB_Window_Minimize)
SetWindowState(0, #PB_Window_Normal)
EndIf
EndProcedure
BindEvent(#PB_Event_Timer, @CheckStillOnTop())
While WaitWindowEvent() <> #PB_Event_CloseWindow : Wend
Without SetWindowState(... bindEvent continue to think GetActiveWindow = -1 (why ?)

Re: State: Window Visible or not (user can see the window)
Posted: Thu May 04, 2017 12:31 pm
by RASHAD
Work with windows created by PB
Code: Select all
OpenWindow(0, 0, 0, 320, 200, "", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
ButtonGadget(0,10,170,60,20,"TEST")
OpenWindow(1, 100, 100, 300, 200, "", #PB_Window_SystemMenu)
HideWindow(1,1)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Quit = 1
Case #PB_Event_Gadget
Select EventGadget()
Case 0
StickyWindow(1,1)
StartDrawing(WindowOutput(1))
Delay(50)
color = Point(5,5)
If color > 0
Debug "Window #1 is visible"
Else
Debug "Window #1 is hidden"
EndIf
StopDrawing()
Delay(1000)
HideWindow(1,0)
StartDrawing(WindowOutput(1))
Delay(50)
color = Point(5,5)
If color > 0
Debug "Window #1 is visible"
Else
Debug "Window #1 is hidden"
EndIf
StopDrawing()
EndSelect
EndSelect
Until Quit = 1
Re: State: Window Visible or not (user can see the window)
Posted: Thu May 04, 2017 12:55 pm
by Amnesty
Servus!
The Hint from Chi works fine, thank you all.
Amnesty
Re: State: Window Visible or not (user can see the window)
Posted: Wed Mar 02, 2022 1:06 pm
by BarryG
Great tip, Chi! Can't believe I missed it 5 years ago.
Re: State: Window Visible or not (user can see the window)
Posted: Wed Mar 02, 2022 3:14 pm
by RASHAD
Keep your window visible
Very simple just few lines of code
Code: Select all
OpenWindow(0, 100, 100, 300, 200, "", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
AddWindowTimer(0,125,5000)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Quit = 1
Case #PB_Event_Timer
If GetForegroundWindow_() <> WindowID(0)
SendMessage_(#HWND_BROADCAST, #WM_SYSCOMMAND, #SC_HOTKEY, WindowID(0))
EndIf
EndSelect
Until Quit = 1