State: Window Visible or not (user can see the window)

Just starting out? Need help? Post your questions and find answers here.
Amnesty
User
User
Posts: 54
Joined: Wed Jul 04, 2007 4:34 pm
Location: Germany

State: Window Visible or not (user can see the window)

Post 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
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: State: Window Visible or not (user can see the window)

Post by chi »

Something like this?

Windows only

Code: 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

Et cetera is my worst enemy
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: State: Window Visible or not (user can see the window)

Post 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 ?)

:wink:
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: State: Window Visible or not (user can see the window)

Post 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
Egypt my love
Amnesty
User
User
Posts: 54
Joined: Wed Jul 04, 2007 4:34 pm
Location: Germany

Re: State: Window Visible or not (user can see the window)

Post by Amnesty »

Servus!

The Hint from Chi works fine, thank you all.

Amnesty
BarryG
Addict
Addict
Posts: 4128
Joined: Thu Apr 18, 2019 8:17 am

Re: State: Window Visible or not (user can see the window)

Post by BarryG »

Great tip, Chi! Can't believe I missed it 5 years ago.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: State: Window Visible or not (user can see the window)

Post 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
Egypt my love
Post Reply