Page 1 of 1

restore window & give it focus.

Posted: Mon Apr 10, 2023 3:59 pm
by jassing
When I double click the systray icon, it restores, but not with focus, unlike when using right-click/restore.
How can I force the window to have focus? (also restoring from code (I use a timer in the example) doesn't give it focus)

Code: Select all

Enumeration
  #Image0
EndEnumeration

If OpenWindow(0, 100, 150, 300, 100, "SysTray Example", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget)   
  TrayIcon = CatchImage(0, ?TrayIcon)
  
  If CreatePopupImageMenu(0) 
    MenuItem(1, "Restore",TrayIcon) 
    MenuItem(2, "Close")
  EndIf 

  Repeat 
    Event = WaitWindowEvent(1000) 
    
    Select Event 
      Case #PB_Event_Timer
        Select EventTimer()
          Case 1
            RemoveWindowTimer(0,1)
            PostEvent(#PB_Event_Menu,0,1)
        EndSelect
        
      Case #PB_Event_Menu
        Select EventMenu()
          Case 1
            ShowWindow_(WindowID(0),#SW_RESTORE) 
            RemoveSysTrayIcon(0)
            HideWindow(0,#False)
            
            ; Required for "double click"
            ;   but doesn't get focus...
            StickyWindow(0,#True)
            SetActiveWindow(0)
            StickyWindow(0,#False)
            
          Case 2
            PostEvent(#PB_Event_CloseWindow)
            
        EndSelect
        
      Case #PB_Event_SysTray 
        Select EventType() 
          Case #PB_EventType_RightClick  
            DisplayPopupMenu(0, WindowID(0)) 
          Case #PB_EventType_LeftDoubleClick
            PostEvent(#PB_Event_Menu,0,1)
        EndSelect
        
      Case #PB_Event_MinimizeWindow
        HideWindow(0,#True )
        AddSysTrayIcon(0, WindowID(0), TrayIcon)
        SysTrayIconToolTip(0, "DBL click or right click to restore") 
        AddWindowTimer(0,1, 10000)
    EndSelect  
  Until Event = #PB_Event_CloseWindow 
EndIf

DataSection
  TrayIcon:
  IncludeBinary #PB_Compiler_Home+"\Examples\Sources - Advanced\MoviePlayer\Icons\Play.ico"
EndDataSection

Re: restore window & give it focus.

Posted: Mon Apr 10, 2023 4:39 pm
by infratec
Try this:

Code: Select all

Select EventMenu()
          Case 1            
            If IsSysTrayIcon(0)
              RemoveSysTrayIcon(0)
            EndIf
            HideWindow(0, #False)
            SetWindowState(0, #PB_Window_Normal)

Re: restore window & give it focus.

Posted: Mon Apr 10, 2023 4:44 pm
by Comfort
Replacing line #27 = ShowWindow() with...

Code: Select all

            SendMessage_(WindowID(0), #WM_SYSCOMMAND, #SC_HOTKEY, WindowID(0))
should work.

Re: restore window & give it focus.

Posted: Mon Apr 10, 2023 5:14 pm
by JHPJHP
Hi jassing,

Something else to try.

Code: Select all

Enumeration
  #Image0
EndEnumeration

If OpenWindow(0, 100, 150, 300, 100, "SysTray Example", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget)   
  TrayIcon = CatchImage(0, ?TrayIcon)
  
  If CreatePopupImageMenu(0) 
    MenuItem(1, "Restore",TrayIcon) 
    MenuItem(2, "Close")
  EndIf 

  Repeat 
    Event = WaitWindowEvent(50) 

    Select Event 
      Case #PB_Event_Menu
        Select EventMenu()
          Case 1
            RemoveSysTrayIcon(0)
            HideWindow(0, #False)
            Delay(100)
            SetWindowState(0, #PB_Window_Normal)
          Case 2
            PostEvent(#PB_Event_CloseWindow)
        EndSelect
      Case #PB_Event_SysTray 
        Select EventType() 
          Case #PB_EventType_RightClick  
            DisplayPopupMenu(0, WindowID(0)) 
          Case #PB_EventType_LeftDoubleClick
            PostEvent(#PB_Event_Menu, 0, 1)
        EndSelect
      Case #PB_Event_MinimizeWindow
        HideWindow(0, #True)
        AddSysTrayIcon(0, WindowID(0), TrayIcon)
        SysTrayIconToolTip(0, "DBL click or right click to restore") 
    EndSelect  
  Until Event = #PB_Event_CloseWindow 
EndIf

DataSection
  TrayIcon:
  IncludeBinary #PB_Compiler_Home+"\Examples\Sources - Advanced\MoviePlayer\Icons\Play.ico"
EndDataSection

Re: restore window & give it focus.

Posted: Mon Apr 10, 2023 5:15 pm
by jassing
Thanks! both suggestions worked. Thank you!