Bring (hidden) window to front when clicking systray icon

Windows specific forum
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6113
Joined: Sat May 17, 2003 11:31 am
Contact:

Bring (hidden) window to front when clicking systray icon

Post 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?
( PB5.73LTS Win10 x64 Asrock AB350 Pro4 Ryzen 1600X GTX1060 )
( The path to enlightenment and the PureBasic Survival Guide right here... )
Cyllceaux
Enthusiast
Enthusiast
Posts: 353
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

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

Post by Cyllceaux »

maybe

Code: Select all

SetActiveWindow(#Window)
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6113
Joined: Sat May 17, 2003 11:31 am
Contact:

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

Post 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.
( PB5.73LTS Win10 x64 Asrock AB350 Pro4 Ryzen 1600X GTX1060 )
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Mijikai
Addict
Addict
Posts: 1032
Joined: Sun Sep 11, 2016 2:17 pm

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

Post by Mijikai »

Try this:

Code: Select all

ShowWindow_(HWnd,#SW_RESTORE)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4100
Joined: Sun Apr 12, 2009 6:27 am

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

Post by RASHAD »

Code: Select all

SendMessage_(#HWND_BROADCAST, #WM_SYSCOMMAND, #SC_HOTKEY, hWnd)
Egypt my love
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6113
Joined: Sat May 17, 2003 11:31 am
Contact:

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

Post 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!
( PB5.73LTS Win10 x64 Asrock AB350 Pro4 Ryzen 1600X GTX1060 )
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6113
Joined: Sat May 17, 2003 11:31 am
Contact:

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

Post 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!
( PB5.73LTS Win10 x64 Asrock AB350 Pro4 Ryzen 1600X GTX1060 )
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
mk-soft
Addict
Addict
Posts: 3194
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

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

Post 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
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6113
Joined: Sat May 17, 2003 11:31 am
Contact:

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

Post by blueznl »

Nope, setforegroundwindow_ didn't to the job. It was one of the things I already tried. I don't know why...
( PB5.73LTS Win10 x64 Asrock AB350 Pro4 Ryzen 1600X GTX1060 )
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
mk-soft
Addict
Addict
Posts: 3194
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

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

Post by mk-soft »

Doesn't my example work?

I can hide the window with "Minimise" and also hide the window behind another window.
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6113
Joined: Sat May 17, 2003 11:31 am
Contact:

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

Post 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.
( PB5.73LTS Win10 x64 Asrock AB350 Pro4 Ryzen 1600X GTX1060 )
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply