Page 1 of 1

Bring (hidden) window to front when clicking systray icon

Posted: Fri Sep 17, 2021 7:55 pm
by blueznl
It's a bit unfair. Some apps (for example Teams) move / reopen their window and give it focus when clicking on their systray icon.

I searched the forum, but haven't found a suitable solution yet. Does anyone have a suggestion how to accomplish this?

Re: Bring (hidden) window to front when clicking systray icon

Posted: Fri Sep 17, 2021 8:15 pm
by Cyllceaux
maybe

Code: Select all

SetActiveWindow(#Window)

Re: Bring (hidden) window to front when clicking systray icon

Posted: Fri Sep 17, 2021 9:01 pm
by blueznl
Tried it, doesn't work. It seems that the click on the icon, in spite of the event arriving, doesn't shift focus.

Re: Bring (hidden) window to front when clicking systray icon

Posted: Fri Sep 17, 2021 9:25 pm
by Mijikai
Try this:

Code: Select all

ShowWindow_(HWnd,#SW_RESTORE)

Re: Bring (hidden) window to front when clicking systray icon

Posted: Fri Sep 17, 2021 9:36 pm
by RASHAD

Code: Select all

SendMessage_(#HWND_BROADCAST, #WM_SYSCOMMAND, #SC_HOTKEY, hWnd)

Re: Bring (hidden) window to front when clicking systray icon

Posted: Fri Sep 17, 2021 10:09 pm
by blueznl
Mijikai wrote: Fri Sep 17, 2021 9:25 pm Try this:

Code: Select all

ShowWindow_(HWnd,#SW_RESTORE)
Hmm... this did it. Weird. I have to puzzle out why...

Thx anyway!

Re: Bring (hidden) window to front when clicking systray icon

Posted: Fri Sep 17, 2021 10:14 pm
by blueznl
RASHAD wrote: Fri Sep 17, 2021 9:36 pm

Code: Select all

SendMessage_(#HWND_BROADCAST, #WM_SYSCOMMAND, #SC_HOTKEY, hWnd)
And this one works as well, okay, now I have two ways :-) Thx!

Re: Bring (hidden) window to front when clicking systray icon

Posted: Sat Sep 18, 2021 10:16 am
by mk-soft
MSDN: https://docs.microsoft.com/en-us/window ... oundwindow

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  Macro SetActiveWindow(Window)
    SetForegroundWindow_(WindowID(Window))
  EndMacro
CompilerEndIf
Example (Update)

Code: Select all

;-TOP

EnableExplicit

;- Constant
Enumeration Windows
  #Main
EndEnumeration

Enumeration Menus
  #Menu
EndEnumeration

Enumeration MenuItems
  #MenuExitApplication
EndEnumeration
  
Enumeration Gadgets
  #List
EndEnumeration

Enumeration Statusbar
  #Status
EndEnumeration

Enumeration Images
  #SystrayImage
EndEnumeration

;- Global Variable
Global ExitApplication

;- Functions
Procedure UpdateWindow()
  
  Protected x, y, dx, dy, menu, status
  
  menu = MenuHeight()
  If IsStatusBar(#Status)
    status = StatusBarHeight(#Status)
  Else
    status = 0
  EndIf
  x = 0
  y = 0
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - menu - status
  ResizeGadget(#List, x, y, dx, dy)
  
EndProcedure

Procedure InitSysTray()
  CreateImage(#SystrayImage, 16, 16, 32, #Red)
  AddSysTrayIcon(0, WindowID(#Main), ImageID(#SystrayImage))
EndProcedure

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  Macro SetActiveWindow(Window)
    SetForegroundWindow_(WindowID(Window))
  EndMacro
CompilerEndIf
  
;- Main
Procedure Main()
  
  Protected event, dx, dy
  
  #WinStyle = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget
  dx = 800
  dy = 600
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, dx, dy, "Main Window", #WinStyle)
    
    ; Menu
    CreateMenu(#Menu, WindowID(#Main))
    MenuTitle("File")
    MenuItem(#MenuExitApplication, "E&xit")
    ; Gadgets
    ListViewGadget(#List, 0, 0, dx, dy)
    
    ; Statusbar
    CreateStatusBar(#Status, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    ; Systray
    InitSysTray()
    
    ; Init
    UpdateWindow()
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    ; Main Loop
    Repeat
      event = WaitWindowEvent()
      Select event
        Case #PB_Event_Menu
          Select EventMenu()
            CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
              Case #PB_Menu_About
                
              Case #PB_Menu_Preferences
                
              Case #PB_Menu_Quit
                ExitApplication = #True
                
            CompilerEndIf
              
            Case #MenuExitApplication
              ExitApplication = #True
              
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #List
              
          EndSelect
          
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case #Main
              ExitApplication = #True
              
          EndSelect
          
        Case #PB_Event_MinimizeWindow
          SetWindowState(#Main, #PB_Window_Minimize)
          HideWindow(#Main, #True)
          
        Case #PB_Event_SysTray
          HideWindow(#Main, #False)
          SetWindowState(#Main, #PB_Window_Normal)
          SetActiveWindow(#Main)
          
      EndSelect
      
    Until ExitApplication
    
  EndIf
  
EndProcedure : Main()

End

Re: Bring (hidden) window to front when clicking systray icon

Posted: Sat Sep 18, 2021 9:28 pm
by blueznl
mk-soft wrote: Sat Sep 18, 2021 10:16 am MSDN: https://docs.microsoft.com/en-us/window ... oundwindow
Nope, setforegroundwindow_ didn't to the job. It was one of the things I already tried. I don't know why...

Re: Bring (hidden) window to front when clicking systray icon

Posted: Sun Sep 19, 2021 12:15 pm
by mk-soft
Doesn't my example work?

I can hide the window with "Minimise" and also hide the window behind another window.

Re: Bring (hidden) window to front when clicking systray icon

Posted: Sat Oct 02, 2021 12:01 am
by blueznl
It does sometimes, but not always. The 'sendmessage' did it. I changed it to 'postmessage' and now it skips the 'hickups' as well.