Page 1 of 1

HideWindow() ?

Posted: Wed Jul 17, 2024 5:24 pm
by Axolotl
Sorry, :oops:
Help Text: The window is automatically activated (gets the focus),
Expectation: I click outside the window, the window hides and shows up after to seconds and I can click outside again to repeat.......
This works on Linux as expected, doesn't work on Windows.
=> Different OS behavior.
=> Maybe I should stop comparing the two OSs with each other?

Code: Select all

Macro RStr(Value, Length) 
  RSet(Str(Value), Length) 
EndMacro 

If OpenWindow(0, 0, 0, 320, 120, "Hide Window for a while ...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StickyWindow(0, 1) 

  TextGadget(1, 10, 10, 300, 20, "")
  TextGadget(2, 10, 40, 300, 20, "")
; StringGadget(3, 10, 70, 300, 20, "") ; <-- not helping 

   AddWindowTimer(0, 1, 100) 
   AddWindowTimer(0, 2, 2000)     ; hide the window for 2 s 
   Repeat
     Event = WaitWindowEvent()
     Select Event
       Case #PB_Event_DeactivateWindow  
        HideWindow(0, 1) 
        ; Break 

       Case #PB_Event_Timer 
        If EventTimer() = 1 
          mx = DesktopMouseX() 
          my = DesktopMouseY() 
          SetGadgetText(1, "Desktop Mouse Pos: " + RStr(mx, 4) + ", " + RStr(my, 4)) 

          mx = WindowMouseX(0) 
          my = WindowMouseY(0) 
          SetGadgetText(2, "Window Mouse Pos: " + RStr(mx, 4) + ", " + RStr(my, 4)) 
        EndIf 

        If EventTimer() = 2
          HideWindow(0, 0) 
          ; SetActiveWindow(0)  ; <-- not needed on linux, no effect on window  
          ; SetActiveGadget(2)  ; <-- not working on window 
        EndIf 

    EndSelect
  Until Event = #PB_Event_CloseWindow
EndIf

Re: HideWindow() ?

Posted: Wed Jul 17, 2024 7:45 pm
by infratec
I think it's a bug in the windows PB version.

Re: HideWindow() ?

Posted: Wed Jul 17, 2024 8:22 pm
by SMaag
It works on Windows 10 PB6.11 x64 as long as the Window of the program has the focus.
If I click first on an other windows like editor, the editor gets the focus. Now a click on the desktop do not hide the window.
But in my opinion this correct.

Re: HideWindow() ?

Posted: Wed Jul 17, 2024 8:35 pm
by AZJIO
Axolotl wrote: Wed Jul 17, 2024 5:24 pm

Code: Select all

 ; SetActiveGadget(2)  ; <-- not working on window 
SetActiveWindow()
Remarks
The function will only change the focus within the program. It can not bring the program to the foreground when another program has the focus.
https://www.purebasic.fr/english/viewto ... 09#p492909

Code: Select all

; make the window active.
CompilerSelect #PB_Compiler_OS
	CompilerCase #PB_OS_Windows
		SetWindowFocus(#Window_Main)
		; SetForegroundWindow_(WindowID(#Window_Main))
	CompilerCase #PB_OS_Linux
		StickyWindow(#Window_Main, #True)
		StickyWindow(#Window_Main, #False)
CompilerEndSelect
I don't remember where I got it (https://www.purebasic.fr/english/viewtopic.php?t=7424)

Code: Select all

Procedure SetForegroundWindow(hWnd)
	Protected foregroundThreadID, ourThreadID
	If GetWindowLong_(hWnd, #GWL_STYLE) & #WS_MINIMIZE
		; 		ShowWindow_(hWnd, #SW_MAXIMIZE)
		ShowWindow_(hWnd, #SW_SHOWNOACTIVATE)
		UpdateWindow_(hWnd)
	EndIf
	foregroundThreadID = GetWindowThreadProcessId_(GetForegroundWindow_(), 0)
	ourThreadID = GetCurrentThreadId_()
	
	If (foregroundThreadID <> ourThreadID)
		AttachThreadInput_(foregroundThreadID, ourThreadID, #True);
	EndIf
	
	SetForegroundWindow_(hWnd)
	
	If (foregroundThreadID <> ourThreadID)
		AttachThreadInput_(foregroundThreadID, ourThreadID, #False)
	EndIf   
	
	InvalidateRect_(hWnd, #Null, #True)
EndProcedure 

Re: HideWindow() ?

Posted: Wed Jul 17, 2024 9:39 pm
by boddhi
Axolotl wrote:

Code: Select all

 ; SetActiveGadget(2)  ; <-- not working on window 
Maybe, this thread could help to activate window on Windows and this one more specificly.

Sorry, I'm not enough familiar with threads to adapt it to your needs but some PB masters perhaps :mrgreen: .

Re: HideWindow() ?

Posted: Wed Jul 17, 2024 11:32 pm
by RASHAD
Workaround for Windows

Code: Select all

Macro RStr(Value, Length) 
  RSet(Str(Value), Length) 
EndMacro 

Global p.POINT

If OpenWindow(0, 0, 0, 320, 120, "Hide Window for a while ...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StickyWindow(0, 1) 
  TextGadget(1, 10, 10, 300, 20, "")
  TextGadget(2, 10, 40, 300, 20, "")
  ; StringGadget(3, 10, 70, 300, 20, "") ; <-- not helping 
  
  AddWindowTimer(0, 1, 100) 
  
  Repeat
    Event = WaitWindowEvent()
    Select Event
      Case #PB_Event_DeactivateWindow  
        HideWindow(0, 1) 
        AddWindowTimer(0, 2, 2000)         
        
      Case #PB_Event_Timer 
        If EventTimer() = 1 
          mx = DesktopMouseX() 
          my = DesktopMouseY() 
          SetGadgetText(1, "Desktop Mouse Pos: " + RStr(mx, 4) + ", " + RStr(my, 4)) 
          
          mx = WindowMouseX(0) 
          my = WindowMouseY(0) 
          SetGadgetText(2, "Window Mouse Pos: " + RStr(mx, 4) + ", " + RStr(my, 4)) 
        EndIf 
        
        If EventTimer() = 2        
          HideWindow(0, 0) 
          GetCursorPos_(p.POINT)
          GetWindowRect_(WindowID(0),r.RECT)
          SetCursorPos_((r\left+50),(r\top+5))               
          mouse_event_(#MOUSEEVENTF_LEFTDOWN,0,0,0,0)
          mouse_event_(#MOUSEEVENTF_LEFTUP,0,0,0,0)
          SetCursorPos_(p\x,p\y) 
          RemoveWindowTimer(0,2)
        EndIf 
        
    EndSelect
  Until Event = #PB_Event_CloseWindow
EndIf

Re: HideWindow() ?

Posted: Thu Jul 18, 2024 12:29 am
by boddhi
RASHAD wrote: Workaround for Windows
Excellent!!! :wink:

Re: HideWindow() ?

Posted: Thu Jul 18, 2024 9:44 am
by RASHAD
Thanks boddhi
Time to simplify things

Code: Select all

Macro RStr(Value, Length) 
  RSet(Str(Value), Length) 
EndMacro 

Global p.POINT

If OpenWindow(0, 0, 0, 320, 120, "Hide Window for a while ...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StickyWindow(0, 1) 
  TextGadget(1, 10, 10, 300, 20, "")
  TextGadget(2, 10, 40, 300, 20, "")
  ; StringGadget(3, 10, 70, 300, 20, "") ; <-- not helping 
  
  AddWindowTimer(0, 1, 100) 
  active = 1 
  Repeat
    Event = WaitWindowEvent()
    Select Event
      Case #PB_Event_DeactivateWindow  
        HideWindow(0, 1) 
        active = 0       
        
      Case #PB_Event_Timer 
        If active = 1
          start = ElapsedMilliseconds() 
          mx = DesktopMouseX() 
          my = DesktopMouseY() 
          SetGadgetText(1, "Desktop Mouse Pos: " + RStr(mx, 4) + ", " + RStr(my, 4)) 
          
          mx = WindowMouseX(0) 
          my = WindowMouseY(0) 
          SetGadgetText(2, "Window Mouse Pos: " + RStr(mx, 4) + ", " + RStr(my, 4)) 
        ElseIf  active = 0  And  ((ElapsedMilliseconds() - start) >= 2000)
          active = 1
          HideWindow(0, 0) 
          GetCursorPos_(p.POINT)
          GetWindowRect_(WindowID(0),r.RECT)
          SetCursorPos_((r\left+50),(r\top+5))               
          mouse_event_(#MOUSEEVENTF_LEFTDOWN,0,0,0,0)
          mouse_event_(#MOUSEEVENTF_LEFTUP,0,0,0,0)
          SetCursorPos_(p\x,p\y)           
        EndIf 
        
    EndSelect
  Until Event = #PB_Event_CloseWindow
EndIf


Re: HideWindow() ?

Posted: Thu Jul 18, 2024 11:54 am
by boddhi
RASHAD wrote: Thu Jul 18, 2024 9:44 am Thanks boddhi
I sometimes have this problem of window reactivation, especially when several windows are open in modal mode and another program (like antivirus to authorize a port connection) takes over. SetActiveWindow(), SetActiveWindow_(), ForegroundWindow_() and/or combined with SetWindowPos_() have never worked.

Re: HideWindow() ?

Posted: Thu Jul 18, 2024 2:00 pm
by Axolotl
Hello and thank you guys,
I hope I didn't make you waste your time with this. :oops:
I just wanted to share my observations (differences between Windows and Linux) with you.
So thanks again for the great suggestions.

Re: HideWindow() ?

Posted: Thu Jul 18, 2024 3:26 pm
by boddhi
On Windows, this is an old subject.
Although it's in the form of a hack, it's certainly an unofficial but short, efficient solution.