Systray tool and the active window

Just starting out? Need help? Post your questions and find answers here.
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Systray tool and the active window

Post by Michael Vogel »

Long story:
I made a tool which helps me work with my notebook until I get a replacement for some faulty keys. But there's an issue to make a screenshot of the active window in certain situations (seems that my keyboard hook routine gets blocked from certain programs), so I added an icon to the taskbar to be able to do the same functions like the keyboard shortcuts do.

Short problem description:
How to get the handle of the taskbar overflow? Otherwise I'll loose the handle of the 'real' active window.

Code: Select all

Procedure.s GetWinTitle(handle)

	Protected name.s
	Protected len

	name.s=Space(#MAX_PATH)
	len=GetWindowText_(handle,@name,#MAX_PATH)

	ProcedureReturn Left(name,len)

EndProcedure

MyWin=OpenWindow(0,0,0,100,100,"*")
MyBar=FindWindowEx_(#Null,#Null,"Shell_TrayWnd",#Null)
MyIco=AddSysTrayIcon(0,MyWin,LoadIcon_(#Null,#IDI_INFORMATION))

AddWindowTimer(0,0,100)
Repeat

	Dummy=GetForegroundWindow_()
	SetWindowTitle(0,Str(Dummy))
	If Dummy<>MyBar And Dummy<>MyWin And Dummy<>Active
		Active=Dummy
		Debug "New Active: "+Active+" = '"+GetWinTitle(Active)+"'"
	EndIf

Until WaitWindowEvent()=#PB_Event_CloseWindow
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Systray tool and the active window

Post by Little John »

Hi,

did you already look at these approaches?

by Hi-Toro, 2005 & 2019
by Luis, 2012
by Rashad, 2023

Unfortunately, none of them yields the expected result here on Windows 11, using PB 6.02 LTS (x64).
However, the codes might be helpful for you.
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Systray tool and the active window

Post by Michael Vogel »

Thanks for the links, as you said the codes don't work anymore.
There's also a thread in the older forum from 2009 about scanning the last active window but also with no usable solution because the taskbar overflow just didn't exist at this time.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Systray tool and the active window

Post by RASHAD »

Code: Select all

hWnd = FindWindow_("Shell_TrayWnd", #Null)
If hWnd
  hWnd = FindWindowEx_(hWnd, 0, "TrayNotifyWnd", #Null)
  If hWnd
    hWnd = FindWindowEx_(hWnd, 0, "SysPager", #Null)
    If hWnd
      hTray = FindWindowEx_(hWnd, 0, "ToolbarWindow32", "Notification Area")
      If hTray = 0
        hTray = FindWindowEx_(hWnd, 0, "ToolbarWindow32","User Promoted Notification Area")
        hOverflow = FindWindow_("NotifyIconOverflowWindow", #Null)
        hOverflowNotify = FindWindowEx_(hOverflow,0,"ToolbarWindow32", "Overflow Notification Area")
        Debug hTray
        Debug hOverflow
        Debug hOverflowNotify
      EndIf
    EndIf
  EndIf
EndIf
Egypt my love
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Systray tool and the active window

Post by RASHAD »

Code: Select all

Procedure LeftClick ()
  In.INPUT
  In\type        = #INPUT_MOUSE
  In\mi\dwFlags  = #MOUSEEVENTF_LEFTDOWN
  SendInput_(1,@In,SizeOf(INPUT))

  In\mi\dwFlags  = #MOUSEEVENTF_LEFTUP
  SendInput_(1,@In,SizeOf(INPUT))
EndProcedure


hWnd = FindWindow_("Shell_TrayWnd", #Null)
If hWnd
  hWnd = FindWindowEx_(hWnd, 0, "TrayNotifyWnd", #Null)
  If hWnd
    hWnd = FindWindowEx_(hWnd, 0, "SysPager", #Null)
    If hWnd
      hTray = FindWindowEx_(hWnd, 0, "ToolbarWindow32", "Notification Area")
      If hTray = 0
        hTray = FindWindowEx_(hWnd, 0, "ToolbarWindow32","User Promoted Notification Area")
        hOverflow = FindWindow_("NotifyIconOverflowWindow", #Null)
        hOverflowNotify = FindWindowEx_(hOverflow,0,"ToolbarWindow32", "Overflow Notification Area")
        Debug hTray
        Debug hOverflow
        Debug hOverflowNotify
      EndIf
    EndIf
  EndIf
EndIf

If OpenWindow(0, 0, 0, 800, 600, "PureBasic - SysTray Example", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  ButtonGadget(1,10,450,80,20,"Show")
  ButtonGadget(2,100,450,80,20,"Hide")  
  AddSysTrayIcon(1, WindowID(0), LoadImage(0, "CdPlayer.ico"))
  SysTrayIconToolTip(1, "Icon 1")
  
  Repeat
    Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          GetWindowRect_(hTray,r.RECT)
          SetCursorPos_(r\left+2,r\top+2)
          LeftClick ()          
        Case 2
          ShowWindow_(hOverflow,#SW_HIDE	)
      EndSelect
      EndSelect

  Until Quit = 1
  
EndIf

Egypt my love
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Systray tool and the active window

Post by Michael Vogel »

Hi Rashad,

your codes don't work here, mainly because I don't use an english version of Windows 11.

Your program does assign a handle to hOverflow but it's a different handle as the one connected to the arrow up symbol on the taskbar to show the tray symbols.
The two other handles will be zero.
Post Reply