restore window & give it focus.

Just starting out? Need help? Post your questions and find answers here.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

restore window & give it focus.

Post 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
infratec
Always Here
Always Here
Posts: 7665
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: restore window & give it focus.

Post 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)
Comfort
User
User
Posts: 32
Joined: Thu Jul 05, 2018 11:52 pm

Re: restore window & give it focus.

Post by Comfort »

Replacing line #27 = ShowWindow() with...

Code: Select all

            SendMessage_(WindowID(0), #WM_SYSCOMMAND, #SC_HOTKEY, WindowID(0))
should work.
User avatar
JHPJHP
Addict
Addict
Posts: 2272
Joined: Sat Oct 09, 2010 3:47 am

Re: restore window & give it focus.

Post 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

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: restore window & give it focus.

Post by jassing »

Thanks! both suggestions worked. Thank you!
Post Reply