HideWindow() ?

Just starting out? Need help? Post your questions and find answers here.
Axolotl
Addict
Addict
Posts: 872
Joined: Wed Dec 31, 2008 3:36 pm

HideWindow() ?

Post 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
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: HideWindow() ?

Post by infratec »

I think it's a bug in the windows PB version.
SMaag
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: HideWindow() ?

Post 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.
AZJIO
Addict
Addict
Posts: 2225
Joined: Sun May 14, 2017 1:48 am

Re: HideWindow() ?

Post 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 
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: HideWindow() ?

Post 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: .
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: HideWindow() ?

Post 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
Egypt my love
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: HideWindow() ?

Post by boddhi »

RASHAD wrote: Workaround for Windows
Excellent!!! :wink:
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: HideWindow() ?

Post 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

Egypt my love
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: HideWindow() ?

Post 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.
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
Axolotl
Addict
Addict
Posts: 872
Joined: Wed Dec 31, 2008 3:36 pm

Re: HideWindow() ?

Post 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.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: HideWindow() ?

Post 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.
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
Post Reply